将MySQL数据插入现有行

时间:2014-12-22 17:00:38

标签: php mysql

我需要在用户点击“注销”按钮后向表格访问者列中插入时间戳。注销按钮已删除表liveroster中的行,但它不会更新列中的表访问者中的行以及时间戳。

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>

2 个答案:

答案 0 :(得分:0)

你可以创建一个列并使用&#34; NOW()&#34;来更新它。每次单击“提交”时都会运行。

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

答案 1 :(得分: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>