简体   繁体   English

PHP mysql查询不起作用

[英]PHP mysql query doesn't work

This is the query code: 这是查询代码:

if (isset($_POST['moduleAction']) && ($_POST['moduleAction'] == 'edit')) {

            $date = date('Y-m-d H:i:s', time());                
            $stmt = $db->prepare('UPDATE todolist SET what = ?, priority = ?, added_on = ? WHERE id = ?');
            $stmt->execute(array($what, $priority + 1, $date, $id));

    }

My db connection: 我的数据库连接:

<?php

    try {
        $db = new PDO('mysql:host=' . DB_HOST .';dbname=' . DB_NAME . ';charset=utf8mb4', DB_USER, DB_PASS);
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

    } catch (Exception $e) {
        showDbError('connect', $e->getMessage());
    }

The query is not executed on the db, on another page in the same document i am executing queries to the same db without problem. 查询不在数据库上执行,在同一文档的另一页上,我正在对同一数据库执行查询而没有问题。 I've tried executing it without a prepared statement, double quotes, restarting te connection,... nothing works. 我已经尝试过在没有准备好的语句,双引号,重新启动te连接的情况下执行它,但是没有任何效果。

Anyone who can push me in the right direction? 任何人都可以将我推向正确的方向?

EDIT 编辑

Setting variables: 设置变量:

$priorities = array('low','normal','high'); // The possible priorities of a todo
    $formErrors = array(); // The encountered form errors

    $id = isset($_GET['id']) ? (int) $_GET['id'] : 0; // The passed in id of the todo

    $what = isset($_POST['what']) ? $_POST['what'] : ''; // The todo that was sent in via the form
    $priority = isset($_POST['priority']) ? $_POST['priority'] : 'low'; // The priority that was sent in via the form

If your query is not execute and you get no error I would say that something is wrong with this 如果您的查询未执行并且没有错误,我会说这有问题

if(isset($_POST['moduleAction']) && ($_POST['moduleAction'] == 'edit')) {

Make sure your moduleAction is set in your post array and is really egal to 'edit'. 确保在您的post数组中设置了moduleAction,并且对于“ edit”而言确实如此。

Hope this helps 希望这可以帮助

You should always check for errors, there is so many reason an query fail to work as expected. 您应该始终检查错误,因为查询失败的原因有很多。

Here is the right way to check: 这是检查的正确方法:

 if(isset($_POST['moduleAction']) && ($_POST['moduleAction'] == 'edit')) {
   $date = date('Y-m-d H:i:s', time());
   $query ='UPDATE todolist SET what = ?, priority = ?, added_on = ? WHERE id = ?';   
   if($stmt = $db->prepare($query)){
        if($stmt->execute(array($what, $priority + 1, $date, $id))){
            echo 'execute() successful';
            if($stmt->rowCount() > 0){
                echo 'Affected a row';
            }else{
                echo 'No row affected';
            }
        }else{
            echo 'execute() error:';
            die($dbh->errorInfo());
        }
    }else{
        echo 'prepare() error:';
        die($dbh->errorInfo()); 
    }
}

Edit 编辑

One more thing, $priority + 1 seem a little weird. 再说一遍, $priority + 1似乎有点不可思议。

After your update I see this line: 更新后,我会看到以下行:

$priority = isset($_POST['priority']) ? $_POST['priority'] : 'low';

so you try to increment a string by 1? 所以您尝试将字符串增加1?

Anyway what happened to traditional debugging ? 无论如何,传统调试发生了什么?

$sql_debug = "UPDATE todolist 
              SET what = '$what', priority = '$priority', added_on = '$date' 
              WHERE id = $id";

echo "**************************************<br>";
echo $sql_debug."<br>";
echo "**************************************<br>";

error_log('sql = '.$sql_debug);

Take a look at the query 看一下查询

And run to see what happens 跑去看看会发生什么

I think update query is correct,please check the date time format,try this code 我认为更新查询是正确的,请检查日期时间格式,尝试使用此代码

 if (isset($_POST['moduleAction']) && ($_POST['moduleAction'] == 'edit')) {

            $date=date_create("2014-10-09");
            date_time_set($date,13,24,46);         
            $datetime =date_format($date,"Y-m-d H:i:s");                
            $stmt = $db->prepare('UPDATE todolist SET what = ?, priority = ?, added_on = ? WHERE id = ?');
            $stmt->execute(array($what, $priority + 1, $datetime, $id));

    }

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

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