2014-07-15 75 views
0
<?php 
    mysql_connect("localhost", "root", "") or die(mysql_error()); 
    mysql_select_db("dentalclinic") or die(mysql_error()); 

    if (isset($_POST['update'])){ 
    $UpdateQuery = "UPDATE appointment SET appointmentstatusid='$_POST[appointmentstatusid]'"; 
    mysql_query($UpdateQuery); 
    }; 

    $sql = "SELECT * from appointment a join appointmentstatus s on (a.appointmentstatusid=s.appointmentstatusid) join patient p on (a.patientid=p.patientid)"; 
    $query = mysql_query($sql) or die(mysql_error()); 

    echo "<table border=1> 
    <tr> 
    <th>FIRST NAME</th> 
    <th>LAST NAME</th> 
    <th>APPOINTMENT STATUS</th> 
    <th>UPDATE</th> 
    </tr>"; 

    while($record = mysql_fetch_array($query)){ 
    echo "<form action=editstatus.php method=post>"; 
    echo "<tr>"; 
    echo "<td>"."<input type=text name=firstname value=".$record['firstname']."></td>"; 
    echo "<td>"."<input type=text name=lastname value=".$record['lastname']."></td>"; 
    echo "<td>"; 
    $query2 = "SELECT * from appointmentstatus"; 
    $result = mysql_query($query2); 
    echo "<select name=appointmentstatusid>"; 
    while ($line = mysql_fetch_array($result)) { 
    echo "<option value=".$line['appointmentstatusid'].">"; 
    echo $line['appointmentstatus']; 
    echo "</option>"; 
    } 
    echo "</select>"; 
    echo "</td>"; 

    echo "<td>"."<input type=submit name=update value=update"."></td>"; 
    echo "</tr>"; 
    echo "</form>"; 
    }  
echo "</table>" 
?> 

每當我更新patient1的appointmentstatusid,它會影響其他患者(patient2,patient3 ...)的約會狀態。我嘗試在更新中添加代碼WHERE appointmentstatusid='$_POST[appointmentstatusid]',但是當我這樣做時,它不會再更新。如何更新特定行(患者)?

+0

嘗試$ UpdateQuery =「更新約會SET appointmentstatusid ='{$ _ POST ['appointmentstatusid']}'」; –

+0

不要再使用'mysql_ *'函數,因爲它們在當前版本的PHP中被取消了。改用「PDO」或「mysqli」。 – TiMESPLiNTER

+0

@SyedQarib的結果是一樣的。它會更新一切。 –

回答

0

要更新特定的記錄,查詢應該有一個WHERE子句指定要更新什麼

$UpdateQuery = "UPDATE appointment SET appointmentstatusid='$_POST[appointmentstatusid]' WHERE `appointmentid` = 1"; 

請注意:直接從$ _POST傳遞$ _POST [appointmentstatusid]變量會使你的代碼容易受到sql注入的影響。嘗試

$appointmentstatusid = (int) mysql_real_escape_string($_POST[appointmentstatusid]); 
$UpdateQuery = "UPDATE appointment SET appointmentstatusid='".appointmentstatusid ."' WHERE `appointmentid` = $id"; 

或使用數據庫抽象庫的練習。

+0

問題。它給了我一個關於WHERE子句的想法。謝謝 :) –

1

您必須有有效,並且匹配您試圖影響的行的編號。你如何確定這取決於你。那麼,這樣做:

UPDATE table_name 
SET column1=value1,column2=value2,... 
WHERE some_column=some_value; 
0

試試這行6

$UpdateQuery = "UPDATE appointment SET willChangeColumn = 'newValueForThatColumn' WHERE appointmentstatusid=".$_POST['appointmentstatusid']; 
+0

它仍然更新所有的約會狀態ID。 –

+0

此查詢將全部更新,因爲沒有聲明任何條件,如果您想要更新特殊行,您必須聲明條件並解決WHERE –