我无法让我的update
复选框功能工作。我需要能够删除或添加我选择调用checked
到我的表的值。代码如下所示。
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Name <input type"text" name="inputName" value="<?php echo $hemsida['Namn']; ?>" /> </br>
Commentar <input type"text" name="inputComment" value="<?php echo $hemsida['Comment']; ?>" />
<br/>
</br><input type="checkbox" name="all" value="<?php echo $hemsida['All']; ?>"
<?php if($hemsida['All'] == 'checked') echo " checked"; ?> /> Alla
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" />
<input type="submit" name="submit" value="Redigera">
</form>
并且更新 PHP 看起来像这样
if(isset($_POST['submit'])) {
$all = ($_POST['All'] == 1) ? "checked" : "";
$u = "UPDATE hemsida SET `Namn`='$_POST[inputName]', `Comment`='$_POST[inputComment]', `ALL`=$all WHERE ID = $_POST[id]";
mysql_query($u) or die(mysql_error());
echo "User has been modified";
header("Location: ..//sokh.php");
}
错误出现Undefined variable: hemsida
在所有部分。还有You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE ID = 33' at line 1
. 但是我没有问题将数据输入Modifier
或我应该称之为但无法将其取出。
回答 !!!
我让它工作但无法回答我自己的问题,所以我在这里写下来,我添加和删除了代码,直到一切都崩溃了。删除“$all = ($_POST['All'] == 1) ?checked : ;” 部分,现在它可以工作了。我将复制它下面的代码有兴趣
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Name <input type"text" name="inputName" value="<?php echo $hemsida['Namn']; ?>" /> </br>
Commentar <input type"text" name="inputComment" value="<?php echo $hemsida['Comment']; ?>" />
<br/>
<input type="checkbox" name="all" value="checked" <?php if($hemsida['All'] == 'checked') echo "checked=\"checked\""; ?>/> Alla
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" />
<input type="submit" name="submit" value="Redigera">
</form>
新的 php
if(isset($_POST['submit'])) {
$u = "UPDATE hemsida SET `Comment`='$_POST[inputComment]', `Namn`='$_POST[inputName]', `All`='$_POST[all]' WHERE ID = $_POST[id]";
mysql_query($u) or die(mysql_error());
echo "User has been modified";
header("Location: ..//sokh.php");
}