I am trying to update a mysql database from a list that is submitted from a textarea. Every line in the textarea represents a value that is to be compared to existing entries in the database. If there is a match than the Status in that row has to be updated. With this Code only the last line from the textarea has an effect. What am I doing wrong?
<?php
if(isset($_POST['regnr']))
{
$db_host = "localhost";
$db_user = "root";
$db_pass = "root";
$db_name = "sonstwas";
$port = 3306;
// Create connection
$conn = new mysqli($db_host, $db_user, $db_pass, $db_name);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$new_status = 2;
$lines = preg_split("/\r\n/", $_POST["regnr"]);
foreach ($lines as $key => $value){
$sql = "UPDATE kunden ". "SET Status = $new_status ". "WHERE RegNr = '$value'";
//echo 'rows '.$key.' ist: "'.$value.'"<br>';
}
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
}
else
{
?>
<form method="post" action="<?php $_PHP_SELF ?>">
<textarea name="regnr" rows="30" id="regnr"></textarea>
<input name="update" type="submit" id="update" value="Update">
</form>
<?php
}
?>
Thanks a lot
You're looping and preparing sql-statements, but you're not doing anything with that (except of course with the last prepared statement). So move the execution of the sql statements inside the loop.
foreach ($lines as $key => $value){
$sql = "UPDATE kunden ". "SET Status = $new_status ". "WHERE RegNr = '$value'";
//echo 'rows '.$key.' ist: "'.$value.'"<br>';
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.