gpt4 book ai didi

PHP:更新行时遇到问题

转载 作者:行者123 更新时间:2023-11-29 17:18:05 27 4
gpt4 key购买 nike

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

Image here

我点击另一个页面中的编辑按钮,然后用户被定向到 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)

最佳答案

您在 HTML 部分中使用了 $word['word']。但您尚未分配变量$word['word']。您只有 $word

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

编辑:

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

关于PHP:更新行时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51437081/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com