I'm trying to insert multiple rows in a while loop with a form using implode but cannot seem to make it work. I want to insert multiple values named "weight". Like values 30,20,15,10,5 and the remaining emp_id and task_id.
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/>";
}
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]')");
}