2011-10-10 92 views
0

我有2個按鈕,批准並拒絕更新預訂狀態。 嘗試將值傳遞給approve_booking.php時,我迷路了。 根據bookingID不知道如何更新單行。需要編碼幫助!!!如何更新PHP中的單行?

的index.php

<head> 
<script src="jquery-latest.js" type="text/javascript"></script> 
<script src="jquery.tablesorter.js" type="text/javascript"></script> 
<script> 
$(document).ready(function() { 
$("#myTable").tablesorter({widgets: ['zebra']}); 
}); 

$(document).ready(function() 
{ 
    $("#myTable").tablesorter(); 
} 
); 

$(document).ready(function() 
{ 
    $("#myTable").tablesorter({sortList: [[0,0], [1,0]]}); 
} 
); 
</script> 
<link href="style.css" rel="stylesheet" type="text/css"> 
<link href="stylelogin.css" rel="stylesheet" type="text/css"> 
</head> 
<body> 
<form name="form1" method="post" action="approve_booking.php" > 
<?php 

$db = new mysqli("localhost", "root", "", "eventdb"); 

$query = "SELECT customer.companyName, customer.contactName,eventinfo.eventTitle,boothAlias,date, testbook.bstatus, testbook.username, bookingID from eventinfo, testbook, customer where testbook.username=customer.username AND testbook.eventID=eventinfo.eventID"; 

$result = $db->query($query, MYSQLI_STORE_RESULT); 

$o = '<table id="myTable" class="tablesorter" width="660px"><thead><tr><th>Company Name</th><th>Contact Name</th><th>Event</th><th>Booth</th><th>Date</th><th>Status</th></tr></thead><tbody>'; 

while(list($companyName, $contactName, $eventTitle, $boothAlias, $date, $bstatus,$bookingID) = $result->fetch_row()) { 

if($bstatus==0){ 
    $status="Pending"; 
}else if($bstatus==1){ 
    $status="Successful"; 
}else{ 
    $status="Reject"; 
} 


$o .= '<tr><td width="120px">'.$companyName.'</td><td width="120px">'.$contactName.'</td><td width="180px">'.$eventTitle.'</td><td width="70px">'.$boothAlias.'</td><td width="170px">'.$date.'</td><td width="70">'.$status.'</td><td width="100"><input width="100px" name="APPROVED" type="submit" id="APPROVED" value="Approve"> <input width="100px" name="REJECT" type="submit" id="REJECT" value="Reject"></td></tr>'; 
} 

$o .= '</tbody></table>'; 

echo $o; 
?> 
</form> 
</body> 

approve_booking.php

<?php 

mysql_connect("localhost", "root", "") or die (mysql_error()); 
mysql_select_db("eventdb") or die (mysql_error()); 

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

     $query2 = "UPDATE testbook SET bstatus ='0' WHERE bookingID='$_POST[booking]'"; 
      $result2 = @mysql_query($query2); 
    } 


if (isset($_POST['REJECT'])) 
    { 
     $query3 = "UPDATE testbook SET bstatus ='2' WHERE bookingID='$_POST[booking]'"; 
      $result3 = @mysql_query($query3); 

    } 



?> 

http://i.stack.imgur.com/0KGLB.jpg

+0

我認爲你沒有通過你的預約ID ..使用隱藏變量傳遞它... – Rikesh

+1

永遠不要將客戶端數據直接插入到SQL語句中。閱讀:http://en.wikipedia.org/wiki/SQL_injection。 客戶端數據必須始終視爲不可信。 改用Prepared Statements:http://de3.php.net/manual/en/mysqli-stmt.prepare.php – Zeemee

回答

0

如果預訂的ID是你的汽車increament /主鍵,所以它僅更新單行,但如果它不是那麼在WHERE條件中使用您的主鍵,以便它只能找到並更新1行。

0

這可能會導致您的問題。

snip 
     $query3 = "UPDATE testbook SET bstatus ='2' WHERE bookingID='$_POST[booking]'"; 
/snip 

它改成這樣:

snip 
     $query3 = "UPDATE testbook SET bstatus ='2' WHERE bookingID='{$_POST['booking']}'"; 
snip 

注意,直接呼應後的變量爲查詢字符串像這些是非常不安全的。我建議在將變量放入之前首先檢查變量。

無論如何,你的問題是變量的值$_POST['booking']可能沒有被傳遞到查詢中。它通過了字符串$_POST['booking']

1

嗨,我沒有看到這種形式的任何表單字段。 創建一個隱藏字段「預訂」的名字

<input type="text" name="booking" value="<?php echo $bookingID; ?>" > 
1

你傳遞bookingID到PHP?

<input type="hidden" name="booking" id="booking"> 

添加onclick功能,既bottons選定行的bookingid保存到一個隱藏的輸入。

onclick=$(\"#booking\").val('.$bookingID .');