php - 使用 PHP 和 MySQL 编辑表单 : Best way to obtain an ID

标签 php mysql sql forms

我有一个 MYSQL 表,每行都有编辑和删除链接。编辑链接转到 edit_ Patient.php,其中有一个表单(实际上,最初用于将患者插入数据库的表单的副本)。经过几次尝试,该脚本正在工作,尽管我猜它可以改进(事实上,当我提交编辑时,我收到“未定义索引:id”的通知。ID 通过 GET 过程传递到 edit_patent.php 文件。相关代码如下:

// 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();
}

并且,经过一些清理并检查提交的值后:

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:

这里,我不明白的关键行是 WHERE dem_id='".$_GET['id']."'"; --> 如果我保持原样,脚本运行几乎可以,但随后我收到未定义索引 id 的通知。

但是,当我替换为 WHERE dem_id=$id"; 时与第一个查询一样,脚本给出了 fatal error undefined variable: id

最后,为了提交表单,我使用以下命令: "/> 工作正常,但当我使用时它不起作用: ”/>

任何人都可以帮助我理解为什么以及如何解决这个问题,我宁愿能够简单地使用 $id (我相信是直接和简单的)但由于某种原因没有按预期工作。最后,我希望能够在要编辑的表单中报告使用单选按钮和下拉(选择)菜单插入的数据。任何有关这方面的建议将不胜感激!

最佳答案

请确保您的具体记录在您的 edit_patent.php 中的提交按钮后已更新?如果有效,下一页后将显示哪个页面..?是不是Display.php(即所有记录显示页面)?请先具体说明,我真的会帮助您解决您的疑问。

关于php - 使用 PHP 和 MySQL 编辑表单 : Best way to obtain an ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21218823/

相关文章:

mysql - SQL:具有特定值的一行中有多少行

php - 如何在每个类别的加载页面中仅获取 2 个产品

php - 无法根据 SQL 输出创建单独的论坛类别

mysql - EclipseLink JPA 持久化时重复条目

Python:如何将这 2 个重载构造函数合并为 1 个构造函数

sql - 将 DELETE FROM 与 OUTPUT 结合使用时如何指定 ORDER BY?

php - 处理 php(展开)

php - 可排序 PHP MYSQL

PHP/MySQL/JSON - 遍历 JSON 响应的所有页面

mysql - 将数据从我的数据库传输到我 friend 的数据库