Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Im trying to insert multiple rows in a while loop with a form using implode but cannot seem to make it work. Pls help. I want to insert multiple values named "weight". Like values 30,20,15,10,5 and the remaining emp_id and task_id. Forgive me for being a newbie, but please bare with me. Im still learning.

page1.php

$sql = mysql_query("SELECT 
                    task_tbl.task_id,
                    task_tbl.task_name,
                    task_tbl.task_sem,
                    task_tbl.task_yr,
                    task_tbl.post_id,
                    post_tbl.post_id,
                    post_tbl.post_name AS ppost_name
                    FROM 
                    task_tbl 
                    LEFT JOIN 
                    post_tbl
                    ON
                    task_tbl.post_id = post_tbl.post_id 
                    WHERE 
                    task_sem = '$sem' 
                    AND 
                    task_yr = '$yr' 
                    ORDER BY 
                    task_id ASC");

echo '<form action = "upds_peval.php" name = "add" method = "post">';

while($row = mysql_fetch_assoc($sql)){

        echo "<br/>";
        echo "<b>Employee ID No.:</b>"; 
        echo '<input size = "2" type = "text" name = "emp_id'.$id.'" value = "';
        echo $_POST['emp_id'];
        echo '"/>';
        echo "<br/>";   
        echo "<b>Work/Activity ID No.:</b> ";
        echo '<input size = "2" type = "text" name = "task_id'.$row['task_id'].'" value = "';
        echo $row['task_id'];
        echo '"/>';
        echo "<br/>";
        echo "<b>Work/Activity:</b> ";
        echo $row['task_name'];
        echo "<br/>";
        echo "<b>Weight:</b> ";
        echo '<input size = "1" type="text" name="weight" value = ""/>';
        echo "%";
        echo "<br/>";

        }

        echo '<input type="submit" name="submit" value="ADD"/>';
        echo "</form>"; 

the output would be like:

Employee ID No.: 1001 Work/Activity ID No.: 2002 Work/Activity: Supervises Maintenance and Troubleshooting of Computers Weight: [__]%

Employee ID No.: 1001 Work/Activity ID No.: 2003 Work/Activity: Supervises Software Installation and Maintenance Weight: [__]%

Employee ID No.: 1001 Work/Activity ID No.: 2004 Work/Activity: Maintains, Monitors, and Troubleshoots Virtual Terminals Weight: [__]%

|SUBMIT|

page2.php

mysql_connect ("localhost", "root","")  or die (mysql_error());
    mysql_select_db ("emp_db0");

    if(isset($_POST['submit'])){
    $_POST['weight'];


    $vals=implode(",",$_POST);  

    error_reporting(E_ALL ^ E_NOTICE);

    mysql_query("INSERT INTO peval_tbl(weight,task_id,emp_id) VALUES('$vals')");
    echo "<br/>";
    echo "You have successfully added work/activities!";
    echo "<br/>";   
    }
share|improve this question
    
The output should be, but what are you getting? Nothing? Wrong output? –  Daryl Gill May 2 '13 at 1:03
    
are you assigning the value of $_POST['weight'] to any variable? what value is contained by your $_POST in your statement $vals = implode(",",$_POST) ? –  Þaw May 2 '13 at 1:05
    
Why are you using implode? Why not just '".$_POST['weight']."','".$_POST['task_id']."','".$_POST['emp_id']."' –  shapeshifter May 2 '13 at 1:05
    
Also what is $_POST['weight']; doing? –  shapeshifter May 2 '13 at 1:06
    
well no output for page2.php. im trying to debug. probably its wrong. i dont know yet. still guessing wat could work –  raziel2101 May 2 '13 at 1:09
show 2 more comments

2 Answers

up vote 1 down vote accepted

okay based on my impression of your question, you want a form that is extensible right, where you can access all of its instance in one name?

1st Instance of form sample:

<input size = "2" type = "text" name = "emp_id[]" value = "1">
<input size = "2" type = "text" name = "task_id[]" value = "5001">
<input size = "2" type = "text" name = "weight[]" value = "50">

2nd Instance sample:

<input size = "2" type = "text" name = "emp_id[]" value = "2">
<input size = "2" type = "text" name = "task_id[]" value = "5003">
<input size = "2" type = "text" name = "weight[]" value = "30">

this will convert your posts into arrays

to access:

$var_emp_id = $_POST["emp_id"];
$var_task_id = $_POST["task_id"];
$var_weight_id = $_POST["weight"];

use a loop to access those variables. to access the first instance of emp_id u use:

$var_emp_id[0]; //will output 1
$var_emp_id[1]; //will output 2

$var_task_id[0]; //will output 5001
$var_task_id[1]; //will output 5003

to insert it on your database:

for ($i = 0; $i <= count($var_emp_id); $i++){
     mysql_query("INSERT INTO peval_tbl(weight,task_id,emp_id) VALUES('$weight[$i]','$var_task_id[$i]','$var_emp_id[$i]')");
}
share|improve this answer
    
tnx a lot mate... it worked like charm! cheers to u! –  raziel2101 May 2 '13 at 4:26
    
np :), but i suggest you use PDO or mysqli for your Queries, mysql_query already has been depreciated. –  Viscocent May 2 '13 at 5:38
add comment

You are addressing the $_POST array incorrectly, so rather than recode your script try this.

Add this line to your code in page2.php before the mysql_connect

echo '<pre>' . print_r($_POST,true) . '</pre>';

This will dump a nice print of the post array so you can see how data arrives in $_POST.

share|improve this answer
    
tnx.. i only got this –  raziel2101 May 2 '13 at 1:25
    
Array ( [emp_id] => 1001 [task_id5001] => 5001 [weight] => 90 [task_id5002] => 5002 [task_id5003] => 5003 [task_id5004] => 5004 [task_id5005] => 5005 [task_id5006] => 5006 [task_id5009] => 5009 [submit] => ADD ) –  raziel2101 May 2 '13 at 1:26
    
the name "wieght" is wrong. tsk. How do I call them one by one and insert them altogether? –  raziel2101 May 2 '13 at 1:28
    
use foreach, sir @raziel2101 you may want to learn PDO or MVC Frameworks such as CodeIgniter, CakePHP mysql_* functions are no longer used because of some issues like securtiy. –  Þaw May 2 '13 at 1:47
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.