0

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/>";   
    }
4

3 回答 3

1

好吧,根据我对您问题的印象,您想要一个可扩展的表单,您可以在其中以一个名称访问其所有实例?

表格样本的第一个实例:

<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">

第二个例子:

<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">

这会将您的帖子转换为数组

访问:

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

使用循环访问这些变量。要访问 emp_id 的第一个实例,请使用:

$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

将其插入您的数据库:

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]')");
}
于 2013-05-02T01:55:38.870 回答
0

您对 $_POST 数组的寻址不正确,因此与其重新编码脚本,不如试试这个。

在 mysql_connect 之前将此行添加到 page2.php 中的代码中

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

这将转储 post 数组的漂亮打印,以便您可以看到数据如何到达 $_POST。

于 2013-05-02T01:07:55.313 回答
-2
$sql = "select * from login where nombre !='carlos'";
$result = $conn->query($sql);
$index = 1;
while($row = mysqli_fetch_array($result)){
    $valor = $row["nombre"];
    $valor2 = $row["apellido"];
    $insertsql= "insert into lista (`dato1`,`dato2`)values ('$valor', now())";
    // mysql_query($insertsql);
    $conn->query($insertsql);
    $index++;
}

http://www.baulphp.com/insertar-multiples-registros-ciclo-while-php/

于 2017-10-17T18:27:48.830 回答