dreamice2013 2019-04-18 12:46
浏览 30

如何在php中编辑和更新? [重复]

Statement

I would like to edit and update the record into MySQL server.

There are some records in MySQL server.

What I have tried so far

In the editing process (edit&update.php), it shows that there are some syntax errors as the followings:

  1. if (count($sel)==1) -> Warning: count(): Parameter must be an array or an object that implements Countable.
  2. Notice: Undefined index: category/url/datetime/lat/lng => Notice: Undefined index...

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['Edit']))
      {
          $id = $_GET['Edit'];
          $update = true;
          $sel = $conn->query("SELECT * FROM crimenews WHERE crimenews_id=$id");
          if (count($sel)==1)
          {
              $row = $sel->fetch_array();
              $category = $row['category'];
              $url = $row['url'];
              $datetime = $row['datetime'];
              $lat = $row['lat'];
              $lng = $row['lng'];
          }
      }
  if (isset($_POST['Update']))
      {
          $id = $_POST['id'];
          $stmt = $conn->prepare("UPDATE crimenews SET crime_cat='$category', crimenews_url='$url', crimenews_datetime='$datetime', crimenews_locationLat='$lat', crimenews_locationLong='$lng' WHERE crimenews_id=$id");
          $stmt->bind_param("i",$_POST['category'],$_POST['url'],$_POST['datetime'],$_POST['lat'],$_POST['lng'],$_SESSION['crimenews_id']);
          $stmt->execute();

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

          $stmt->close();
          header('location: front.php');
        }
?>

Expectation

I am able to edit and update the record(s) into MySQL server.

</div>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?