【问题标题】:trying to update the records but failing in php尝试更新记录但在 php 中失败
【发布时间】:2019-04-02 11:49:05
【问题描述】:

我试图通过 $_GET 获取帖子的 id 来更新记录,然后通过 $_POST 更新记录。

我已经通过 $_GET 执行了删除操作,它也可以正常工作,mysqli_fetch_assoc 也可以很好地显示要编辑的记录,但实际编辑没有发生,它在空检查功能的代码验证中给出了空错误.

我已经进行了大量研究,但似乎无法解决这个错误,如果有人可以建议对代码进行任何更改,我将非常感谢。

提前谢谢你!

这是错误

Notice: Undefined index: id in /then the long url etc/

下面是代码

<?php
 //DB Connection
  include'include/db-conn.php';
  if (isset($_POST['edit'])) {
     //Raw GET Inputs
    $raw_c_id       = $_GET['id'];

    //Cleaned Inputs
    $c_c_id         = filter_var($raw_c_id, FILTER_SANITIZE_STRING);

    //Error Mwssages
    $empty      = '<div class="alert alert-danger alert-dismissible">
                <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                <strong>Error!</strong>Field is empty please provide content!
              </div>
              ';
    $success    = '<div class="alert alert-success alert-dismissible fixed-top">
                <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                <strong>Success!</strong> Content Added Successfully
              </div>
              ';
    $not_success  = '<div class="alert alert-danger alert-dismissible">
                    <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                    <strong>Not Success!</strong> Content Not Added Successfully
                  </div>
                  ';

      if (empty($c_c_id)) {
        echo $empty;
        exit();
        header("Location:index.php");
      }

      $update      = "UPDATE `continents` 
       SET `continent_name`='$c_c_name', `last_edited`='date(d/m/Y)'  
       WHERE `id`='$c_c_id'";
       $run_update  = mysqli_query($conn, $update);
      if (!$run_update) {
        header("Location: index.php");
        echo $not_success;

      }
      else{
        header("Location: index.php");
        echo $success;
      }
  }
 ?>

这是html部分

<div  class="panel-body">
            <form action="edit.php" method="POST">
                <div class="form-group">
                  <label for="continent_name">Continent Name</label>
                  <input required type="text" placeholder="10" class="form-control" value="<?php echo  $c_name ; ?>" name="continent_name">
                </div>
              <small>Date Added: <?php echo  $c_dated_added ; ?></small> / <small>Last Edited: <?php echo  $c_last_edited ; ?></small>
                <div class="form-group">
                  <input class="form-control btn-success" type="submit" name="edit" value="Submit">
                </div>
              </form>
          </div>

使用 while 循环

<div class="table-responsive">
<table id="example" class="table table-hover ">
    <thead>
      <tr class="">
        <th>ID</th>
        <th>Continent Name</th>
        <th>Date Added</th>
        <th>Status</th>
        <th>Edit</th>
        <th>Delete</th>
      </tr>
    </thead>
    <tbody>
        <?php 
            $all_continents     =   "SELECT * FROM `continents` ORDER BY `status`";

            $run                    =   mysqli_query($conn,$all_continents);

            while ($row_result = mysqli_fetch_assoc($run)) {
                $id =   $row_result['id'];
                $c_continent_name = $row_result['continent_name'];
                $c_date_added = $row_result['date_added'];
                $c_status = $row_result['status'];

                echo "
                <tr>
                    <td>$id</td>
                    <td>$c_continent_name</td>
                    <td>$c_date_added</td>
                    <td>$c_status</td>
                    <td>
                        <a class='btn btn-info' href='edit.php?id=$id'>Edit</a>
                    </td>
                    <td>
                        <a class='btn btn-danger'  href='delete.php?id=$id'>Delete</a>
                    </td>
                </tr>
                ";
            }
         ?>
    </tbody>
</table>

【问题讨论】:

  • 你能发布它发布到的 URL。我想查看 GET 参数
  • 显示您的实际网址。

标签: php post mysqli phpmyadmin get


【解决方案1】:

您似乎正在尝试同时使用 GET 和 POST 参数。它不起作用的原因是提交表单时丢失了 GET 参数。您需要在表单的 action 属性中传递它:

<form action="edit.php?id=<?php echo $_GET['id'] ?>" method="POST">

还请查看这篇文章中的建议: Is there a way to use GET and POST together?

【讨论】:

  • 非常感谢您的帮助。有没有机会从我这里查看这段代码,让我知道我哪里弄错了! stackoverflow.com/questions/53023485/…
  • 如果此答案解决了您的问题,请将此答案标记为正确。我会看看你的其他帖子。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-11
  • 2014-11-26
  • 1970-01-01
  • 2014-04-20
  • 2014-07-14
  • 1970-01-01
相关资源
最近更新 更多