dongluobei9359 2015-10-02 08:45
浏览 83

Mysql没有更新数据库中的记录

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. 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. I get around 10 requests per second. 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).' ',FILE_APPEND); and the return is:

So no errors from the mysql

  • 写回答

1条回答 默认 最新

  • dqthn68688 2015-10-02 09:00
    关注

    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. 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() . "
    ", 
                          FILE_APPEND);
        exit;
    }
    
    $dbname = 'dbname';
    if ( ! mysql_select_db($dbname) ) {
        file_put_contents('admin_error.log', 
                          mysql_error() . "
    ",
                          FILE_APPEND);
        exit;
    }
    

    Of course here comes the standard warning:

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100