【问题标题】:PHP: trouble updating rowsPHP:更新行时遇到问题
【发布时间】:2018-07-20 07:15:11
【问题描述】:

我在更新数据库行时遇到问题。 插入很好,但是当我试图编辑我的行时,会发生一些奇怪的事情:

我在另一个页面中点击了编辑按钮,然后用户被定向到 addnew.php 页面并且所有输入都填充了我们要编辑的行的数据。当我点击更新按钮时,就会出现这个错误。

这是我的 addnew.php 代码:

<?php
  session_start();
  if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] == false) {
    header('location: login');
  }
  require_once '../core/init.php';
  require_once '../helpers/helper.php';
  include 'includes/header.php';
  $sqlAdd = "";
  //Edit
  if (isset($_GET['edit'])) {
    $edit_id = (int)$_GET['edit'];
    $sql = "SELECT * FROM words WHERE id = '$edit_id'";
    $getWord = $db->query($sql);
    $word = mysqli_fetch_assoc($getWord);

  }
  //Add
  if (isset($_POST['submit'])) {
    $word = mysqli_real_escape_string($db, sanitize($_POST['word']));
    $phonetic = mysqli_real_escape_string($db, sanitize($_POST['phonetic']));
    $meaning = mysqli_real_escape_string($db, sanitize($_POST['meaning']));
    $engMeaning = mysqli_real_escape_string($db, sanitize($_POST['engMeaning']));
    $example = mysqli_real_escape_string($db, sanitize($_POST['example']));
    $eMeaning = mysqli_real_escape_string($db, sanitize($_POST['eMeaning']));
    if ($word == '' ||
        $phonetic == '' ||
        $meaning == '' ||
        $engMeaning == '' ||
        $example == '' ||
        $eMeaning == ''
        ) {
          echo '<p style="text-align:center;
                          background-color: #E74C3C;
                          margin-top:20px;
                          color:white;
          ">All forms must be filled</p>';
    }else {
      if (isset($_GET['edit'])) {
        $sqlAdd = "UPDATE words SET word = '$word', phonetic = '$phonetic', meaning = '$meaning', engMeaning = '$engMeaning',
         example = '$example', exampleMeaning = '$eMeaning' WHERE id = '$edit_id'";
      }else {
        $sqlAdd = "INSERT INTO words (word,   meaning,   engMeaning,  example,    exampleMeaning,phonetic)
                             VALUES ('$word','$meaning','$engMeaning','$example','$eMeaning',   '$phonetic')";
      }
echo $sqlAdd;
  // $db->query($sqlAdd);
    }
  }
?>

<link rel="stylesheet" href="../styles/admin-style.css">
<title>Add new Words</title>
<div class="container2">

  <?= var_dump($word); ?>
  <form autocomplete="off"  method="post">

    <?= var_dump($word); ?>
    <input type="text" name="word" placeholder="Word" value="<?= ((isset($_GET['edit']))?$word['word']:''); ?>">
    <input type="text" name="phonetic" placeholder="Phonetic" value="<?= ((isset($_GET['edit']))?$word['phonetic']:''); ?>">
    <input type="text" name="meaning" placeholder="meaning" value="<?= ((isset($_GET['edit']))?$word['meaning']:''); ?>">
    <input type="text" name="engMeaning" placeholder="English Meaning" value="<?= ((isset($_GET['edit']))?$word['engMeaning']:''); ?>">
    <textarea type="text" name="example" placeholder="Example" rows="5"><?= ((isset($_GET['edit']))?$word['example']:''); ?></textarea>
    <textarea type="text" name="eMeaning" placeholder="example meaning" rows="5"><?= ((isset($_GET['edit']))?$word['exampleMeaning']:''); ?></textarea>
    <button type="submit" name="submit"><?= ((isset($_GET['edit']))?'Update':'Add'); ?></button>
  </form>
</div>

以及错误(其中之一):

警告:C:\wamp64\www\EnglishProject\admin\addnew.php 中第 55 行调用堆栈#TimeMemoryFunctionLocation 10.0002266584{main}( )...\addnew.php:0 a" >

var_dump 结果:

array (size=8)
  'id' => string '23' (length=2)
  'word' => string 'a1aaaaaa' (length=8)
  'meaning' => string 'sssaaaa' (length=7)
  'engMeaning' => string 'assssssssa' (length=10)
  'example' => string 'aaaaaaaaaaaaaaas' (length=16)
  'exampleMeaning' => string 'saaaaaaaaaaaa' (length=13)
  'phonetic' => string 'aaasss' (length=6)
  'views' => string '0' (length=1)

【问题讨论】:

  • 您如何获得$edit_id 的价值?注释$db-&gt;query($sqlAdd); 行并使用echo $sqlAdd;。检查查询。你会有一个想法。
  • 我做到了,然后在 phpMyAdmin 中测试了 SQL 代码,它工作正常。当我在表单中执行此操作时会出现问题。 @SriramG
  • “我不明白为什么人们在没有答案的情况下投反对票。” - idownvotedbecau.se/noresearch, idownvotedbecau.se/noattempt 这不是关于“没有”答案,但关于你愿意付出多少努力。副本解释了错误的原因是什么 - 你明白了吗?如果是,请开始考虑现在需要做什么来修复这方面的代码。如果仍然没有 - 然后询问你对解释究竟什么不理解。
  • 如果通过提交相同的表单调用了 addnew.php 脚本,您将不可能获得此调试输出。然后$_POST['submit'] 将被设置,你在if 分支中做的第一件事就是用名为word 的表单字段的mysql 转义值覆盖$word。这只会产生您在之前首次提交此表单之前显示的调试输出。

标签: php html mysql forms crud


【解决方案1】:

您在 HTML 部分中使用了$word['word']。但是您还没有分配变量$word['word']。你只有$word

不要使用$word['example'],而是使用$example。其他变量也一样。

编辑:

关于您的代码的信息不足。但是您可以在输入标签((isset($word['word']))?$word['word']:''); 上尝试这样的操作。这不是一个好的解决方案。但是你必须学会​​调试你的代码。

【讨论】:

  • $word = mysqli_fetch_assoc($getWord); $word 已分配。这是一个保存我的数据库的数组。
  • 第 55 到 58 行是什么?
  • 我的 标签由 DB 提供。
  • 通过'print_r($word)'检查你的结果
  • 它正确打印所有数据库行项目。当我点击更新按钮时发生错误。
猜你喜欢
  • 2019-07-22
  • 2020-11-29
  • 2020-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-07
  • 2021-04-05
  • 1970-01-01
相关资源
最近更新 更多