2016-02-10 67 views
-1

嗨,我已經創建了一個刪除頁面,當我剛剛提交表單並且URL爲http://localhost/delete-session.php時無法工作,但是一旦我將URL更改爲http://localhost/delete-session.php?id=1它可行,我缺少什麼在我的代碼中讓它起作用?刪除頁面PHP MYSQL

<h1>Delete Page</h1> 

<h3>Enter the booking number of the session you would like to delete!</h3> 
<form action ="delete-session.php" method="post"> 

Booking ID:(Refer To Database):<input type="text" name="booking"> 

這是PHP

if(isset($_GET['booking'])){ 
    $id=$_GET['booking'];  
    if(!is_numeric($id)){ 
     echo "sorry, there appears to have been an error."; 
     exit; 
    } 
} else { 
    echo "sorry, there appears to have been an error."; 
    exit; 
} 

$servername = "localhost"; 
$username = "root"; 
$password = ""; 
$dbname = "olympics"; 

$conn = mysqli_connect($servername, $username, $password, $dbname); 

$id=$_GET['id']; 
if(!is_numeric($id)){ 
    echo "Sorry, there is an error"; 
    exit; 
} 

if (!$conn) { 
    die("Connection failed: " . mysqli_connect_error()); 
} 

$sql="DELETE from olympiics where booking='$id'"; 
echo $sql; 

if (mysqli_query($conn, $sql)) { 
    echo "Record deleted successfully"; 
} else { 
    echo "Error deleting record: " . mysqli_error($conn); 
} 

mysqli_close($conn); 

回答

-1

變化形式方法來獲得,或使用$ _REQUEST而不是$ _GET

0

我會採取裂縫在這個。

我猜這是因爲當你去http://localhost/delete-session.php?id=1您通過GET傳遞id=1,所以當你在你的代碼檢索獲取輸入它$id=1成功,但在你的HTML表單是通過POST發送。

作爲使用修復嘗試$id=$_POST['booking'];

-1

看來你要發送的值由POST的ID,而是通過GET獲得:

$id=$_GET['booking']; 

它應該是:

$id=$_POST['booking']; 

檢查一下。

編輯

我寫的價值觀錯了,固定

更多信息

GET VS POSThttp://www.w3schools.com/tags/ref_httpmethods.asp

0

臺架試驗你的代碼。

它開始得到$id=$_GET['booking'];,它不存在,因爲您已在<form>標記中設置了method="post"

所以使用$id=$_POST['booking'];

再後來就它$id=$_GET['id'];覆蓋值你已經嘗試從上面得到。

這可以解釋爲什麼它需要http://localhost/delete-session.php?id=1額外的ID放慢參數爲使用查詢字符串來發送數據將在$_GET['id']陣列發送id參數,我不明白你爲什麼會想無論如何要做到這一點,因爲它已經完成在你的代碼通過獲取值這個ID從$id=$_POST['booking']

頂這也使得代碼,以便更容易閱讀和更重要的是調試如果採用縮進標準在腳本中像下面。

試試這個尺寸,不添加id=1的查詢字符串

if(isset($_POST['booking'])){ 
    $id=$_POST['booking'];  
    if(!is_numeric($id)){ 
     echo "sorry, there appears to have been an error. Booking must be numeric"; 
     exit; 
    } 
} else { 
    echo "sorry, there appears to have been an error."; 
    exit; 
} 

$servername = "localhost"; 
$username = "root"; 
$password = ""; 
$dbname = "olympics"; 

$conn = mysqli_connect($servername, $username, $password, $dbname); 
if (!$conn) { 
    die("Connection failed: " . mysqli_connect_error()); 
} 

$sql="DELETE from olympiics where booking='$id'"; 

$res = mysqli_query($conn, $sql); 
if ($res !== FALSE) { 
    echo "Record deleted successfully"; 
} else { 
    echo "Error deleting record: " . mysqli_error($conn); 
} 

mysqli_close($conn); 
?> 

當您使用mysqli擴展,你也應該使用參數化查詢防止SQL注入。

if(isset($_POST['booking'])){ 
    $id=$_POST['booking'];  
    if(!is_numeric($id)){ 
     echo "sorry, there appears to have been an error. Booking must be numeric"; 
     exit; 
    } 
} else { 
    echo "sorry, there appears to have been an error."; 
    exit; 
} 

$servername = "localhost"; 
$username = "root"; 
$password = ""; 
$dbname = "olympics"; 

$conn = mysqli_connect($servername, $username, $password, $dbname); 
if (!$conn) { 
    die("Connection failed: " . mysqli_connect_error()); 
} 

$sql="DELETE from olympiics where booking=?"; 

$stmt = mysqli_prepare($conn, $sql); 
if ($stmt === FALSE) { 
    echo mysqli_error($conn); 
    exit; 
} 
mysqli_stmt_bind_param($stmt, 'i', $id); 


$res = mysqli_stmt_execute($stmt); 
if ($res !== FALSE) { 
    echo "Record deleted successfully"; 
} else { 
    echo "Error deleting record: " . mysqli_error($conn); 
} 

mysqli_close($conn); 
?> 
+0

太感謝你了哥們你是一個真正的生命的救星!我如何選擇這個作爲幫助他人的答案? – cpprr

+0

以下是官方說明http://stackoverflow.com/help/someone-answers – RiggsFolly