使用PHP和MySQL编辑表单:获取ID的最佳方式
我有一个MYSQL表,每行都有编辑和删除链接。编辑链接转到edit_patient.php,它有一个表单(实际上是最初用于将患者插入数据库的表单的副本)。经过几次尝试后,脚本仍在运行,尽管我认为它可以改进(事实上,我在提交编辑时收到了“Undefined index:id”的通知。id通过get过程传递到edit_patient.php文件。相关代码如下:使用PHP和MySQL编辑表单:获取ID的最佳方式,php,mysql,sql,forms,Php,Mysql,Sql,Forms,我有一个MYSQL表,每行都有编辑和删除链接。编辑链接转到edit_patient.php,它有一个表单(实际上是最初用于将患者插入数据库的表单的副本)。经过几次尝试后,脚本仍在运行,尽管我认为它可以改进(事实上,我在提交编辑时收到了“Undefined index:id”的通知。id通过get过程传递到edit_patient.php文件。相关代码如下: // Check for a valid user ID, through GET or POST: if ( (isset($_GET['
// Check for a valid user ID, through GET or POST:
if ( (isset($_GET['id'])) && (is_numeric($_GET['id'])) ) { // From view_patient.php
$id = $_GET['id'];
} elseif ( (isset($_POST['id'])) && (is_numeric($_POST['id'])) ) { // Form submission.
$id = $_POST['id'];
} else { // No valid ID, kill the script.
echo '<p>Sorry, it is not possible to update patient info at this time</p>';
include ('../elements/layouts/footer.php');
exit();
}
在这里,我不理解的关键行是其中dem_id='“$\u GET['id']。””;
-->如果我保持原样,脚本运行几乎正常,但随后会收到未定义索引id的通知
但是,当我在第一个查询中替换为WHERE dem_id=$id”
时,脚本给出了一个致命错误未定义变量:id
最后,要提交表单,我使用以下命令:
“/>正常工作,但当我使用:
“/>
有人能帮我理解为什么,以及如何纠正这个问题吗?我更希望能够简单地使用
$id
(我认为这很简单),但由于某些原因,它不能按预期工作。最后,我希望能够以要编辑的形式报告数据,并使用单选按钮和下拉列表(选择)插入数据菜单。如果您对此有任何建议,我们将不胜感激!请确保您的特定记录已在edit_patient.php中的submit按钮后更新?如果它有效,下一页显示哪个页面?是否为display.php(即所有记录显示页面)?请先指定,我真的帮助您解决您的查询。Hey user321,谢谢您的及时回复!是的,我确认数据库中的记录更新得很好。这是在我使用$\u GET['id']构造时发生的。另一方面,当我使用dem\u id=$id(如我所愿)时,它不起作用(显然,$id未定义)…非常感谢您的支持!woops差点忘了说:当记录更新时,用户会收到一条“迄今为止一切顺利”的消息,现在就到此为止…感谢您编辑Agnes,非常感谢!
if($action['result'] != 'error'){
// Make the query:
$q = "UPDATE `demographics`
SET lastname='$lastname', firstname='$firstname', clinic='$clinic', sex='$sex', dob='$dob', age='$age',
disease_1='$disease_1', disease_2='$disease_2', disease_3='$disease_3', address='$address', city='$city', country='$country',
zip='$zip', phone_1='$phone_1', phone_2='$phone_2', phone_3='$phone_3', email_1='$email_1', email_2='$email_2',
physician='$physician', notes='$notes'
WHERE dem_id=$id
LIMIT 1";
$r = @mysqli_query ($db_connect, $q);
if (mysqli_affected_rows($db_connect) == 1) { // If it ran OK.
// Tell the user we have edited patient data successfully
$action['result'] = 'success';
array_push($text,'Patient data have been updated on databank');
}else{
$action['result'] = 'error';
array_push($text,'Patient data could not be changed on databank. Reason: ' .
'<p>' . mysqli_error($db_connect) . '<br /><br />Query: ' . $r . '</p>');
} // End of if (empty($errors)) IF.
} // End of if (empty rows))
// Retrieve the user's information:
$q = "SELECT lastname, firstname, clinic, sex, dob, age, disease_1, disease_2, disease_3, address, city, country, zip, phone_1,
phone_2, phone_3, email_1, email_2, physician, notes
FROM `demographics`
WHERE dem_id='".$_GET['id']."'";
$r = @mysqli_query ($db_connect, $q);
if (mysqli_num_rows($r) == 1) { // Valid user ID, show the form.
// Get the user's information:
$row = mysqli_fetch_assoc ($r);
// Create the form: