提交到数据库是否会更改显示查询的结果?

时间:2013-07-11 15:58:12

标签: php mysql

以下是生成该人将看到的表的代码(默认为$ date作为今天的日期,如果他们选择未来日期或过去日期,则$ date会根据URL变量更改。$ time只是$ row [time] )

$query = mysql_query("SELECT * FROM times", $db);
while ($row = mysql_fetch_array($query)){ //grab all available times
    $time = $row[time];
    $query2 = mysql_query("SELECT * FROM appts WHERE time = '$time' AND date = '$date' ", $db); //select all appointments that match current date and available times
    $aptid = $apt[id];

    while($apt = mysql_fetch_array($query2))
    {
    $mem = $apt[mem];    
    if($mem != ''){
        $class = 'green';
    }
    $ns = $apt[ns];
    $x = $apt[x];
    if($ns || $x != ''){
        $class = 'yellow';
    }
    echo "<tr><td class='$class'>$apt[date_set]</td><td class='$class'>";
    echo date('h:i A', strtotime($row[time]));
    echo "</td>";
    $aptid = $apt[id];
    echo "<td class='$class'>$apt[name]</td>
    <td class='$class'>$apt[phone]</td>
    <td class='$class'>$apt[phone2]</td>
    <td class='$class'>$apt[src]</td>
    <td class='$class'>$apt[coach]</td>
    <td class='$class'>$apt[v1]</td>
    <td class='$class'>$apt[v2]</td>
    <td class='$class'>$apt[mem]</td>
    <td align='center' class='$class'>$apt[x]</td>
    <td align='center' class='$class'>$apt[ns]</td>
    <td class='$class'>$apt[comments]</td>
    <td align='center'>";
    if($aptid != ''){//i dont want to display options on blank rows
        echo "<a href='index.php?date=$date&apptid=$aptid&action=edit'>Edit</a> &nbsp;
        <a href='index.php?year=$year&month=$month&date=$date&apptid=$aptid&action=x'>X</a>&nbsp; 
        <a href='index.php?year=$year&month=$month&date=$date&apptid=$aptid&action=ns'>NS</a>&nbsp;";
        //<a href='index.php?year=$year&month=$month&date=$date&apptid=$aptid&action=delete'>Delete</a> (HIDDEN FOR NOW)
    }
    echo "</td></tr>";
    }   
}

?>
    </tbody>
</table>

以下是将新记录添加到数据库的代码

<div class='dialog' title='New Appointment'>
    <form id='newappt' method='post'>
        <table>
            <tr>
                <td>Time</td>
                <td><select name='time'>
                <? $query = mysql_query("SELECT * FROM times", $db); //set time option as times available from times db
                   while($row = mysql_fetch_array($query)){
                    echo "<option value = '$row[time]'>";
                    echo date('h:i A', strtotime($row[time])); 
                    echo "</option>";
                   }
                ?>
                </td>
            </tr>
            <tr>
                <td>Date</td>
                <td><input type='text' id='datepicker' name='date'></td>
            </tr>
            <tr>
                <td>Name</td>
                <td><input type='text' name='name'></td>
            </tr>
            <tr>
                <td>Phone</td>
                <td><input type='text' name='phone'></td>
            </tr>
            <tr>
                <td>Phone 2</td>
                <td><input type='text' name='phone2'></td>
            </tr>
            <tr>
                <td>Src</td>
                <td><input type='text' name='src'></td>
            </tr>
            <tr>
                <td>Coach</td>
                <td><input type='text' name='coach'></td>
            </tr>
            <tr>
                <td>Comments</td>
                <td><input type='text' name='comments'></td>
            </tr>
        </table>
        <input type='submit' name='submit-newappt' value='Add Appointment'>
    </form>
</div>

然后是继续发生的事情:

    if($_POST['submit-newappt']){ //when submitted insert new record and display success message.
        $dateset = date("Y-m-d");
        $q = "INSERT INTO appts (date_set, time, date, name, phone, phone2, src, coach, comments) VALUES ('$dateset', '$_POST[time]', '$_POST[date]', '$_POST[name]', '$_POST[phone]', '$_POST[phone2]', '$_POST[src]', '$_POST[coach]', '$_POST[comments]')";
        $result = mysql_query($q) or die(mysql_error());    
        echo "<br>New appointment added successfully for ";
        echo $_POST['name'];
        echo " at ";
        echo $_POST['date'];
        echo " at ";
        echo date('h:i A', strtotime($_POST['time']));
    }

所以说我今天正在观看,该表将显示今天约会的所有约会。然后,我在2013年7月14日“添加新约会”。它改变了表格,以显示我刚刚加入约会而不是今天留在任何日期的约会。

tl; dr(ish) - 为什么在向数据库添加新记录时我的php查询会发生变化?

日期来自:

if(isset($_GET['date'])){
        $date = $_GET['date'];
    }
    if(!$date){
        $date = date('Y-m-d');
    }

0 个答案:

没有答案