2015-09-06 109 views
0

我有以下变量列表,我试图将这些传递给sql UPDATE语句。将变量传递给mysql语句

但更新语句不起作用,我意识到ID值没有被sql语句检测到。

我应该怎么做呢?

形式:

<table class="table table-striped table-bordered table-hover" id="dataTables-example" data-pagination="true"> 
            <form name="form1" method="post" action="processUpdateAccount.php"> 
            <thead> 
             <tr> 
              <th>Name</th> 
              <th>Account</th> 
              <th>Email Address</th> 
              <th>Contact</th> 
              <th>Country</th> 
              <th>Town</th> 
              <th>Address</th> 
              <th>Postal Code</th> 
              <th>Action</th> 
             </tr> 
            </thead> 

            <tbody> 
             <tr class="gradeU"> 
              <td> 
               <input name="userName" type="text" id="userName" value="<? echo $rowsUsers['name']; ?>" required> 
              </td> 
              <td> 
               <input name="userAccount" type="text" id="userAccount" value="<? echo $rowsUsers['account']; ?>" required> 
              </td> 
              <td> 
               <input name="userEmail" type="text" id="userEmail" value="<? echo $rowsUsers['email']; ?>" required> 
              </td> 
              <td> 
               <input name="userPhone" type="text" id="userPhone" value="<? echo $rowsUsers['phone']; ?>" required> 
              </td> 
              <td> 
               <input name="userCountry" type="text" id="userCountry" value="<? echo $rowsUsers['country']; ?>" required> 
              </td> 
              <td> 
               <input name="userTown" type="text" id="userTown" value="<? echo $rowsUsers['town']; ?>" required> 
              </td> 
              <td> 
               <input name="userAddress" type="text" id="userAddress" value="<? echo $rowsUsers['address']; ?>" required> 
              </td> 
              <td> 
               <input name="userPostalCode" type="text" id="userPostalCode" value="<? echo $rowsUsers['postalCode']; ?>" required> 
              </td> 

              <!-- link to updateAccount.php and send value of ID --> 
              <td> 
               <input type="submit" name="Submit" value="Update"> 
              </td> 

              <input name="userId" type="hidden" id="userId" value="<? echo $rows['Id'];?>"> 
              <input name="userPassword" type="hidden" id="userPassword" value="<? echo $rows['password'];?>"> 
              </form> 
             </tr> 
            </tbody> 
           </table> 

processUpdateAccount.php

if (isset($_POST['userId']) > 0) 
     { 
      // query db 
      $Id = $_POST['userId']; 
      $name = $_POST['userName']; 
      $account = $_POST['userAccount']; 
      $email = $_POST['userEmail']; 
      $phone = $_POST['userPhone']; 
      $country = $_POST['userCountry']; 
      $town = $_POST['userTown']; 
      $address = $_POST['userAddress']; 
      $postalCode = $_POST['userPostalCode']; 
      $password = $_POST['userPassword']; 

      $sql="UPDATE users SET email='$email', password='$password', account='$account', phone='$phone', country='$country', town='$town', address='$address', postalCode='$postalCode', name='$name' WHERE id='$Id'"; 
      $result=mysql_query($sql); 
      $result = mysql_query("UPDATE users SET name='testing' WHERE Id='28'"); 

      if($result) 
      { 
       echo '<META HTTP-EQUIV="Refresh" Content="0; URL=manageAccounts.php?msg=1">'; 
      } 
      else 
      { 
       echo '<META HTTP-EQUIV="Refresh" Content="0; URL=updateAccount.php?Id=' . $Id .'&msg=2">'; 
      } 
     } 
+0

可以共享的HTML表单? – RamRaider

+0

分享表格 –

+0

隐藏的输入'userid'有一个值'$ rows ['Id']'和一个隐藏的值'$ rows ['password']',但其他字段使用'$ rowsUsers'。顺便说一句,为什么你将用户的密码添加为隐藏值? – RamRaider

回答

2

你获取一切从$rowsUsers else和ID从$rows因此这可能是非常错字这使你的ID空。

echo $rows['Id']; 

最有可能是

echo $rowUsers['Id']; 
0

使用PDO - 它比你采用的方法更安全。 This page是一个很好的介绍。特别看看准备好的陈述部分。所以你的情况,在连接到数据库后,你会碰到这样的:

$data = array('name'=>$name,'account'=>$account, [...] , 'id' => $id); 
$sth = $db->("UPDATE users SET name = :name, account = :account WHERE id = :id"); 
$sth->execute($data);