我在更新数据库行时遇到问题。 插入很好,但是当我尝试编辑行时,发生了一些奇怪的事情:
我在另一页中单击了“编辑”按钮,然后将用户定向到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>
和错误(其中之一):
警告:第55行的C:\ wamp64 \ www \ EnglishProject \ admin \ addnew.php中的字符串偏移量'word'非法调用堆栈#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)
答案 0 :(得分:0)
您正在HTML部分中使用$word['word']
。但是您尚未分配变量$word['word']
。您只有$word
。
使用$word['example']
代替使用$example
。其他变量也一样。
编辑:
关于您的代码的信息不足。但是您可以在输入标签((isset($word['word']))?$word['word']:'');
上尝试这样。这不是一个好的解决方案。但是您必须学习调试代码。