简体   繁体   English

MySQL不更新数据库中的记录

[英]Mysql is not updating records in Database

Please help as i have an issue. 请帮忙,因为我有问题。 I have a callback script, that another website is sending me some data, and mysql is not updating the records. 我有一个回调脚本,另一个网站向我发送了一些数据,而mysql没有更新记录。 I didn't mind securing my code yet, because it's not that important at this testing stage. 我不介意保护我的代码,因为在此测试阶段它并不那么重要。 My code is: 我的代码是:

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

Echoing the mysql query and then running it manual in my DB, works fine. 回显mysql查询,然后在我的数据库中手动运行它,效果很好。 I get around 10 requests per second. 我每秒收到10个请求。 Why doesn't it work when this code is running 为什么这段代码在运行时不起作用

Thanks 谢谢

EDIT: i added this line in the code: file_put_contents('errors.txt', mysql_error($conn).'\\n',FILE_APPEND); 编辑:我在代码中添加了这一行:file_put_contents('errors.txt',mysql_error($ conn)。'\\ n',FILE_APPEND); and the return is: 返回是:

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

So no errors from the mysql 所以从mysql没有错误

If the query is sound, as you say it is because you ran it independantly, then it must be either the connect or select database that has an error. 如果查询是正确的(如您所说的那样)是因为您是独立运行的,则该查询必须是具有错误的connect数据库或select数据库。 You need to get into the habit of testing the results of database access calls. 您需要养成测试数据库访问调用结果的习惯。

As I assume this code has no associated browser page, then you need to check for errors and send the error code somewhere that you can see it, something like this for example. 因为我假设此代码没有关联的浏览器页面,所以您需要检查错误并将错误代码发送到可以看到的地方,例如类似这样的代码。 Or regularly check your standard server logs for errors. 或定期检查标准服务器日志中是否有错误。

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

Of course here comes the standard warning: 当然,这里会出现标准警告:

You should not be using the mysql_ database access extension for new code. 您不应将mysql_数据库访问扩展名用于新代码。 It has been deprecated for years and will dissapear completely in PHP7 due out late 2015. Instead use the mysqli_ or PDO extensions. 它已被弃用多年,并将在2015年末发布的mysqli_完全消失。请改用mysqli_PDO扩展。 See this post to help in that decision if nothing else its quite a good read. 请参阅这篇文章,以帮助做出该决定,如果没有其他内容可以很好地阅读的话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM