我已经创建了一个表单来更新名称到数据库,我已经在数据库中创建了两个表first_name
和last_name
,看看曾经这里是我的代码:
$sql="
CREATE TABLE admin
(
id INT PRIMARY KEY AUTO_INCREMENT,
first_name VARCHAR(100),
last_name VARCHAR(100),
)";
现在我创建了更新表单:
<div class="box">
<h1 style="font-family: consolas">Change your name</h1><hr>
<div id="change_name">
<label><strong>Your current name: </strong></label>
<?php
include('change_setting_db.php');
while($row = mysqli_fetch_array($result))
{
echo $row['first_name']." ".$row['last_name'];
}
?><br><br>
<form method="post" action="do_update_name.php">
<label><strong>First name: </strong></label>
<input type="text" name="first_name">
<label><strong>Last name: </strong></label>
<input type="text" name="last_name">
<input type="submit" value="Submit">
</form>
</div>
</div>
这是我的do_update_name.php
文件:
<?php
$firstname=$_POST['first_name'];
$lastname=$_POST['last_name'];
$id=$_GET['id'];
$con=mysqli_connect("localhost","root","Bhawanku", "members");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$update =mysqli_query($con,"UPDATE admin SET first_name='$firstname' AND last_name='$lastname' WHERE id='$id' ");
if($update){
echo "Successfully created!!";
}
?>
现在问题是,当我输入新名称并提交时,它显示错误:
Notice: Undefined index: id in C:\Users\Raj\PhpstormProjects\...\new do_update_name.php on line 4
Successfully created!!
答案 0 :(得分:1)
<form method="post" action="do_update_name.php">
<label><strong>First name: </strong></label>
<input type="text" name="first_name">
<label><strong>Last name: </strong></label>
<input type="text" name="last_name">
<input type="submit" value="Submit">
</form>
您必须在此处添加ID,以便对方可以选择$ _GET参数[&#39; id&#39;] 例如:
<form method="post" action="do_update_name.php?id=1">
<label><strong>First name: </strong></label>
<input type="text" name="first_name">
<label><strong>Last name: </strong></label>
<input type="text" name="last_name">
<input type="submit" value="Submit">
</form>
答案 1 :(得分:1)
输入类型为隐藏ID的HTML表单
<div class="box">
<h1 style="font-family: consolas">Change your name</h1><hr>
<div id="change_name">
<label><strong>Your current name: </strong></label>
<?php
include('change_setting_db.php');
while($row = mysqli_fetch_array($result))
{
$id = $row['id'];
$first_name = $row['first_name'];
$last_name = $row['last_name'];
echo $id.") ".$first_name." ".$last_name;
}
?>
<br>
<br>
<form method="post" action="do_update_name.php">
<input type="hidden" name="id" value="<?php echo $id;?>">
<label><strong>First name: </strong></label>
<input type="text" name="first_name" value="<?php echo $first_name;?>">
<label><strong>Last name: </strong></label>
<input type="text" name="last_name" value="<?php echo $last_name;?>">
<input type="submit" value="Submit">
</form>
</div>
</div>
带有$ _POST ['id']的PHP代码
<?php
$firstname=$_POST['first_name'];
$lastname=$_POST['last_name'];
$id=$_POST['id'];
…
?>