2014-09-21 6 views
-1

我的數據庫沒有更新,沒有顯示任何錯誤,任何人? 我已經設置了$ id和所有變量,但是如果我在查詢中做錯了什麼,請問可以嗎? 預先感謝您SQL查詢不更新數據庫

PHP代碼

   <?php 

        //get values from Database for user 
        if (isset($_GET['id'])) { 
         $id = (int) $_GET['id']; 
         $result = mysql_query("SELECT * FROM Credentials, Company WHERE Credentials.ID = $id AND Company.CompanyID = $id") or trigger_error(mysql_error()); 
         $row = mysql_fetch_array($result); 
        } 

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


         //define index for variables 
         if(isset($_POST['email'])){ 
          $email = $_POST['email']; 
         } 
         if(isset($_POST['password'])){ 
          $password = $_POST['password']; 
         } 
         if(isset($_POST['permission'])){ 
          $permission = $_POST['permission']; 
         } 
         if(isset($_POST['status'])){ 
          $status = $_POST['status']; 
         } 


          //safe input for all fields 
          $email = safe_input($email); 
          $password = safe_input($password); 
          $permission = safe_input($permission); 
          $status = safe_input($status); 






          //double checking inputs 
          foreach($_POST AS $key => $value) { $_POST[$key] = mysql_real_escape_string($value); } 

          //SQL queries 
          $sql = mysql_query("UPDATE Credentials SET `Email` = '".$email."' , `Password` = '".$password."' , `Permission` = '".$permission."', `Status` = '".$status."' WHERE `ID` = $id ") or die(mysql_error()); 

           echo "<div id='add-success'><i class='fa fa-check'></i>&nbsp;<b>Updated Successfuly!</b></div><br/>"; 
          } 

       ?> 

HTML

   <form action='edit.php?id=<?php echo safe_input($id); ?>' method='POST' id="add-merchant" enctype="multipart/form-data"> 

        <p><br /> 
        <h6>Email</h6> 
        <input type='text' name='email' value="<?php echo nl2br($row['Email']); ?>" 
          onfocus="if (this.value==this.defaultValue) this.value = ''" 
          onblur="if (this.value=='') this.value = this.defaultValue"/> 
        <p><br /> 
        <h6>Enter New Password</h6> 
        <input type='text' name='password' value='Password' 
          onfocus="if (this.value==this.defaultValue) this.value = ''" 
          onblur="if (this.value=='') this.value = this.defaultValue"/> 
        <p><br /> 
        <h6>Permission</h6> 
        <select name="permission"> 
         <option value="0" selected>Unverified</option> 
         <option value="1">Verified</option> 
        </select> 
        <p><br /> 
        <h6>Status</h6> 
        <select name="status"> 
         <option value="1" selected>Activated</option> 
         <option value="0">Deactivated</option> 
        </select> 


        <p><input type='submit' value='Update' /><input type='hidden' value='1' name='submitted' /> 

       </form> 
+1

做一些'如果執行在'if'聲明腳本echo'測試 – 2014-09-21 09:32:36

+0

檢查'if'滿足條件還是不行! – MH2K9 2014-09-21 09:32:49

+0

我已經設置了回聲聲明沒有錯誤,查詢成功,它顯示「已成功更新」 – 2014-09-21 09:45:11

回答

0

ID場數字,如果是的話那麼你的SQL查詢應該是這樣的。

$sql = mysql_query("UPDATE Credentials SET `Email` = '".$email."', `Password` = '".$password."', `Permission` = '".$permission."', `Status` = '".$status."' WHERE `ID` = $id ") // id = $id without quotes because it is numeric 

如果上面的查詢工作不試試這個

mysql_query("UPDATE Credentials SET `Email` = '{$email}', `Password` = '{password}', `Permission` = '{$permission}', `Status` = '{$status}' WHERE `ID` = $id") 

如果這也太不粘貼作品在phpMyAdmin查詢或數據庫嘗試運行查詢網上,如果possible.Also設置的error_reporting (E_ALL)讓瀏覽器顯示錯誤;

+0

id是數字我已經試過它沒有引號,它也沒有工作,它可能是什麼除此之外 ? – 2014-09-21 09:41:34

+0

請嘗試我們使用這個,我們不會保證這一個真實性。 – 2014-09-21 09:44:48

+0

已更新我的代碼來添加更多詳情。 – 2014-09-21 09:48:15

0

首先檢查'如果'條件是否爲真。我修改了這樣的查詢併爲我工作。

$DB_NAME = "database name"; 
    $HOST = "localhost"; 
    $USER = "mysql username"; 
    $PASSWD = "mysql password"; 

    $conn = mysql_connect($HOST,$USER,$PASSWD); 
    mysql_select_db('Credentials', $conn); 

'$ conn將' 指定MySQL連接

$sql = mysql_query("UPDATE Credentials SET `Email` = '".$email."', `Password` = '".$password."', `Permission` = '".$permission."', `Status` = '".$status."' WHERE `ID` = '$id' ", $conn) or die(mysql_error());