gpt4 book ai didi

php - INSERT 查询不插入数据,但是执行表明 INSERT 成功的回显

转载 作者:行者123 更新时间:2023-11-30 22:22:43 31 4
gpt4 key购买 nike

我有以下代码片段:

$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>";
}
}

这个想法是,当用户上传他们的 bio , 查询 INSERTUPDATE基于是否已经存在该行 username .例如,我为用户名 Conor 手动插入了一行:

id: 1
age: 30
studying: Business
language: English
relationship_status: Single
username: conor
about_me: This is conor's bio.

如果我以 Conor 身份登录,然后转到 account_settings_bio.php ,conor 可以编辑他们的简历,UPDATE查询(else 语句)将运行,因为数据库中有一行用户名 conor .但是,如果我以 Alice 身份登录然后转到 account_settings_bio.php ,INSERT 查询应该运行,因为 user_bio 中不存在 alice 的数据。表。

现在,问题是,当我以 Alice 身份登录时,我转到 account_settings_bio.php ,我添加了我的所有详细信息并单击提交,它回显了 INSERT 时应该出现的消息查询成功即 <p> Details added successfully! </p> - 但数据库中没有添加新行?

最佳答案

我没有看到导致 mysqli 在发生 SQL 错误时抛出异常的设置。

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

如果未设置,则 mysqli 不会抛出错误,代码必须检查 SQL 语句是否成功。

在此之后:

  $insert_query = mysqli_query ($connect, "INSERT INTO 

您需要检查 $insert_query 是否为 TRUE 以了解语句执行是否成功。

否则,代码会将它的小指放在嘴角,邪恶博士的风格,然后说“我只是假设一切都按计划进行。什么?”

关于php - INSERT 查询不插入数据,但是执行表明 INSERT 成功的回显,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36242139/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com