我正在尝试向当前显示MYSQL表内容的表添加编辑功能。
我可以添加和删除记录,但我似乎无法编辑它们。
以下是我的代码。谁能告诉我我做错了什么?
//Editing a record
$name=$_POST['editc'];
$sql="UPDATE course SET name='$name' WHERE id='$id'";
$result=mysql_query($sql);
// if successfully updated.
if($result)
{
echo "Successful";
echo "<BR>";
}
else
{
echo "ERROR";
}
echo'
<form action"'.$_SERVER['PHP_SELF'].'" method="POST">
<table border="1" width="80%" align="center">
<tr>
<td>
<h4>Record ID</h4>
</td>
<td>
<h4>Record Name</h4>
</td>
<td>
<h4>Add/Edit Record</h4>
</td>
<td>
<h4>Remove Selected Record(s)</h4>
</td>
</tr>
<tr>
<td>
<h5>Create New Record</h5>
</td>
<td>
<input type="text" name="cname" value="Type New Record Name Here" size="50"/>
</td>
<td>
<input type="Submit" name="Add" value="Add Record"/>
</td>
<td>
<h5> Select items you wish to remove, then click "Delete Selected" </h5>
</td>
</tr>
';
while ($row=mysql_fetch_array($query))
{
echo'<tr>
<td>
'.$row['cid'].'
</td>
<td>
'.$row['cname'].'
</td>
<td align="center">
<input type="text" name="editc" value="Modify record Name">
<input type="submit" name="cedit" value="Edit" />
</td>
<td align="center">
<input type="checkbox" name="cid[]" value="'.$row['cid'].'"/>
</td>
</tr>';
}
mysql_free_result($query);
echo '
</table>
<br/>
<br/>
<div align = "center">
<input type="submit" name="deletesel" value="Delete Selected"/>
---
<input type="reset" name="unselect" value="Deselect All"/>
</div>
</form>
';
答案 0 :(得分:2)
您忘记初始化$id
$id=intval($_POST["cid"][0]);
$sql="UPDATE course SET name='$name' WHERE id='$id'";
在你被迫在紧急情况下这样做之前离开mysql_*
,当然还要消毒$name
答案 1 :(得分:0)
感谢您提供的所有意见和建议,非常感谢。
我现在设法解决了我的问题!!
请在下面找到新代码:(如果其他人在将来遇到同样的问题......)
//Editing a record
if (isset($_POST['cedit']))
{
$UpdateQuery = "UPDATE tablename SET columnname='$_POST[cname]' WHERE cname='$_POST[hidden]'";
mysql_query($UpdateQuery, $connect);
};
echo'
<form action"'.$_SERVER['PHP_SELF'].'" method="POST">
<table border="1" width="80%" align="center">
<tr>
<td>
<h4>Record ID</h4>
</td>
<td>
<h4>Record Name</h4>
</td>
<td>
<h4>Add/Edit Record</h4>
</td>
<td>
<h4>Remove Selected Record(s)</h4>
</td>
</tr>
<tr>
<td>
<h5>Create New Record</h5>
</td>
<td>
<input type="text" name="cname" value="Type New Record Name Here" size="50"/>
</td>
<td>
<input type="Submit" name="Add" value="Add Record"/>
</td>
<td>
<h5> Select items you wish to remove, then click "Delete Selected" </h5>
</td>
</tr>
';
while ($row=mysql_fetch_array($query))
{
echo'<tr>
<td>
'.$row['cid'].'
</td>
<td>
<form action="course_man.php" method="post">
<input type=text name=cname value="'.$row['cname'].'"/>
<input type=hidden name=hidden value="'.$row['cname'].'"/>
</td>
<td align="center">
<input type="submit" name="cedit" value="Edit " />
</form>
</td>
<td align="center">
<input type="checkbox" name="cid[]" value="'.$row['cid'].'"/>
</td>
</tr>';
}
mysql_free_result($query);
echo '
</table>
<br/>
<br/>
<div align = "center">
<input type="submit" name="deletesel" value="Delete Selected"/>
---
<input type="reset" name="unselect" value="Deselect All"/>
</div>
</form>
';