我尝试使用此表单编辑我输入数据库的数据。
<form method="POST">
<table align ="center">
<br>
<tr><td>First Name: </td> <td><input type="text" name="first" pattern="[A-Za-z]{1,}"
title="A-Z only" required value="<?php echo $query2['fname']; ?>"></td> </tr>
<tr><td>Middle Initial: </td><td><input type="text" name="middle"
value="<?php echo $query2['middleinitial'] ?>"></td></tr>
<tr><td>Last Name:</td><td> <input type="text" name="last"
value="<?php echo $query2['lname'] ?>"></td></tr>
<tr><td>Contact Number:</td><td> <input type="tel" name="contact" maxlength="11"
value="<?php echo $query2['contactnum'] ?>"></td></tr>
<tr> <td>Province: </td><td> <input type="text" name="provincee"
value="<?php echo $query2['province'] ?>"></td></tr>
<tr> <td>City: </td><td> <input type="text" name="cityy"
value="<?php echo $query2['city'] ?>"></td></tr>
<tr> <td>Username:</td><td> <input type="email" name="usernamee"
value="<?php echo $query2['username'] ?>"></td></tr>
<tr><td>Password:</td><td> <input type="password" name="pass"
value="<?php echo $query2['password'] ?>"></td></tr>
<tr><td><br> <input type="Submit" name="submitt"></td></tr>
</table>
</form>
这是我的PHP代码。
if(isset($_GET['id'])){
$userid=$_GET['id'];
}
if(isset($_POST['submitt'])){
$firstname= mysqli_real_escape_string($mysqli,$_POST['first']);
$middleinitial= mysqli_real_escape_string($mysqli,$_POST['middle']);
$lastname= mysqli_real_escape_string($mysqli,$_POST['last']);
$contactnumber= mysqli_real_escape_string($mysqli,$_POST['contact']);
$province= mysqli_real_escape_string($mysqli,$_POST['provincee']);
$city = mysqli_real_escape_string($mysqli,$_POST['cityy']);
$username= mysqli_real_escape_string($mysqli,$_POST['usernamee']);
$password= mysqli_real_escape_string($mysqli,$_POST['pass']);
$query=mysqli_query($mysqli, "UPDATE usertbl SET fname='$firstname',
middleinitial='$middleinitial', lname='$lastname',contactnum = '$contactnumber',
province = '$province',city= '$city' ,username = '$username' ,
password = '$password' where userid='$userid'");
if($query){
header('location:employees.php');
}
$query1=mysqli_query($mysqli,"SELECT * FROM usertbl WHERE userid='$userid'");
$query2 = mysqli_fetch_array($query1);
}
继续显示的错误是表单上信息的输入类型值的未定义变量。我试图从数据库中获取数据,但我似乎无法得到它。这似乎是什么问题?
这些是错误:
注意:未定义的变量: C:\ xampp \ htdocs \ thesis2 \ admin \ pages \ editemployee.php 中的查询2 372
注意:未定义的变量: C:\ xampp \ htdocs \ thesis2 \ admin \ pages \ editemployee.php 中的查询2 375
注意:未定义的变量: C:\ xampp \ htdocs \ thesis2 \ admin \ pages \ editemployee.php 中的查询2 378
注意:未定义的变量: C:\ xampp \ htdocs \ thesis2 \ admin \ pages \ editemployee.php 中的查询2 381
注意:未定义的变量: C:\ xampp \ htdocs \ thesis2 \ admin \ pages \ editemployee.php 中的查询2 384
注意:未定义的变量: C:\ xampp \ htdocs \ thesis2 \ admin \ pages \ editemployee.php 中的查询2 387
注意:未定义的变量: C:\ xampp \ htdocs \ thesis2 \ admin \ pages \ editemployee.php 中的查询2 390
注意:未定义的变量: C:\ xampp \ htdocs \ thesis2 \ admin \ pages \ editemployee.php 中的查询2 390
答案 0 :(得分:0)
我认为问题在于您的评估顺序。您尝试仅在提交表单时进行查询(以及重定向之后 - 这没有任何意义,因为在您之前进行重定向之前永远不会到达查询)。
我就是这样做的
<?php
$userid = isset($_GET['id']) ? $_GET['id'] : null;
if (isset($_POST['submitt'])) {
$firstname = trim($_POST['first']);
$middleinitial = trim($_POST['middle']);
$lastname = trim($_POST['last']);
$contactnumber = trim($_POST['contact']);
$province = trim($_POST['provincee']);
$city = trim($_POST['cityy']);
$username= trim($_POST['usernamee']);
$password= $_POST['pass']; // leave it as is
$stmt = mysqli_prepare($mysqli, "
UPDATE usertbl
SET fname = ?, middleinitial = ?, lname = ?, contactnum = ?, province = ?, city = ?, username = ?, password = ?
WHERE userid = ?
");
mysqli_stmt_bind_param($stmt, 'ssssssssd',
$firstname, $middleinitial, $lastname, $contactnumber, $province, $city, $username, $password, $userid);
$updated = mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
// redirect if user is updated
if ($updated) {
header('Location: employees.php');
exit;
}
}
// note: the next lines of code appear OUTSIDE the previous if statement (1)
// cheating here as you should definitely be using prepared statements here also!
if ($result = mysqli_query($mysqli, "SELECT * FROM usertbl WHERE userid = '$userid'")) {
$query2 = mysqli_fetch_array($result);
}
?>
<!-- HTML form -->
<强>侧面备注:强>
没有理由真正使用mysqli_real_escape_string
。您应该使用prepared statements。
我通常不使用MySQLi,因为它们的语法非常丑陋。使用PDO更简单,更清洁。