更新php中的查询不更新或工作

时间:2016-08-07 20:14:07

标签: php html

我正在尝试更新我的表中的内容,但每次单击更新都没有任何反应。这应该直接更新到我正在进行的项目的表中我一直在这个问题,但似乎无法弄明白。

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Admin</title>
</head>


<body>

<?php

$con = mysql_connect('localhost', 'root', '');

if(!$con){
    die("can not connect: " . mysql_error());
}
mysql_select_db("ie_login", $con);

if(isset($_POST['update'])){

    $UpdateQuery = "UPDATE users SET FirstName='$_POST[fName]'. LastName='$_POST[lName]'. Email='$_POST[email]'. UserName='$_POST[username]'. Faculty='$_POST[faculty]'. Unit='$_POST[unit]'. 'Day'='$_POST[day]'. 'Time'='$_POST[time]' WHERE UserName='$_POST[hidden]'";
    mysql_query($UpdateQuery, $con);


};

$sql = "SELECT * FROM users";

$records = mysql_query($sql, $con);

echo "<table class=table table-bordered>
  <tr>
    <th scope=col class=info>User ID:</th>
    <th scope=col class=info>First Name:</th>
    <th scope=col class=info>Last Name:</th>
    <th scope=col class=info>Email:</th>
    <th scope=col class=info>Username:</th>
    <th scope=col class=info>Faculty:</th>
    <th scope=col class=info>Unit:</th>
    <th scope=col class=info>Consultation 1:</th>
    <th scope=col class=info>Time of Consultation 1:</th>    
  </tr>";


          while( $user = mysql_fetch_array( $records )){
      echo "<form method=post action=Admin2.php>";     
      echo "<tr>";
      echo "<td>" . $user['UserID']."</td>";
      echo "<td>" . "<input type=text name=fName value=" . $user['FirstName']. " </td>";
      echo "<td>" . "<input type=text name=lName value=".$user['LastName'] . " </td>";
      echo "<td>" . "<input type=email name=email value=" .$user['Email']. " </td>";
      echo "<td>" . "<input type=text name=username value=" .$user['UserName'] . " </td>";  
      echo "<td>" . "<input type=text name=faculty value=" .$user['Faculty'] . " </td>";
      echo "<td>" . "<input type=text name=unit value=" .$user['Unit'] . " </td>";
      echo "<td>" . "<input type=text name=day value=" .$user['Day'] . " </td>";
      echo "<td>" . "<input type=time name=time value=" .$user['Time'] . " </td>";
      echo "<td>" . "<input type=hidden name=hidden value=" . $user['UserName'] . "</td>";
      echo "<td>" . "<input type=submit name=update value=update" . " </td>";
      echo "</form>";
          }

          echo "</table>";
          mysql_close($con);

?>

</body>
</html>

2 个答案:

答案 0 :(得分:0)

按以下方式编辑查询

$UpdateQuery = "UPDATE users SET FirstName='".$_POST['fName']."', LastName='".$_POST['lName']."', Email='".$_POST['email']."', UserName='".$_POST['username']."', Faculty='".$_POST['faculty']."', Unit='".$_POST['unit']."', Day='".$_POST['day']."', Time='".$_POST['time']."' WHERE UserName=".$_POST['hidden'];

答案 1 :(得分:0)

用以下内容替换您的查询:

$FirstName=$_POST['fname'];
$LastName=$_POST['lName'];
$email=$_POST['email'];
$UserName=$_POST['username'];
$Faculty=$_POST['faculty'];
$Unit=$_POST['unit'];
$Day=$_POST['day'];
$Time=$_POST['time'];
$hidden=$_POST['hidden'];

$UpdateQuery = "UPDATE users SET FirstName='$FirstName', LastName='$LastName', Email='$email', UserName='$UserName', Faculty='$Faculty', Unit='$Unit', 'Day'='$Day', 'Time'='$Time' WHERE UserName='$hidden'";
    mysql_query($UpdateQuery, $con);