如何将这部分代码转换为PHP中的预处理语句?

问题描述 投票:1回答:1

从我以前的帖子(Why the query didn't update after click at the update button?

声明

如何将部分代码转换为准备好的语句?

edit.php

if (isset($_GET['id']))
      {
          $id = $_GET['id'];
          $update = true;
          $sql = "SELECT * FROM crimenews WHERE crimenews_id=$id";
          $query = mysqli_query($conn,$sql);

        if(mysqli_num_rows($query) == 1)
          {
              $row = mysqli_fetch_array($query);
              $category = $row['crimenews_cat'];
              $url = $row['crimenews_url'];
              $datetime = $row['crimenews_datetime'];
              $lat = $row['crimenews_locationLat'];
              $lng = $row['crimenews_locationLong'];
          }
      }

上面给出的链接中已经提供了更多代码。

php mysql prepared-statement
1个回答
1
投票
// while testing add
ini_set('display_errors', 1); 
ini_set('log_errors',1); 
error_reporting(E_ALL); 
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

if (isset($_GET['id']))
{
    $update = true;
    $sql = "SELECT * FROM crimenews WHERE crimenews_id=?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param('i', $_GET['id']);
    $stmt->execute();

    $res = $stmt->get_result();

    if($res->num_rows == 1)
    {
        // fetch_assoc() returns just an assoc array and not the numeric array as well
        $row = $res->fetch_assoc();

        $category = $row['crimenews_cat'];
        $url = $row['crimenews_url'];
        $datetime = $row['crimenews_datetime'];
        $lat = $row['crimenews_locationLat'];
        $lng = $row['crimenews_locationLong'];
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.