我的代码中出现错误,基本上我的用户输入没有被读取,我不知道为什么。我使用了isset并且它跳过了,这意味着用户输入没有被正确读取?任何想法为什么不呢?当我提交表单时,我会收到另一个错误,其中包含'注意:未定义的变量:绑定在C:\ xampp \ htdocs \ editartist.php' 但我认为这是事实,当你搜索你不再有id。我已经留下了一些代码来展示。
<?php
if(isset($_GET['id']))
{
$artistID = fix_string($_GET['id']);
$sql = ("SELECT artName FROM artist WHERE artID = '$artistID' ");
$result = $conn->prepare($sql);
$result->execute();
$result->bind_result($bound);
$result->fetch();
}
else {
echo "this is broken";
}
?>
<form method="post" action="editartist.php"/>
<?php echo '<input type="text" name="artistname" value= "'.$bound.'">' ?>
<input type="submit" value="Save"/>
</form>
<?php
if(isset($_GET['id'])) {
if(isset($_GET['artistname'])) {
$userinput = $_GET['artistname'];
echo "$userinput $artistID";
$sqltwo = ("UPDATE artist SET artName='$userinput' WHERE artID='$artistID'");
$stmt = $conn->prepare($sqltwo);
$stmt->execute();
} else {
echo "this is broken 2";
}
}
?>
这不是重复,它没有询问第二个错误。主要问题是为什么用户输入没有被读取?
答案 0 :(得分:0)
因为您在html表单中使用POST方法, 而在php中正在等待GET方法
假设您在Get请求中发送ID,我重写了您的代码 如下
<?php
if(isset($_GET['id']))
{
$artistID = fix_string($_GET['id']);
$sql = ("SELECT artName FROM artist WHERE artID = '$artistID' ");
$result = $conn->prepare($sql);
$result->execute();
$result->bind_result($bound);
$result->fetch();
}
else {
echo "this is broken";
}
?>
<form method="post" action="editartist.php"/>
<?php echo '<input type="text" name="artistname" value= "'.$bound.'">' ?>
<input type="submit" value="Save"/>
</form>
<?php
//no need to check for the id here , assuming you are posting data now.and you //already checked at the top of script for $_GET['id']
if(isset($_POST['artistname'])) {
$userinput = $_POST['artistname'];
echo "$userinput $artistID";
$sqltwo = ("UPDATE artist SET artName='$userinput' WHERE artID='$artistID'");
$stmt = $conn->prepare($sqltwo);
$stmt->execute();
} else {
echo "this is broken 2";
}
?>
您需要考虑清理Sanitize filters
并正确绑定参数:)