为什么单击更新按钮后查询没有更新? [英] Why the query didn't update after click at the update button?

查看:43
本文介绍了为什么单击更新按钮后查询没有更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我以前的文章(重复的文章-如何编辑&是否在php中更新?)

From my previous post (duplicated one - How to edit & update in php?)

问题

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

After I click at the update button (in front.php), the session message is shown but the query didn't update.

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

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

In edit&update.php, count() is not available after PHP 7.0+.

所以我改成is_countable(),但我不知道它与count()是否等效或兼容.

So I changed into is_countable() but I don't know it is an equivalent/compatible function to count() or not.

注释

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

For edit&update.php, I will change into the prepared statement later if the query is able to be updated after clicking at the update button.

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

There is no warning or notice while running the code but the query didn't update after clicking at the update button.

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');
        }

推荐答案

您不应尝试count调用 mysqli_num_rows 即更改

You shouldn't be trying to count the result of a call to mysqli_query at all (it's a mysqli_result object, not an array). Instead, to find out how many rows were returned by the query, check the value of mysqli_num_rows i.e. change

if (is_countable($query)==1)

if (mysqli_num_rows($query) == 1)

您还需要更改

$row = $sel->fetch_array();

$row = $query->fetch_array();

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

as $query is the name of your result object, not $sel.

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

Based on the results of your query, and the update code following, it seems what you really want is this:

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

这篇关于为什么单击更新按钮后查询没有更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆