为什么mysql数据库在这种情况下没有更新?

发布于 2024-11-26 11:49:34 字数 3796 浏览 2 评论 0原文

我有这个 PHP 脚本:

require_once('global.php'); //connects to db and various functions used
//select the user's information
$statement = $db->prepare("SELECT * FROM user WHERE id=1");
$statement->execute();
$result = $statement->fetchObject();
//get the chat
$string = $result->chats;

$from = $_REQUEST['from'];
$msg = $_REQUEST['msg'];
$sent = $_REQUEST['sent'];

//see what we should do with the recieved data
if($string == "") {
    //there isnt any chats right now, we must add the first ever contact
    $string = "<ResultSet><chats><chat><messages><sms><from>{$from}</from><msg>{$msg}</msg><sent>{$sent}</sent></sms></messages><contact>{$from}</contact></chat></chats></ResultSet>";
    //send the data back to the user's row in the database
    $statement = $db->prepare("UPDATE user SET chats='{$string}' WHERE id=1");
    $statement->execute();
} else if($from == $result->name) {
    //the user is sending a message to a contact. we now need to get the "to" value.
    $to = trim(str_replace("_", " ", $_REQUEST['to']));
    //add the sms to the contact's chat
    $string = str_replace("</sms></messages><contact>{$to}</contact>", "</sms><sms><from>{$from}</from><msg>{$msg}</msg><sent>{$sent}</sent></sms></messages><contact>{$to}</contact>", $string);
    //send the data back to the user's row in the database
    $statement = $db->prepare("UPDATE user SET chats='{$string}' WHERE id=1");
    $statement->execute();
} else if(strstr($string, "<contact>".$from."</contact>")) {
    //The contact that sent the message already exists in this user's row, add the message to the contact's chat
    $string = str_replace("</sms></messages><contact>{$from}</contact>", "</sms><sms><from>{$from}</from><msg>{$msg}</msg><sent>{$sent}</sent></sms></messages><contact>{$from}</contact>", $string);
    //send the data back to the user's row in the database
    $statement = $db->prepare("UPDATE user SET chats='{$string}' WHERE id=1");
    $statement->execute();
} else {
    //Person who sent the message doesnt exist in the chats, add him.
    $string = str_replace("</chats>", "<chat><messages><sms><from>{$from}</from><msg>{$msg}</msg><sent>{$sent}</sent></sms></messages><contact>{$from}</contact></chat></chats>", $string);
    //send the data back to the user's row in the database
    $statement = $db->prepare("UPDATE user SET chats='{$string}' WHERE id=1");
    $statement->execute();
}

问题出在这个 else if 代码中:

else if($from == $result->name) {
//the user is sending a message to a contact. we now need to get the "to" value.
$to = trim(str_replace("_", " ", $_REQUEST['to']));
//add the sms to the contact's chat
$string = str_replace("</sms></messages><contact>{$to}</contact>", "</sms><sms><from>{$from}</from><msg>{$msg}</msg><sent>{$sent}</sent></sms></messages><contact>{$to}</contact>", $string);
//send the data back to the user's row in the database
$statement = $db->prepare("UPDATE user SET chats='{$string}' WHERE id=1");
$statement->execute();
}

我确信代码正在通过这个 else 运行,我已经回显并确认了。当我使用 $string = str_replace() 时,我打印了 $string 并且它确实被替换了。但是,当我将数据提交到数据库中的行时,刷新数据库时什么也没有发生。为什么它对 else 不起作用,但对 if 语句的其余部分(以及它之前的 if)起作用?

这对我来说没有意义。我尽力对代码进行适当的注释,如果您需要解释,请询问。

I have this PHP script:

require_once('global.php'); //connects to db and various functions used
//select the user's information
$statement = $db->prepare("SELECT * FROM user WHERE id=1");
$statement->execute();
$result = $statement->fetchObject();
//get the chat
$string = $result->chats;

$from = $_REQUEST['from'];
$msg = $_REQUEST['msg'];
$sent = $_REQUEST['sent'];

//see what we should do with the recieved data
if($string == "") {
    //there isnt any chats right now, we must add the first ever contact
    $string = "<ResultSet><chats><chat><messages><sms><from>{$from}</from><msg>{$msg}</msg><sent>{$sent}</sent></sms></messages><contact>{$from}</contact></chat></chats></ResultSet>";
    //send the data back to the user's row in the database
    $statement = $db->prepare("UPDATE user SET chats='{$string}' WHERE id=1");
    $statement->execute();
} else if($from == $result->name) {
    //the user is sending a message to a contact. we now need to get the "to" value.
    $to = trim(str_replace("_", " ", $_REQUEST['to']));
    //add the sms to the contact's chat
    $string = str_replace("</sms></messages><contact>{$to}</contact>", "</sms><sms><from>{$from}</from><msg>{$msg}</msg><sent>{$sent}</sent></sms></messages><contact>{$to}</contact>", $string);
    //send the data back to the user's row in the database
    $statement = $db->prepare("UPDATE user SET chats='{$string}' WHERE id=1");
    $statement->execute();
} else if(strstr($string, "<contact>".$from."</contact>")) {
    //The contact that sent the message already exists in this user's row, add the message to the contact's chat
    $string = str_replace("</sms></messages><contact>{$from}</contact>", "</sms><sms><from>{$from}</from><msg>{$msg}</msg><sent>{$sent}</sent></sms></messages><contact>{$from}</contact>", $string);
    //send the data back to the user's row in the database
    $statement = $db->prepare("UPDATE user SET chats='{$string}' WHERE id=1");
    $statement->execute();
} else {
    //Person who sent the message doesnt exist in the chats, add him.
    $string = str_replace("</chats>", "<chat><messages><sms><from>{$from}</from><msg>{$msg}</msg><sent>{$sent}</sent></sms></messages><contact>{$from}</contact></chat></chats>", $string);
    //send the data back to the user's row in the database
    $statement = $db->prepare("UPDATE user SET chats='{$string}' WHERE id=1");
    $statement->execute();
}

The problem is in this else if code:

else if($from == $result->name) {
//the user is sending a message to a contact. we now need to get the "to" value.
$to = trim(str_replace("_", " ", $_REQUEST['to']));
//add the sms to the contact's chat
$string = str_replace("</sms></messages><contact>{$to}</contact>", "</sms><sms><from>{$from}</from><msg>{$msg}</msg><sent>{$sent}</sent></sms></messages><contact>{$to}</contact>", $string);
//send the data back to the user's row in the database
$statement = $db->prepare("UPDATE user SET chats='{$string}' WHERE id=1");
$statement->execute();
}

I am sure the code is running through this else, I have echo'd and confirmed. When I use the $string = str_replace(), I printed the $string and it had indeed replaced. But when I submit the data to the row in the database, nothing happens when I refresh my db. Why does it not work for this else but does for the rest of the if statement (and the if before it)?

It doesn't make sense to me. The code I tried my best to comment it appropriately, if you need something explained just ask.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

失与倦" 2024-12-03 11:49:34

如果 $db 不是 MySQLi 或 PDO 的实例,而是数据库包装器,请查看实例化为 $db 的类并确保它不会启动事务。如果启动了事务,您将需要提交事务,以便应用/保存对数据库的更改。

In case $db is not an instance of MySQLi or PDO but a db wrapper, look at the class that is instantiated as $db and make sure it doesn't start a transaction. If there is a transaction initiated, you will need to commit the transaction so that the changes to the database are applied/saved.

遗弃M 2024-12-03 11:49:34

我不知道为什么,但我必须更改我的代码,以便我在执行函数而不是查询中发送变量。在查询中我会输入“?”我想要变量的地方,在execute()函数中我会放置一个像这样的数组,

$statement->execute(array($string, 1));

这似乎已经解决了我的问题。

I'm not sure why but I had to change my code up so Iwould send the variables in the execute function instead of in the query. In the query I would put "?" where I want the variables, and in the execute() function I would put an array like

$statement->execute(array($string, 1));

That seems to have fixed my problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文