因此,我有一个PHP CRUD系统,可以在数据库中添加,编辑和删除条目。有问题的CMS是药物表。我可以使用添加按钮并填写表格将毒品添加到该表中,也可以通过简单地删除毒品来删除毒品。但是问题之一是系统的编辑/更新部分。
编辑功能本身可以正常工作,我能够编辑一个条目,它会发布到数据库中并显示在表格中,因为用户被重定向回显示所有药物的表格页面,但是,当我从字段中删除文本,使其为空,然后按update,我收到一条错误消息,提示文本字段为空,但是,我还注意到页面顶部的ID不再存在,这给了我一个Undefined索引:错误。
我将问题的范围缩小到仅是因为输入类型“ submit”出于某种原因将其删除了。
按下更新前的URL,文本字段为空:php_files / DrugEdit.php?Drug_ID = 23
按下更新后的URL,文本字段为空:eMAR / php_files / DrugEdit.php
程序的工作方式(带代码):
通过从URL获取Drug_ID创建的Php变量(在本例中为23)
$Drug_ID = $_GET['Drug_ID'];
然后从数据库获取字段数据并将其分配给变量
$result = mysqli_query($conn, "SELECT * FROM drugs WHERE Drug_ID=$Drug_ID");
while($res = mysqli_fetch_array($result))
{
$Drug_Name = $res['Drug_Name'];
$Allergies = $res['Allergies'];
$Side_effects = $res['Side_effects'];
$Type_of_Medication = $res['Type_of_Medication'];
$Dosage = $res['Dosage'];
}
?>
然后以可以编辑数据的形式显示数据
<form name="form1" method="post" action="DrugEdit.php">
<table border="0">
<tr>
<td>Drug_Name <?php echo $drug_name_error ?></td>
<td><input type="text" Name="Drug_Name" value="<?php echo $Drug_Name;?>"></td>
</tr>
<tr>
<td>Allergies</td>
<td><input type="text" Name="Allergies" value="<?php echo $Allergies;?>"></td>
</tr>
<tr>
<td>Side_effects</td>
<td><input type="text" Name="Side_effects" value="<?php echo $Side_effects;?>"></td>
</tr>
<tr>
<td>Type_of_Medication</td>
<td><input type="text" Name="Type_of_Medication" value="<?php echo $Type_of_Medication;?>"></td>
</tr>
<tr>
<td>Dosage</td>
<td><input type="text" Name="Dosage" value="<?php echo $Dosage;?>"></td>
</tr>
<tr>
<td><input type="hidden" Name="Drug_ID" value=<?php echo $_GET['Drug_ID'];?>></td>
<td><input type="submit" Name="update" value="Update"> </td>
</tr>
</table>
</form>
如果按下更新按钮,它将运行下面的代码块:
$drug_name_error = " ";
if(isset($_POST['update']))
{
$Drug_ID = $_POST['Drug_ID'];
$Drug_Name=$_POST['Drug_Name'];
$Allergies=$_POST['Allergies'];
$Side_effects=$_POST['Side_effects'];
$Type_of_Medication=$_POST['Type_of_Medication'];
$Dosage=$_POST['Dosage'];
// checking empty fields
if(empty($Drug_Name) || empty($Allergies) || empty($Side_effects) || empty($Type_of_Medication) || empty($Dosage)) {
if(empty($Drug_Name)) {
$drug_name_error = "<font color='red'>Drug_Name field is empty.</font><br/>";
}
if(empty($Allergies)) {
echo "<font color='red'>Allergies field is empty.</font><br/>";
}
if(empty($Side_effects)) {
echo "<font color='red'>Side_effects field is empty.</font><br/>";
}
if(empty($Type_of_Medication)) {
echo "<font color='red'>Type_of_Medication field is empty.</font><br/>";
}
if(empty($Dosage)) {
echo "<font color='red'>Dosage field is empty.</font><br/>";
}
} else {
//updating the table
$result = mysqli_query($conn, "UPDATE drugs SET Drug_Name='$Drug_Name' ,Allergies='$Allergies' ,Side_effects='$Side_effects' ,Type_of_Medication='$Type_of_Medication',Dosage='$Dosage' WHERE Drug_ID=$Drug_ID");
//redirecting to the display page. In our case, it is index.php
header("Location: ../drug_manager.php");
}
}
感谢您花时间阅读我的问题。希望我已经为您提供了足够的信息。
由于某种原因,输入类型“提交”将其删除
因为您正在提交表单,并且表单提交操作的URL在<form>
元素中定义:
<form name="form1" method="post" action="DrugEdit.php">
请注意,该URL上没有Drug_ID
值。但是,在表单处理代码中,您不是look在URL中寻找它,而是在表单发布中寻找它:
$Drug_ID = $_POST['Drug_ID'];
所以我不太确定您在哪里遇到该错误。但是,如果您在表单逻辑中的某处正在寻找$_GET['Drug_ID']
,或者只是希望将其放置在URL上以用于某些下游需求,则可以将其添加到该URL:<form name="form1" method="post" action="DrugEdit.php?Drug_ID=<?php echo $_GET['Drug_ID'];?>">