开发者

Why isn't the mysql database updating in this case?

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->execu开发者_运维技巧te();
} 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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜