gpt4 book ai didi

php - Mysql 不更新数据库中的记录

转载 作者:行者123 更新时间:2023-11-29 21:58:24 26 4
gpt4 key购买 nike

请帮助我,因为我有问题。我有一个回调脚本,另一个网站正在向我发送一些数据,而 mysql 没有更新记录。我还不介意保护我的代码,因为在这个测试阶段它并不那么重要。我的代码是:

    $dbhost = 'localhost';
$dbuser = 'user';
$dbpass = 'pass';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

$dbname = 'dbname';
mysql_select_db($dbname);
$postData = $_POST; //GET ALL POST DATA

//GET ALL DATA FROM POST AND PREPARE IT FOR SQL
$mId = $postData['id'];
$mDone = date('Y-m-d H:i:s',$postData['donedate']);
$mStatus = $postData['status'];
$mText = $postData['txtstatus'];
if ($mText == 'DELIVRD')
$mText = 'DELIVERED'; //Correction of test status
$mPhone = $postData['receiver'];

//ADD TO DB
if ($postData['type'] == 1){ //success
$sql = mysql_query("UPDATE message_details SET doneDate='$mDone', status='$mText' WHERE contact='$mPhone' AND msgId='$mId'");
echo "UPDATE message_details SET doneDate='$mDone', status='$mText' WHERE contact='$mPhone' AND msgId='$mId'"
}elseif ($postData['type'] == 2) {//FAILED
$sql = mysql_query("UPDATE message_details SET doneDate='$mDone', status='FAILED' WHERE contact='$mPhone' AND msgId='$mId'");
echo "UPDATE message_details SET doneDate='$mDone', status='FAILED' WHERE contact='$mPhone' AND msgId='$mId'"
}

回显 mysql 查询,然后在我的数据库中手动运行它,工作正常。我每秒收到大约 10 个请求。为什么这段代码运行时不起作用

谢谢

编辑:我在代码中添加了这一行:file_put_contents('errors.txt', mysql_error($conn).'\n',FILE_APPEND);返回结果为:

\n\n\n\n\n\n\n\n\n\n\n

所以 mysql 没有错误

最佳答案

如果查询是正确的,正如您所说,这是因为您独立运行了它,那么一定是连接或选择数据库出现了错误。您需要养成测试数据库访问调用结果的习惯。

由于我假设此代码没有关联的浏览器页面,因此您需要检查错误并将错误代码发送到您可以看到的地方,例如这样。或者定期检查标准服务器日志是否有错误。

$dbhost = 'localhost';
$dbuser = 'user';
$dbpass = 'pass';

$conn = mysql_connect($dbhost, $dbuser, $dbpass)
if ( ! $conn ) {
file_put_contents('admin_error.log',
mysql_error() . "\n",
FILE_APPEND);
exit;
}

$dbname = 'dbname';
if ( ! mysql_select_db($dbname) ) {
file_put_contents('admin_error.log',
mysql_error() . "\n",
FILE_APPEND);
exit;
}

当然这是标准警告:

You should not be using the mysql_ database access extension for new code. It has been deprecated for years and will dissapear completely in PHP7 due out late 2015. Instead use the mysqli_ or PDO extensions. See this post to help in that decision if nothing else its quite a good read.

关于php - Mysql 不更新数据库中的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32903545/

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