使用mysql update语句中链接的值

时间:2015-12-09 19:19:49

标签: php mysql

我一直在寻找相关问题的答案,但似乎我的情况是独一无二的。我尝试使用更新语句中的链接传递的值。能够使用链接中传递的值填充我的更新表单,但即使我设置了所有变量,更新也会失败。

这是返回值

的链接
<a href="edit_student.php?student=<?php echo urldecode($student["student_id"]) ?>"><span class="glyphicon glyphicon-pencil"></span></a>

这是我的功能

function find_student_by_id($student_id) {
    global $connection;

    $query = "select * from students where student_id = {$student_id} limit 1";
    $found_student = mysqli_query($connection, $query);
    confirm_query($found_student);
    return $found_student;
}

这是我的更新代码

<?php 
// Checking for a set employee ID
if (isset($_GET["student"])) {
    $selected_student = find_student_by_id($_GET["student"]);
}

// Processing the form
if (isset($_POST['submit'])) {
    # process the form

    $student_id = $_GET["student"];
    $fname = ucfirst($_POST["s_fname"]);
    $lname = ucfirst($_POST["s_lname"]);
    $mname = ucfirst($_POST["s_mname"]);
    $sex = $_POST["sex"];
    $dob = $_POST["s_dob"];
    $home_address = ucwords($_POST["home_address"]);
    $guardian = ucwords($_POST["guardian"]);
    $contact_address = ucwords($_POST["contact_address"]);
    $phone = $_POST["g_phone"];
    $email = $_POST["g_email"];
    $year = $_POST["entry_year"];

    $query  = "update students set s_fname = '{$fname}', s_lname = '{$lname}', s_mname =  '{$mname}', sex = '{$sex}', s_dob = {$dob}, home_address = '{$home_address}', guardian = '{$guardian}', contact_address = '{$contact_address}', g_phone = {$phone}, g_email = '{$email}', entry_year = {$year}) where student_id = {$student_id}";

    $result = mysqli_query($connection, $query);

    if ($result) { 
        # successful
        $_SESSION["message"] = "Updated student information successfully.";
        if ($year == 0) {
            redirect_to("preschool.php");
        } elseif ($year == 1) {
            redirect_to("year1.php");
        } elseif ($year == 2) {
            redirect_to("year2.php");
        } elseif ($year == 3) {
            redirect_to("year3.php");
        } elseif ($year == 4) {
            redirect_to("year4.php");
        } elseif ($year == 5) {
            redirect_to("year5.php");
        } elseif ($year == 6) {
            redirect_to("year6.php");
        }
    } else {
        # failure
        $_SESSION["message"] = "Update unsuccessful.";
        redirect_to("edit_student.php");
    }
}

&GT;

当我提交表单时,我在附件error message

中收到错误

我已将selected_student设置在我的代码顶部,但我仍然得到未定义的变量警告。我如何解决这个问题,我已经在这几个小时了。感谢任何帮助

0 个答案:

没有答案