我希望登录用户希望他们可以更改他们的记录(地址等)。出于这个目的,我为当前登录用户制作了一个自动填充表单。现在的问题是数据赢了,不能更新..
我的表格代码是........
<?php session_start();
include 'conn.php';
include '../includes/layouts/header.php';
if(!isset($_SESSION['user']))
{
header("location:signin.php");
}
$sql="SELECT * FROM signup";
$qry=mysql_query($sql);
$rows=mysql_fetch_array($qry);
?>
<div id="main">
<div id="navigation">
</div>
<div id="page">
<h2>Login Section</h2>
<p>Welcome to LMS</p>
<form method="post" action="update.php">
<div class="reg_section">
<h3>Your Personal Information</h3>
<input type="text" name="fname" value="<?php echo $rows[1];?>" placeholder="First Name"><br>
<input type="text" name="lname" value="<?php echo $rows[2];?>" placeholder="Last Name"><br>
<input type="text" name="uname" value="<?php echo $rows[3];?>" placeholder="Desired Username"><br>
<input type="text" name="email" value="<?php echo $rows[4];?>" placeholder="Email"><br>
<input type="text" name="department" value="<?php echo $rows[5];?>" placeholder="Department"><br>
<input type="text" name="id" value="<?php echo $rows[6];?>" placeholder="Id #"/><br>
<input type="text" name="phone" value="<?php echo $rows[7];?>" placeholder="Phone #"/><br>
</div>
<div class="reg_section">
<h3>Your Password</h3>
<input type="password" name="pass" value="<?php echo $rows[8];?>" placeholder="Your Password"><br>
<input type="password" name="cpass" value="<?php echo $rows[8];?>" placeholder="Confirm Password">
</div>
<div class="reg_section">
<h3>Your Address</h3>
<input type="text" name="address" value="<?php echo $rows[9];?>" placeholder="Address">
</div>
<p class="submit"><input type="submit" name="submit" value="Update Info"></p>
</form>
</div>
</div>
我的PHP代码是..........
<?php session_start();
if(isset($_POST['submit']))
{
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$user=$_POST['uname'];
$email=$_POST['email'];
$depart=$_POST['department'];
$id=$_POST['id'];
$phone=$_POST['phone'];
$pass=$_POST['pass'];
$address=$_POST['address'];
$qry="UPDATE signup SET First_Name=$$fname,Last_Name=$$lname,Username=$$user,Email=$$email,Department=$$depart,Employe_Id=$$id,Phone=$$phone,Password=$$pass,Address=$$address WHERE Username=$$user";
if(mysql_query($qry))
{
header('location:setting.php');
}
}
?>
任何人都可以知道错误是什么......
答案 0 :(得分:0)
正如我在上面的第一条评论中所提到的,您在构建查询时遇到了问题。您使用双美元字符($$
),这没有任何意义。结果是您运行一个空查询,解释了为什么不更新记录。
作为第一个步骤更改您的查询,如下所示:
$qry = "UPDATE signup SET
First_Name='$fname', Last_Name='$lname', Username='$user',
Email='$email', Department='$depart', Employe_Id='$id',
Phone='$phone', Password='$pass', Address='$address'
WHERE Username='$user'";
您没有实现任何形式的错误处理,这就是您没有看到从mysql获得的错误的原因。请查看一些有关如何执行此操作的教程。
还要考虑我关于阻止sql注入的第二条评论。至少,作为某种&#34; hot fix&#34;您应该在集成到查询中的所有用户输入上使用mysql_real_ecape()
。但正如所说,唯一真正的解决方案是切换到准备好的陈述&#34;。
答案 1 :(得分:0)
试试这个sql
$qry="UPDATE signup
SET First_Name='$fname', Last_Name='$lname', Username='$user', Email='$email',Department='$depart', Employe_Id='$id', Phone='$phone', Password='$pass', Address='$address'
WHERE Username='$user' LIMIT 1 ";