我无法使用PDO程序更新数据中的状态行

时间:2017-02-17 12:35:52

标签: php mysql pdo

我在使用PDO程序在mysql表中更新状态行时遇到了麻烦。状态行在表中被定义为0,因此我希望每当我点击确认按钮时它都会更改为1,但它不能并且我一直试图整夜修复它但我不能。

这是我的数据库连接

<?php
class Database
{   
    private $host = "localhost";
    private $db_name = "jerrydb";
    private $username = "root";
    private $password = "";
    public $conn;





    public function dbConnection()
    {

        $this->conn = null;    
        try
        {
            $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
            $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);   
        }
        catch(PDOException $exception)
        {
            echo "Connection error: " . $exception->getMessage();
        }

        return $this->conn;
    }
}

?>

这是我的脚本,我希望每当我点击它并同时将状态行更改为1时确认就会消失。

<?php


if (isset($_GET['entity']) && isset ($_GET['action']) && isset($_GET['user_id'])){

  $entity = mysqli_real_escape_string($db, $_GET['entity']);
   $action = mysqli_real_escape_string($db, $_GET['action']);
    $user_id = mysqli_real_escape_string($db, $_GET['user_id']);



$query = "UPDATE jerrydb set status = '1' WHERE user_id='$user_id'";

    $db->query($query);
}




$query = "SELECT * FROM jerrydb WHERE status='0' ORDER BY user_id DESC";

$jerrydb = $db->query($query);

?>

 <?php if($row = $jerrydb->fetch_assoc())  { ?>


                  <tr> 
                  <td><a href="index.php?entity=go&action=approve&id=<?php echo $row['id'];?>" class="btn btn-success">Confirm</a> </td>



                </tr>

                  <?php } ?>

1 个答案:

答案 0 :(得分:0)

您实际上正在使用PDO,因此您无需使用这些mysqli_real_escape_string当您使用PDO时,最好准备bind,执行语句。

<?php
if (isset($_GET['entity']) && isset($_GET['action']) && isset($_GET['user_id'])) {
    $entity  = $_GET['entity'];
    $action  = $_GET['action'];
    $user_id = $_GET['user_id'];
    $query   = $conn->prepare("UPDATE jerrydb SET status = '1' where user_id = ? ");
    if ($query->execute([$user_id])) {
        echo "status updated";
    } else {
        echo "not";
    }
}
$query = $conn->prepare("SELECT * FROM jerrydb WHERE status='0' ORDER BY user_id DESC");
$query->execute();
$jerrydb = $query->fetchall(PDO::FETCH_ASSOC);
if (count($jerrydb) > 0) { // we have results
    foreach ($jerrydb as $key => $row) {
    }
?>

             <tr> 
                  <td><a href="index.php?entity=go&action=approve&id=<?php
    echo $row['id'];
?>" class="btn btn-success">Confirm</a> </td>



                </tr>

<?php
}
?>

要了解有关PDO的更多信息,请访问此网站,它会很好地解释并使一切变得简单明了https://phpdelusions.net/pdo