2014-12-22 81 views
0

我需要將簽出用戶點擊按鈕之後插入一個時間戳到表遊客柱而出。將簽出按鈕已刪除表中的行liveroster但在與時間戳列了它沒有更新表遊客行。插入MySQL數據到exsisting行

mysql_query("UPDATE visitors SET out=test WHERE ID=" . $ID);

<?php 
/* 
Connection Stuff 
*/ 
     if (isset($_POST['ID'])) { 
      $ID = $_POST['ID']; 
      if (isset($_POST['delete_id'])) { 
       mysql_query("UPDATE visitors SET out=test WHERE ID=" . $ID); 
       mysql_query("DELETE FROM liveroster WHERE ID = " . $ID); 
      } 
     } 
    ?> 

    <!DOCTYPE html> 
    <html lang="en"> 
     <head> 
      <meta charset="utf-8"/> 
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
      <style> 
       #delete-post { 
        width: 100%; 
        margin: auto; 
        background-color: #999; 
       } 
      </style> 
      <script src="http://code.jquery.com/jquery-1.5.min.js"></script> 
     </head> 
     <body> 
      <div> 
       <table border cellpadding="3"> 
        <tr> 
         <td></td> 
         <td><strong>Time</strong></td> 
         <td><strong>Name</strong></td> 
         <td><strong>Teacher</strong></td> 
         <td><strong>Reason</strong></td> 
        </tr> 
        <?php 
        $data = mysql_query("SELECT * FROM liveroster") or die(mysql_error()); 
        while($info = mysql_fetch_array($data)){ ?> 
         <tr> 
          <th> 
           <form method="post" action=""> 
            <input type="hidden" name="ID" value="<?php echo $info['ID']; ?>" /> 
            <input type="submit" name="delete_id" value="Sign-Out" /> 
           </form> 
          </th> 
          <td> 
           <?php echo $info['timestamp']; ?> 
          </td> 
          <td> 
           <?php echo $info['name']; ?> 
          </td> 
          <td> 
           <?php echo $info['teacher']; ?> 
          </td> 
          <td> 
           <?php echo $info['reason']; ?> 
          </td> 
         </tr> 
        <?php } ?> 
       </table> 
      </div> 
     </body> 
    </html> 
+3

兩件事情。 1不要使用** mysql _ ***,因爲它被棄用並且不安全。 2參數化你的查詢,不要只是彈出一個這樣的ID。你可能容易受到sql注入攻擊 –

+0

你想把什麼值放入'out'列?如果你希望它是字符串''test''將你的查詢改爲這個'mysql_query('UPDATE visitor SET ='test'WHERE ID =「。$ ID);' – gellu

+0

@JohnRuddell同意,MYSQLi或PDO是更適合的替代品。 –

回答

0

使用PDO:

<?php 
    $dsn = "mysql:host=localhost;port=$port;dbname=$dbname"; 
     $options = array(
     PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8', 
    ); 
    $dbh = new PDO($dsn, $username, $password, $options); 

    /* 
    Connection Stuff 
    */ 
      if (isset($_POST['ID'])) { 
       $ID = $_POST['ID']; 
       if (isset($_POST['delete_id'])) { 
        /* Update */ 
        $count = $dbh->exec("UPDATE `visitors` SET `out` = 'test' WHERE `ID` = '" . $ID ."'"); 

        /* Delete */ 
        $count = $dbh->exec("DELETE FROM `liveroster` WHERE `ID` = '" . $ID ."'"); 
       } 
      } 
     ?> 

     <!DOCTYPE html> 
     <html lang="en"> 
      <head> 
       <meta charset="utf-8"/> 
       <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
       <style> 
        #delete-post { 
         width: 100%; 
         margin: auto; 
         background-color: #999; 
        } 
       </style> 
       <script src="http://code.jquery.com/jquery-1.5.min.js"></script> 
      </head> 
      <body> 
       <div> 
        <table border cellpadding="3"> 
         <tr> 
          <td></td> 
          <td><strong>Time</strong></td> 
          <td><strong>Name</strong></td> 
          <td><strong>Teacher</strong></td> 
          <td><strong>Reason</strong></td> 
         </tr> 
         <?php 
         $sql = "SELECT * FROM liveroster"; 
         $sth = $dbh->prepare($sql); 
         $sth->execute(); 
         $rows = $sth->fetchAll(); 
         foreach($rows as $value){ ?> 
          <tr> 
           <th> 
            <form method="post" action=""> 
             <input type="hidden" name="ID" value="<?php echo $value['ID']; ?>" /> 
             <input type="submit" name="delete_id" value="Sign-Out" /> 
            </form> 
           </th> 
           <td> 
            <?php echo $value['timestamp']; ?> 
           </td> 
           <td> 
            <?php echo $value['name']; ?> 
           </td> 
           <td> 
            <?php echo $value['teacher']; ?> 
           </td> 
           <td> 
            <?php echo $value['reason']; ?> 
           </td> 
          </tr> 
         <?php } ?> 
        </table> 
       </div> 
      </body> 
     </html> 
+0

你能幫我在上面的代碼中使用PDO嗎? –

+0

嘗試編輯代碼並閱讀PDO手冊http://php.net/manual/de/class.pdo.php。如果一切正常,接受答案:-) – Eugen

+0

尋找有麻煩figureing了連接東西,如在那裏把我的用戶名,密碼錶等 –

0

您可以創建一個列,使用「NOW()」函數每次點擊提交時更新它。

UPDATE visitors 
SET out = test, getdate = NOW() 
WHERE ID = $ID