Reputation: 867
I have the following code snippet:
$get_data = mysqli_query ($connect, "SELECT * FROM users WHERE username = '$username'");
$row2 = mysqli_fetch_assoc ($get_data);
$age = $row2['age'];
$get_data2 = mysqli_query ($connect, "SELECT * FROM user_bio WHERE username = '$username'");
$row3 = mysqli_fetch_assoc ($get_data2);
$bio = $row3['about_me'];
$studying = $row3['studying'];
$lang = $row3['language'];
$rel_status = $row3['relationship_status'];
$about_me = htmlentities(trim(strip_tags(@$_POST['biotextarea'])));
$new_studying = htmlentities(trim(strip_tags(@$_POST['studying'])));
$new_lang = htmlentities(trim(strip_tags(@$_POST['lang'])));
$new_rel = htmlentities(strip_tags(@$_POST['rel']));
if(isset($_POST['update_data'])){
// need to check if the username has data already in the db, if so, then we update the data, otherwise we insert data.
$get_bio = mysqli_query($connect, "SELECT * FROM user_bio WHERE username ='$username'");
$row_returned = mysqli_num_rows($get_bio);
if ($row_returned == 0){
$insert_query = mysqli_query ($connect, "INSERT INTO user_bio (id, age, studying, language, relationship_status, username, about_me)
VALUES ('', '$age','$new_studying','$new_lang','$new_rel', '$username', '$about_me'");
echo " <div class='details_updated'>
<p> Details added successfully! </p>
</div>";
} else {
$update_details_query = mysqli_query ($connect, "UPDATE user_bio SET studying ='$new_studying', language ='$new_lang',
relationship_status = '$new_rel', about_me = '$about_me' WHERE username ='$username'");
echo " <div class='details_updated'>
<p> Details updated successfully! </p>
</div>";
}
}
The idea is that when a user uploads their bio
, the query with either INSERT
or UPDATE
based on if a row already exists for that username
. For example, I have one row manually inserted for username Conor
:
id: 1
age: 30
studying: Business
language: English
relationship_status: Single
username: conor
about_me: This is conor's bio.
If I log in as Conor, and go to account_settings_bio.php
, where conor can edit their bio, the UPDATE
query (else statement) would run because there is a row in the db for username conor
. However, if I log in as Alice
and go to account_settings_bio.php
, the INSERT query should run because no data for alice exists in the user_bio
table.
Now, the issue is that, when I am logged in as Alice, and I go to account_settings_bio.php
, I add all my details and click submit, it echo's the message which should occur when the INSERT
query is successful i.e. <p> Details added successfully! </p>
- but no new row is added in the database?
Upvotes: 1
Views: 193
Reputation: 108450
I'm not seeing the setting that causes mysqli to throw an exception when an SQL error happens.
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
If that's not set, then mysqli doesn't throw an error, and the code has to do the check to see if the SQL statement was successful.
After this:
$insert_query = mysqli_query ($connect, "INSERT INTO
You'd need to check if $insert_query
evaluates to TRUE to know if the statement execution was successful.
Otherwise, the code is putting it's pinky finger to the corner of its mouth, Dr.Evil-style, and saying "I just assume it all went to plan. What?"
Upvotes: 0