如何使用HTML表单方法POST从数据库传递id号

时间:2016-06-14 09:58:03

标签: php html

我有这个表单用于编辑或更新数据信息,但当我点击UPDATING查询按钮时,$id的值为0

这是我的代码:

<form action="edit_crew.php?id=$id" method="POST">
           <?php 
            while(mysqli_stmt_fetch($stmt1)) {
            echo "First Name: <input type=\"text\" style=\"border: none; border-color: transparent;\" value=\"$first_name\" name=\"first_name\"><br/>";
            echo "Middle Name: <input type=\"text\" style=\"border: none; border-color: transparent;\" value=\"$middle_name\"><br/>";
            echo "Last Name: <input type=\"text\" style=\"border: none; border-color: transparent;\" value=\"$last_name\"><br/>";
            echo "Age: <input type=\"text\" style=\"border: none; border-color: transparent;\" value=\"$age\"><br/>";
            echo "<button type=\"button\" class=\"btn btn-success\" onclick=\"document.location = 'edit_crew.php?id=$id'; \">Continue</button></center>";
          }
?>
</form>

以下是UPDATE查询的代码:

<?php

include '../session.php';
require_once 'config.php';


    include 'config.php';
    $query_update = "UPDATE `crew_info` SET `first_name` = ?, `middle_name` = ?, `last_name` = ?, `age` = ?, `month` = ?, `day` = ?, `year` = ?, `birth_place` = ?, `gender` = ?, `martial_status` = ?, `religion` = ?, `nationality` = ?, `email_address` = ?, `address_1` = ?, `address_2` = ?, `course` = ?, `school_graduated` = ?, `remarks` = ?  WHERE `id` = ?";
    $stmt2 = mysqli_prepare($conn, $query_update);
    $first_name = $_POST['first_name'];
    mysqli_stmt_bind_param($stmt2, 'sssisiisssssssssssi', $first_name, $middle_name, $last_name, $age, $month, $day, $year, $birth_place, $gender, $martial_status, $religion, $nationality, $email_address, $address_1, $address_2, $course, $school_graduated, $remarks, $_GET['id']);
    mysqli_stmt_execute($stmt2);

?>

我猜查询是正确的唯一问题是id为空

以下是我如何得到$id我正在使用mysqli prepare

<?php

include '../session.php';
require_once 'config.php';
include 'header.php';

  $query1 = "SELECT * FROM `crew_info` WHERE `id` = ?";
  $stmt1 = mysqli_prepare($conn, $query1);
  mysqli_stmt_bind_param($stmt1, 's', $_GET['id']);
  mysqli_stmt_execute($stmt1);
  mysqli_stmt_bind_result($stmt1, $id, $first_name, $middle_name, $last_name, $age, $month, $day, $year, $birth_place, $gender, $martial_status, $religion, $nationality, $email_address, $address_1, $address_2, $course, $school_graduated, $remarks, $date_added, $crew_status, $image_name, $passport);


?>

2 个答案:

答案 0 :(得分:0)

尝试使用GET方法,因为您要获取数据以更新“method =”GET“&gt;

答案 1 :(得分:0)

更改:

<form action="edit_crew.php?id=$id" method="POST">

致:

<form action="edit_crew.php?id=<?=$_GET['id']?>" method="POST">