我有一個表格顯示在用戶屏幕上。當用戶點擊編輯時,我希望他能夠點擊該特定行的參數並更新單個參數(例如,名稱)。那麼其他參數(例如,電子郵件,密碼和地址)應該保持不變。更新表中的特定行
name email password address action
user [email protected] user u.address edit
用於編輯鏈接的代碼是:
echo"<td class='center'><a class='btn btn-info' href=\"admin_edit_user.php?id=".$row['id']."\"><i class='glyphicon glyphicon-edit icon-white'></i>Edit</a></td>";
守則admin_edit_user.php
頁:
<form class="form-horizontal" role="form" action="admin_update_user.php" enctype="multipart/form-data" method="post">
<div class="form-group">
<label class="col-lg-3 control-label">Name</label>
<div class="col-lg-8">
<input class="form-control" name="name" value="" type="text">
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Email</label>
<div class="col-lg-8">
<input class="form-control" name="email" value="" type="text">
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Password</label>
<div class="col-lg-8">
<input class="form-control" name="password" value="" type="text">
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Address</label>
<div class="col-lg-8">
<input class="form-control" name="address" value="" type="text">
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label"></label>
<div class="submit">
<input class="btn btn-primary" value="Save Changes" type="submit" name="submit">
</div>
</div>
</form>
這種形式的用戶被重定向到admin_update_user.php
頁
<?php
$con=mysqli_connect("localhost","root","","db");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$id = $_GET['id'];
$query="SELECT * FROM user";
$result= mysqli_query($con, $query) or die(mysqli_error());
while ($row= mysqli_fetch_array($result))
{
$name_data=$row['name'];
$name_email=$row['email'];
$name_password=$row['password'];
$name_address=$row['address'];
}
if(isset($_POST['submit']))
{
$name=$_POST['name'];
$email=$_POST['email'];
$password=$_POST['password'];
$address=$_POST['address'];
if(empty($name))
{
//if the value is empty its going to set it equal to the database value
$name=$name_data;
}
else
$name=$name;
if(empty($email))
{
$email=$name_email;
}
else
$email=$email;
if(empty($password))
{
$password=$name_password;
}
else
$password=$password;
if(empty($address))
{
$address=$name_address;
}
else
$address=$address;
}
//0: demo.name (value given for name)
//[email protected] (value given for email)
//demo (value given forpassword)
//demo
//demo.address (value given for address)
$sql = "UPDATE user SET name='".$name."',email='".$email."',password='".$password."',address='".$address."' WHERE id ='".$id."'";
echo mysqli_errno($con) . ": " . mysqli_error($con) . "\n";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
header("Location: admin_user_list.php");
exit;
mysqli_close($con);
?>
後
我在更新查詢中收到錯誤。如果有人能幫忙,我會非常感激。
P.S @ FortMauris這裏是你希望看到的編輯部分。
$id = $_GET['id'];
$name = mysqli_real_escape_string($con, $_POST['name']);
$email = mysqli_real_escape_string($con, $_POST['email']);
$password = mysqli_real_escape_string($con, $_POST['password']);
$address = mysqli_real_escape_string($con, $_POST['address']);
$query="SELECT * FROM user";
$result= mysqli_query($con, $query) or die(mysqli_error());
//get the value from database
while ($row= mysqli_fetch_array($result))
{
$name_data=$row['name'];
$name_email=$row['email'];
$name_password=$row['password'];
$name_address=$row['address'];
}
if(isset($_POST['submit']))
{
$name=$_POST['name'];
$email=$_POST['email'];
$password=$_POST['password'];
$address=$_POST['address'];
if(empty($name))
{
//if the value is empty its going to set it equal to the database value
$name=$name_data;
}
if(empty($email))
{
$email=$name_email;
}
if ($password == '')
{
$password=$name_password;
}
if(empty($address))
{
$address=$name_address;
}
}
echo mysqli_errno($con) . ": " . mysqli_error($con) . "\n";
$sql = "UPDATE user SET name='".$name."',email='".$email."',password='".$password."',address='".$address."' WHERE id ='".$id."'";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
mysqli_close($con);
//header("Location: admin_user_list.php");
exit;
mysqli_close($con);
你會得到什麼錯誤? – 2014-10-29 07:10:12
你得到什麼錯誤 – Naincy 2014-10-29 07:10:47
@sgt mysql_errno()期望參數1是資源 – sam 2014-10-29 07:15:11