PHP - 在while循环中插入多行

时间:2013-05-02 00:58:34

标签: php loops while-loop implode

我尝试使用implode在while循环中插入多行,但似乎无法使其工作。请帮忙。我想插入名为“weight”的多个值。像值30,20,15,10,5和剩余的emp_id和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>"; 

输出如下:

员工身份证号码:1001 工作/活动ID号:2002 工作/活动:监督计算机的维护和故障排除 重量:[__]%

员工身份证号码:1001 工作/活动ID号:2003 工作/活动:监督软件安装和维护 重量:[__]%

员工身份证号码:1001 工作/活动ID号:2004 工作/活动:维护,监控和排除虚拟终端故障 重量:[__]%

|提交|

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

3 个答案:

答案 0 :(得分: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]')");
}

答案 1 :(得分:0)

您正在错误地解决$ _POST数组,因此不要重新编写脚本,请尝试此操作。

在mysql_connect

之前,将此行添加到page2.php中的代码中
echo '<pre>' . print_r($_POST,true) . '</pre>';

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

答案 2 :(得分:-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/