gpt4 book ai didi

php - 如何从数据库调用 `id`?

转载 作者:行者123 更新时间:2023-11-29 08:04:32 26 4
gpt4 key购买 nike

我创建了一个表单来将名称更新到数据库中,我在数据库中创建了两个表first_namelast_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!!

最佳答案

<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参数['id']例如:

<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>

关于php - 如何从数据库调用 `id`?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22992395/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com