更新WHERE无法正常工作?

时间:2014-06-10 16:31:20

标签: php mysql

出于某种原因,当我使用WHERE语句时,我无法更新数据库。我是所有这一切的新手,但在我的大脑中它应该有效。任何帮助将不胜感激。我只回应$ stu_dent,看看它是否奏效。

        <?php

include 'connect.php';

    print_r($_POST);

// Get values from form 
$resp=$_POST['responsibility'];
$org=$_POST['organization'];
$ind=$_POST['independentwork'];
$coll=$_POST['collaboration'];
$init=$_POST['initiative'];
$self=$_POST['selfregulation'];

$sql= "SELECT * FROM studentlist";

$result=mysqli_query($connect, $sql);


while($row = mysqli_fetch_array($result)) {

$stu_dent=$row['Student'];

 $sql1 = "UPDATE studentlist 
    SET responsibility='$resp', organization='$org', independentwork='$ind', 
    collaboration='$coll', initiative='$init', selfregulation='$self' WHERE Student='$stu_dent'";

    $result1=mysqli_query($connect, $sql1);

}





// if successfully insert data into database, displays message "Successful". 
if($result1){



echo $sql1;

echo "Successful";


}

else {
echo "ERROR";
}
?> 

<?php 
// close connection 
mysqli_close($connect);
?>

这是我在print_r语句...和echo $ sql1

之后得到的
   Array
(
    [responsibility] => G
    [organization] => G
    [independentwork] => G
    [collaboration] => G
    [initiative] => G
    [selfregulation] => G
)
UPDATE studentlist 
    SET responsibility='G', organization='G', independentwork='G', 
    collaboration='G', initiative='G', selfregulation='G' WHERE Student='STALEY, PETER DOUGLA'Successful<br />
<br />
<br />
<br />

该名称实际上是数据库中的姓氏,而不是我尝试更新的名称。

1 个答案:

答案 0 :(得分:1)

如果未在while行中运行更新查询,则变量将设置为最后一个值。

或者:从数据库中获取一行或在while循环中运行更新查询以更新每一行。