我无法将我的名字更新为数据库,但却发出错误:
Notice: Undefined index: first_name in C:\Users\Raj\PhpstormProjects\new linkvessel\change_name.php on line 178
Notice: Undefined index: last_name in C:\Users\Raj\PhpstormProjects\new linkvessel\change_name.php on line 178
我已将first_name
和last_name
两列创建到数据库中,请问我做错了什么?请帮忙
这是我的代码:
<body>
<div id="left_box"><br>
<img src="Images/general_setting.png" height="18" width="18"><a href="general_settings.php" style="text-decoration: none; color: #000000; font-family: Arial";> General</a><br><br>
<img src="Images/photo_setting.png" height="18" width="18"><a href="photo_settings.php" style="text-decoration: none; color: #000000; font-family: Arial";> Photos</a><br><br>
</div>
<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>
<label><strong>First name: </strong></label>
<input type="text" name="first_name">
<label><strong>Last name: </strong></label>
<input type="text" name="last_name">
</form>
<?php
$con=mysqli_connect("localhost","root","Bhawanku", "members");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM admin");
while($row = mysqli_fetch_array($result))
{
}
mysqli_query($con,"UPDATE admin SET first_name='$_POST[first_name]' AND last_name='$_POST[last_name]' WHERE id='$row[id]' ");
mysqli_close($con);
?>
</div>
</div>
</body>
EDITED
答案 0 :(得分:0)
<form method="POST" action="where you want to put the action">
<label><strong>First name: </strong></label>
<input type="text" name="first_name">
<label><strong>Last name: </strong></label>
<input type="text" name="last_name">
</form>
===============================================
使用选择查询获取要更新的用户的ID,然后运行此
1)RUN SELECT QUERY
2)检查用户存在
3)如果用户存在FETCH他的ID并运行更新查询。
$update =mysqli_query($con,"UPDATE admin SET first_name='$_POST[first_name]' AND last_name='$_POST[last_name]' WHERE id='ID' ");
* ID =您要更新的ID
答案 1 :(得分:-2)
您的代码存在许多问题,但纯粹是让它运行起来&#39;你需要做两件事。
首先,将表单更改为POST类型。
接下来,您可能想要更改以下内容:
while($row = mysqli_fetch_array($result))
{
}
mysqli_query($con,"UPDATE admin SET first_name='$_POST[first_name]' AND last_name='$_POST[last_name]' WHERE id='$row[id]' ");
要:
while($row = mysqli_fetch_array($result))
{
mysqli_query($con,"UPDATE admin SET first_name='$_POST[first_name]' AND last_name='$_POST[last_name]' WHERE id='$row[id]' ");
}
您当前的代码不会遍历语句,实际上只执行一次。
我不太确定你通过查询“管理员”来实现的目标。尽管如此 - 目前,它会遍历所有结果并使用您的新名字/姓氏更新最后一行?