如何在php中编辑和更新?

时间:2019-04-18 12:46:32

标签: php mysql

声明

我想将记录编辑并更新到MySQL服务器中。

MySQL服务器中有一些记录。

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

在编辑过程(edit&update.php)中,显示出一些语法错误,如下所示:

  1. if (count($sel)==1)->警告:count():参数必须是实现Countable的数组或对象。
  2. Notice: Undefined index: category/url/datetime/lat/lng =>注意:未定义的索引...

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

期望

我能够编辑记录并将其更新到MySQL服务器中。

0 个答案:

没有答案