php - 为什么点击更新按钮后查询没有更新?

标签 php mysql

来 self 的上一篇文章(重复的一篇 - How to edit & update in php? )

问题

单击更新按钮(在 front.php 中)后,会显示 session 消息,但查询未更新。

到目前为止我已经尝试过

edit&update.php 中,count() 在 PHP 7.0+ 之后不可用。

所以我改为 is_countable() 但我不知道它是否与 count() 等效/兼容。

注释

对于 edit&update.php,如果单击更新按钮后查询能够更新,我将稍后更改为准备好的语句。

运行代码时没有警告或通知,但单击更新按钮后查询没有更新。

front.php

<html>
<?php 
require_once 'edit&update.php';
include 'connection.php';
?>
<body>
<!--ID (hidden)-->
<input type="hidden" name="id" value="<?php echo $id; ?>"><!--ID-->
<!--Category-->
<label>Category</label>:
<select name="category" value="<?php echo $category; ?>">
       <option value="0">(Please any type below)</option>
       <option value="A">A</option>
       <option value="D">D</option>
       <option value="M">M</option>
       <option value="S">S</option>
       <option value="T">T</option>
       <option value="Tr">Tr</option>
       <option value="Other">Other</option>
</select>
<!--URL-->
<label>URL</label>:
<input type="url" name="url" value="<?php echo $url; ?>" placeholder="URL (www.)" required>
<!--Issued date-->
<label>Issued date:</label><br>
<input type="datetime-local" name="datetime" value="<?php echo $datetime; ?>" required>
<!--Latitude-->
<label>Latitude</label>:
<input type="text" value="<?php echo $lat; ?>" name="lat" placeholder="Latitude">
<!--Longitude-->
<label>Longitude</label>:&nbsp
<input type="text" value="<?php echo $lng; ?>" name="lng" placeholder="Longitude">

<div class="row" style="padding: 10px">
    <div class="col">
        <?php if($update == true):
        ?>
        <button type="submit" name="Update">Update</button>
        <?php else: ?>
        <button type="submit" name="Save">Save</button>
        <?php endif ?>
    </div>
    <div class="col">
        <button type="reset" value="Reset">Reset</button>
    </div>
</div>
</body>
</html>

编辑&更新.php

<?php
include 'connection.php';

      //Default
      $id=0;
      $update = false;
      $category = '';
      $url = '';
      $datetime = '';
      $lat = '';
      $lng = '';

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'];
          }
      }
  if (isset($_POST['Update']))
      {
          $id = $_POST['id'];
          $category = mysqli_real_escape_string($conn,$_POST['category']);
          $url = mysqli_real_escape_string($conn,$_POST['url']);
          $datetime = mysqli_real_escape_string($conn,$_POST['datetime']);
          $lat = mysqli_real_escape_string($conn,$_POST['lat']);
          $lng = mysqli_real_escape_string($conn,$_POST['lng']);

          $conn->query("UPDATE crimenews SET crimenews_cat='$category', crimenews_url='$url', crimenews_datetime='$datetime', crimenews_locationLat='$lat', crimenews_locationLong='$lng' WHERE crimenews_id=$id");

          $_SESSION['message'] = "This news has updated";
          $_SESSION['msg_type'] = "warning";

          header('location: front.php');
        }

最佳答案

您不应该尝试计算 mysqli_query 调用的结果根本没有(它是一个mysqli_result对象,而不是一个数组)。相反,要了解查询返回了多少行,请检查 mysqli_num_rows 的值。即改变

if (is_countable($query)==1)

if (mysqli_num_rows($query) == 1)

你还需要改变

$row = $sel->fetch_array();

$row = $query->fetch_array();

as $query 是结果对象的名称,而不是 $sel

根据您的查询结果以及下面的更新代码,您真正想要的是:

$category = $row['crime_cat'];
$url = $row['crimenews_url'];
$datetime = $row['crimenews_datetime'];
$lat = $row['crimenews_locationLat'];
$lng = $row['crimenews_locationLong'];

关于php - 为什么点击更新按钮后查询没有更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55755679/

相关文章:

mysql - 如何使用 Sequel Pro 连接到在 docker 上运行的 mysql

php - 登录 Kohana 时出现错误消息

php - 如何在 PHP 中运行存储在 .text 文件中的一系列 SQL 语句?

PHP 列出表中的数组项 3 行

mysql - Rails 迁移——暂时忽略外键约束?

mysql - Pyrocms : blank page on database load

php - 使用 PHP 将 shapefile 存储到 postgresql

php - FTP::connection() 随机失败

Mysql撤销权限不影响现有连接

java - 如何使用 MyBatis 更新数据库?