user3006141
user3006141

Reputation: 23

Update with WHERE not working?

For some reason I cannot get my database to update when I use a WHERE statement. I'm new to all of this, but in my brain it should work. Any help would be greatly appreciated. I only echoed the $stu_dent to see if it worked.

        <?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);
?>

Here's what I get after the the print_r statement...and the 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 />

The name is actually the last name in the database, and not the name I tried to update.

Upvotes: 1

Views: 81

Answers (1)

Lelio Faieta
Lelio Faieta

Reputation: 6663

If you do not run the update query inside the while row your variables will be set to the last value.

Alternatively: get a single row from the db or run the update query inside the while loop to update each row.

Upvotes: 1

Related Questions