在while循环php中插入和更新

时间:2017-08-08 12:03:42

标签: php

$sql="SELECT activity,work_order FROM works";
$result=mysql_query($sql);
while($row=mysql_fetch_assoc($result)){
    if($row['work_order']==$work_order && $row['activity']==$activity){
        $sql="UPDATE works SET wei_out='$wei_out',len_out='$len_out',end_time='$end_time' WHERE work_order='$work_order' AND activity='$activity'";
        break;
    }
    else{
        $sql="INSERT INTO works (jobno,date,start_time,work_order,worker,activity,status,len_in,wei_in,grams) values ('$jobno','$date','$start_time','$work_order','$worker','$activity','$status','$len_in','$wei_in','$grams')";
        break;
    }
}

上面的插入和更新代码在while循环中不起作用,任何人都可以帮助我解决

2 个答案:

答案 0 :(得分:0)

您忘记了{for循环中的mysqli_query();。请使用mysqli_*而不是mysql_*

$con = mysqli_connect("localhost", "my_user", "my_password", "my_db");

$sql = "SELECT activity, work_order FROM works";
$result = mysqli_query($con, $sql);

// Don't forget to escape your input!
$len_out    = mysqli_real_escape_string($con, $len_out);
$wei_out    = mysqli_real_escape_string($con, $wei_out);
$end_time   = mysqli_real_escape_string($con, $end_time);
$start_time = mysqli_real_escape_string($con, $start_time);
$work_order = mysqli_real_escape_string($con, $work_order);
$activity   = mysqli_real_escape_string($con, $activity);
$jobno      = mysqli_real_escape_string($con, $jobno);
$date       = mysqli_real_escape_string($con, $date);
$worker     = mysqli_real_escape_string($con, $worker);
$status     = mysqli_real_escape_string($con, $status);
$len_in     = mysqli_real_escape_string($con, $len_in);
$wei_in     = mysqli_real_escape_string($con, $wei_in);
$grams      = mysqli_real_escape_string($con, $grams);

while ($row = mysqli_fetch_assoc($result)) {
    if ($row['work_order'] == $work_order && $row['activity'] == $activity) {
        $sql = "UPDATE works SET wei_out='$wei_out', len_out='$len_out', end_time='$end_time' WHERE work_order='$work_order' AND activity='$activity'";
        mysqli_query($con, $sql);  // Run the query
        break;
    } else {
        $sql = "INSERT INTO works (jobno, date, start_time, work_order, worker, activity, status, len_in, wei_in, grams) VALUES ('$jobno', '$date', '$start_time', '$work_order', '$worker', '$activity', '$status', '$len_in', '$wei_in', '$grams')";
        mysqli_query($con, $sql);  // Run the query
        break;
    }
}

答案 1 :(得分:0)

您忘记执行存储在$sql变量中的语句以及使用mysqli来解决您的安全问题。

$sql="SELECT activity,work_order FROM works";
$result=mysqli_query($connection, $sql);
while($row=mysqli_fetch_assoc($result)){
    if($row['work_order']==$work_order && $row['activity']==$activity){
        $sql="UPDATE works SET wei_out='".$wei_out."',len_out='".$len_out."',end_time='".$end_time."' WHERE work_order='".$work_order."' AND activity='".$activity."'";
        mysqli_query($connection, $sql);
        break;
    }
    else{
        $sql="INSERT INTO works (jobno,date,start_time,work_order,worker,activity,status,len_in,wei_in,grams) values ('".$jobno."','".$date."','".$start_time."','".$work_order."','".$worker."','".$activity."','".$status."','".$len_in."','".$wei_in."','".$grams."')";
        mysqli_query($connection, $sql);
        break;
    }
}