如何使用HTML表单方法POST从数据库传递id号
问题描述:
I have this form for editing or updating the information of data but when I clicked the button for UPDATING query the value of $id
is 0
here is my code:
<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>
Here is my code for UPDATE
query:
<?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);
?>
I guess the query is correct the only problem is the id
is empty
BELOW is how I get the $id
I'm using 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);
?>
答
Change:
<form action="edit_crew.php?id=$id" method="POST">
To:
<form action="edit_crew.php?id=<?=$_GET['id']?>" method="POST">
答
Try using the GET method since you are fetching data to update " method="GET">