为什么单击更新按钮后查询没有更新?

时间:2019-04-19 02:01:12

标签: php mysql

从我之前的帖子(重复的帖子-How to edit & update in php?

问题

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

到目前为止我尝试过的事情

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>

edit&update.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');
        }

1 个答案:

答案 0 :(得分:1)

您根本不应该尝试count调用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();

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

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

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