我写了一个基本脚本,允许用户编辑他们的注册详细信息。
表单中的字段将填入登录用户的数据。当我尝试保存记录时,我收到以下错误:
Query was: UPDATE ccusers SET forename='admin', surname='admin', username='admin',
password='admin', email='admin@gmail.com' WHERE id='Array' AND 'admin'='admin'. Error:
在查询的这个阶段,它没有拿起id。
$editid = mysql_real_escape_string($_POST['editid']);
$Query = "UPDATE ccusers SET forename='$forename', surname='$surname',
username='$username', password='$password', email='$email' WHERE id='$editid'
AND '$user'='$user'";
我删除了Delete和editid输入中隐藏的类型,它们都显示了Array。
<input name="Delete" value="<?php echo $id;?>">
<input name="editid" value="<?php echo $id;?>"/>
我花了2个小时。我无法弄清楚我做错了什么?我附上了表格结构的截图。
SCRIPT
<?php
//connect to database, check login credentials, declare message variables
include "connect.php";
require "authenticate.php";
error_reporting(E_ERROR);
$message = $_GET['message'];
$user = $_SESSION['UserName'];
// var_dump ($user);
// exit;
$fnameErr = $snameErr = $unameErr = $passwordErr = $emailErr = "";
$forename = $surname = $username = $password = $email = "";
function validate_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if (isset($_POST['Submit']))
{
$has_errors = false;
if (empty($_POST["forename"])) {
$has_errors = true;
$fnameErr = "Please enter your forename";
}else{
$forename = validate_input($_POST["forename"]);
}
if (empty($_POST["surname"])) {
$has_errors = true;
$snameErr = "Please enter your surname";
} else {
$surname = validate_input($_POST["surname"]);
}
if (empty($_POST["username"])) {
$has_errors = true;
$usernameErr = "Please enter a username";
} else {
$username = validate_input($_POST["username"]);
}
if (empty($_POST["password"])) {
$has_errors = true;
$passwordErr = "Please enter a password";
} else {
$password = validate_input($_POST["password"]);
}
if (empty($_POST["email"])) {
$has_errors = true;
$emailErr = "Please enter an email address";
} else {
$email = validate_input($_POST["email"]);
}
//write edited data into tables matching logged in user with their data
$editid = mysql_real_escape_string($_POST['editid']);
$Query = "UPDATE ccusers SET forename='$forename', surname='$surname',
username='$username', password='$password', email='$email' WHERE id='$editid'
AND '$user'='$user'";
if($sql = mysql_db_query ($DBName, $Query, $Link)) {
$message = "Changes Saved";
$current = $_GET['page'];
header("Location: ccedituser.php?message=".urlencode($message));
} else {
die("Query was: $Query. Error: ".mysql_error($Link));
}
}
//show logged in user their updated data
$user = $_SESSION['UserName'];
$result = mysql_query("SELECT * FROM ccusers WHERE ccusers.username = '$user'")
or die(mysql_error());
while($row = mysql_fetch_array($result)){
$id[] = $row['id'];
$forename = $row['forename'];
$surname = $row['surname'];
$username = $row['username'];
$email = $row['email'];
$password = $row['password'];
}
//delete form and image data when users clicks delete button
if (isset($_POST['Delete'])){
$deleteuser = $_POST['Delete'];
mysql_query("DELETE FROM ccusers WHERE id = '$deleteuser'");
mysql_query("ALTER TABLE ccusers AUTO_INCREMENT = 1");
$message = 'Profile Deleted';
header("Location: ccedituser.php?&message=".urlencode($message));
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<form action="ccedituser.php?" method="post" enctype="multipart/form-data" name="edit" id="editrecord">
<fieldset>
<div class="legendregister">Edit Profile</div>
<div class="registrationcontainer">
<div class="registrationcontainerinner">
<div><label class="labelshow">Forename</label><input class="insetpodcast"
name="forename" type="text" placeholder="Enter your forename" value="
<?PHP echo $forename; ?>"/><p class="errorinput"><?php echo $fnameErr;?></p></div>
<div><label class="labelshow">Surname</label><input class="insetpodcast"
name="surname" type="text" placeholder="Enter your surname"
value="<?PHP print $surname; ?>"/><p class="errorinput">
<?php echo $snameErr;?></p></div>
<div><label class="labelshow">Username</label><input class="insetpodcast"
name="username" type="text" placeholder="Enter a username"
value="<?PHP print $username; ?>"/>
<p class="errorinput"><?php echo $usernameErr;?></p></div>
<div><label class="labelshow">Password</label><input class="insetpodcast"
name="password" type="text" placeholder="Enter a password" value="
<?PHP print $password; ?>"/><p class="errorinput">
<?php echo $passwordErr;?></p></div>
<div><label class="labelshow">email</label><input class="insetpodcast"
name="email" type="text" placeholder="Enter an email address"
value="<?PHP print $email; ?>"><p class="errorinput">
<?php echo $emailErr;?></p></div>
<div class="submit"><input name="Submit" type="submit" class="submitreg"
value="Save"/></div>
<div class="delete"><input name="deletebtn" type="submit" class="resetreg"
value="Delete"/></div>
<input name="Delete" value="<?php echo $id;?>">
<input name="editid" value="<?php echo $id;?>"/>
</div>
</div>
</div>
</form>
</fieldset>
答案 0 :(得分:2)
使用$ id = $ row ['id']代替$ id []。
答案 1 :(得分:0)
查看错误语句中id列限定符的值,它显示为'Array',表示$ editid变量是一个数组。尝试使用$ editid [0]代替,或者更好地使用val_dump变量来帮助确定变量为数组而不是字符串或整数的原因。
答案 2 :(得分:0)
为什么这个WHERE id ='$ editid' AND'$ user'='$ user'“你把'$ user'='$ user'尝试列名user ='$ user'