php:更新行时出现问题

a5g8bdjr  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(387)

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

我在另一个页面中点击edit按钮,然后用户被引导到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中的字符串偏移量“word”非法。php:0 a">
变量转储结果:

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)
gmol1639

gmol16391#

您正在使用 $word['word'] 在html部分。但您尚未指定变量 $word['word'] . 你只有 $word .
而不是使用 $word['example'] 使用 $example . 其他变量也一样。
编辑:
有关代码的信息不足。但是你可以在你的输入标签上这样做 ((isset($word['word']))?$word['word']:''); . 这不是一个好的解决办法。但是你必须学会调试你的代码。

相关问题