使用变量更新语句

时间:2014-03-27 04:36:26

标签: php mysql

似乎我的代码在使用我的系统的编辑功能后没有显示查询,在按下保存/提交按钮后,我试图使用通过url的变量并使用$ _GET仅显示用户自己的帐号,我知道我的代码与Mysql的更新语句有冲突,请帮忙。

我的问题是:如何在按下表单中的保存按钮后显示查询?

这是我的代码:

   <?PHP
   include ("dbcon1.php");

//通过URL获取VARIABLE USERNAME

   $username=$_GET['username'];

   ?>

   <html>
   <head>
   </head>
   <body>
   <form method="post">
   <table>
   <?PHP

//只能获取网页上的查询(edit2.php?USERNAME = $ USERNAME)

   $customerquery=mysql_query("select * from customerinfo where username='$username'");
   $customerrows=mysql_fetch_array($customerquery);
   ?>

//表示有用户信息

   <tr><td>First name:</td><td><input type="text" name="fname" value="<?PHP echo $customerrows['fname'];?>"></td></tr>
   <tr><td>Last name:</td><td><input type="text" name="lname" value="<?PHP echo $customerrows['lname'];?>"></td></tr>
   <tr><td>Address:</td><td><input type="text" name="address" value="<?PHP echo $customerrows['address'];?>"></td></tr>
   <tr><td>Contact Number:</td><td><input type="text" name="contactno" value="<?PHP echo $customerrows['contactno'];?>"></td></tr>
   <tr><td>Username:</td><td><input type="text" name="username" value="<?PHP echo $customerrows['username'];?>"></td></tr>
   <tr><td>Password:</td><td><input type="password" name="password" value="<?PHP echo $customerrows['password'];?>"></td></tr>

//保存按钮

   <tr><td><input type="submit" name="submit" value="Save"></td></tr>

   </table>
   </form>
   </body>
   </html>
   <?PHP
   include('dbcon1.php');
   include('dbcon.php');

//按下时保存按钮,更新表格

   if(isset($_POST['submit'])){
   $username=$_GET['username'];
   $fname=$_POST['fname'];
   $lname=$_POST['lname'];
   $address=$_POST['address'];
   $contactno=$_POST['contactno'];
   $username=$_POST['username'];
   $password=$_POST['password'];

//更新表

   mysql_query("update customerinfo set fname='$fname',lname='$lname',address='$address',contactno='$contactno',username='$username',password='$password' where username='$username'");
   header("location:index5.php?username=$username");
   }
   ?>
   <table border='1'>
   <?PHP
   include('dbcon.php');
   include('dbcon1.php');

//通过URL获取VARIABLE USERNAME

   $username = $_GET['username'];

//显示当前正在登录的用户 //关于用户的信息表

   $customerquery = mysqli_query($con,"SELECT * FROM customerinfo WHERE username  = '$username'");
   while($customerrows=mysqli_fetch_array($customerquery)){
   ?>
   <tr>
   <td>Id</td><td>First Name</td><td>Last Name</td><td>Address</td><td>Contact No</td <td>Username</td><td>Password</td><td>Edit</td>
   </tr>
   <tr>
   <td><?PHP echo $customerrows['id'];?></td>
   <td><?PHP echo $customerrows['fname'];?></td>
   <td><?PHP echo $customerrows['lname'];?></td>
   <td><?PHP echo $customerrows['address'];?></td>
   <td><?PHP echo $customerrows['contactno'];?></td>
   <td><?PHP echo $customerrows['username'];?></td>
   <td><?PHP echo $customerrows['password'];?></td>

//编辑按钮

   <td><input type="button" value="edit" onClick="window.location='edit2.php?username=<?php echo $username ?>'"></td>
   </tr>
   <?PHP } ?>
   </table>
   <a href="login1.php">Log-out</a>

1 个答案:

答案 0 :(得分:0)

首先,您的查询有错误

$customerquery=mysql_query("select * from customerinfo where username='".$username."' ");

mysql_query("UPDATE customerinfo SET fname='".$fname."',lname='".$lname."',address='".$address."',contactno='".$contactno."',username='".$username."',password='".$password."' WHERE username='".$username."' ");