I have created a table in my database where a user can store daily information about themselves, the data saves successfully to the table but there is an error if I try to update data already in the table (this needs to happen if the user has already entered information on that day, instead of creating a new row).
$sql = "SELECT * FROM $username WHERE day=?;";
// Here we initialize a new statement by connecting to the database (dbh.php file)
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
// If there is an error the user is sent to the enter data page again
header("Location: ../enterTodaysData.php?error=sqlerror");
exit();
}
else { //if there are no errors...
mysqli_stmt_bind_param($stmt, "s", $day); //binds the parameters to the statement
mysqli_stmt_execute($stmt); //executes the statement
$result = mysqli_stmt_get_result($stmt); //saves the result of the statement into the result variable
if ($row = mysqli_fetch_assoc($result)) { //if the user HAS already made an entry that day
$sql = "UPDATE $username (SET peakflow1 = $peakflow1 WHERE day=$day);";
$sql = "UPDATE $username (SET peakflow2 = $peakflow2 WHERE day=$day);";
$sql = "UPDATE $username (SET coughing = $coughing WHERE day=$day);";
$sql = "UPDATE $username (SET tightChest = $tightChest WHERE day=$day);";
$sql = "UPDATE $username (SET shortBreath = $shortBreath WHERE day=$day);";
$sql = "UPDATE $username (SET wheezing = $wheezing WHERE day=$day);";
$sql = "UPDATE $username (SET symptomOne = $symptomOne WHERE day=$day);";
$sql = "UPDATE $username (SET symptomTwo = $symptomTwo WHERE day=$day);";
$sql = "UPDATE $username (SET medication = $medication WHERE day=$day);";
$sql = "UPDATE $username (SET mood = $mood WHERE day=$day);";
$sql = "UPDATE $username (SET comments = $comments WHERE day=$day);";
$sql = "UPDATE $username (SET overall = $overall WHERE day=$day);";
header("Location: ../home.php?sql=success");
exit();
}
else{ //if the user has not
$sql = "INSERT INTO $username (day, peakflow1, peakflow2, medication, mood, coughing, tightChest, shortBreath, wheezing, symptomOne, symptomTwo, overall, comments) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"; //the question marks are placeholders
$stmt = mysqli_stmt_init($conn);
//an sql statement is prepared and the database is connected to
if (!mysqli_stmt_prepare($stmt, $sql)) {
// If there is an error the user is sent back to the signup page
header("Location: ../enterTodaysdata.php?error=sqlerror");
exit();
}
else {
//binds the paramaters and data to the statement
mysqli_stmt_bind_param($stmt, "siisiiiiiiiis", $day, $peakflow1, $peakflow2, $medication, $mood, $coughing, $tightChest, $shortBreath, $wheezing, $symptomOne, $symptomTwo, $overall, $comments);
//this executes the prepared statement and send it to the database, this registers the user.
mysqli_stmt_execute($stmt);
//sends the user back to the signup page, with a message confirming that it was a success
header("Location: ../home.php?sql=success");
exit();
}
}
}
}
The part of the code causing the problem starts at the line $sql = "UPDATE $username (SET peakflow1 = $peakflow1 WHERE day=$day);";
.
There are no results appearing on the screen, other than the "sqlerror" error message at the top of the screen, but the table does not update.