2017-07-25 58 views
0

我有一個數據庫,其中包含一些顯示在網頁上的數據行。現在,我正在尋找一種方法將mysql中的每個「ID」字段鏈接到一個按鈕,以便單擊該按鈕時,將運行一個php腳本,該腳本將刪除與該ID關聯的一行mysql信息。如何將mysql'id'鏈接到html元素

我知道這是不正確的,但我認爲它接近。只是不知道id標籤中的php部分。幫幫我?

<form action="remove.php" method="post"> 
    <input type="submit" value="Remove Entry" id="<?php $row['id'] ?>" /> 
</form> 

我是否在正確的道路上? 會remove.php看起來像...

<?php 

$conn = mysqli_connect($servername, $username, $password, $dbname); 
// Check connection 
if($conn === false){ 
    die("ERROR: Could not connect. " . mysqli_connect_error()); 
} 

$sql = "DELETE from newcars (stock, year, make, model, trim) 
     WHERE ('$_POST[id] = $row[id]'); 

if(mysqli_query($conn, $sql)){ 
    echo "Records deleted successfully."; 
} 

else { 
    echo "ERROR: Could not execute $sql. " . mysqli_error($link); 
} 

mysql_close($conn) 
?> 

任何幫助將不勝感激。 謝謝!

+4

噢,爲什麼你在其他地方使用'mysql_close'而不是'mysqli_ *'? **請**,不要使用'mysql_ *'函數來獲取新代碼。他們不再被維護,社區已經開始[棄用過程](http://news.php.net/php.internals/53799),並且'mysql_ *'函數已經在PHP 7中被正式刪除。相反,你應該瞭解[已準備好的語句](https://en.wikipedia.org/wiki/Prepared_statement)並使用「PDO」或「mysqli_ *」。如果你不能決定,[這篇文章將有助於選擇你最好的選擇](http://php.net/manual/en/mysqlinfo.api.choosing.php)。 – GrumpyCrouton

+3

這是你的實際代碼?那裏有一些錯誤。 – WheatBeak

+1

**警告**:使用'mysqli'時,應該使用[參數化查詢](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php)和['bind_param']( http://php.net/manual/en/mysqli-stmt.bind-param.php)將用戶數據添加到您的查詢中。 **不要**使用字符串插值或連接來完成此操作,因爲您創建了嚴重的[SQL注入漏洞](http://bobby-tables.com/)。 **不要**將'$ _POST','$ _GET'或**任何**用戶數據直接放入查詢中,如果有人試圖利用您的錯誤,這可能會非常有害。 – tadman

回答

3

HTML:

<form action="remove.php" method="post"> 
    <input type="hidden" name="id" value="<?php echo (int)$row['id']; ?>"> 
    <input type="submit" value="Remove Entry" /> 
</form> 

您想通過ID表單元素,不會與提交按鈕。

PHP看起來像這樣 - 這是比您的原始代碼更安全,因爲它使用預準備語句。

<?php 

$conn = mysqli_connect($servername, $username, $password, $dbname); 
// Check connection 
if($conn === false) { 
    die("ERROR: Could not connect. " . mysqli_connect_error()); 
} 

$stmt = $conn->prepare("DELETE FROM newcars WHERE id = ?"); 
// prepare() can fail because of syntax errors, missing privileges, .... 
if(false === $stmt) { 
    // and since all the following operations need a valid/ready statement object 
    // it doesn't make sense to go on 
    // you might want to use a more sophisticated mechanism than die() 
    // but's it's only an example 
    die('prepare() failed: ' . htmlspecialchars($mysqli->error)); 
} 

$rc = $stmt->bind_param('i', $_POST['id']); 
// bind_param() can fail because the number of parameter doesn't match the placeholders in the statement 
// or there's a type conflict(?), or .... 
if(false === $rc) { 
    // again execute() is useless if you can't bind the parameters. Bail out somehow. 
    die('bind_param() failed: ' . htmlspecialchars($stmt->error)); 
} 

$rc = $stmt->execute(); 
// execute() can fail for various reasons. And may it be as stupid as someone tripping over the network cable 
// 2006 "server gone away" is always an option 
if(false === $rc) { 
    die('execute() failed: ' . htmlspecialchars($stmt->error)); 
} 

$stmt->close(); 

//redirect page back to view page 
?> 
+0

'htmlspecialchars'很好,這是徹底的。 – tadman

+0

@tadman這是諷刺嗎?很難說清楚文字:P – GrumpyCrouton

+0

不,我欣賞它。很少有人關注這一點。 XSS是一個嚴重的問題,我還沒有精力去戰鬥,忙於SQL和蹩腳的登錄系統。 – tadman

1

如果你希望你的ID來發布,它應該是這樣的:

<form action="remove.php" method="post"> 
    <input type="hidden" name="id" value="<?php echo (int)$row['id']; ?>"> 
    <input type="submit" value="Remove Entry" /> 
</form> 

帖子名爲id和值$row['id']的隱藏字段。

你應該照顧上面的註釋,以避免在你的php中注入mysql。

+1

好的開始,但PHP代碼中仍然存在很多問題。 – WheatBeak

+0

@WheatBeak是的,但問題只是「如何發送身份證」。 – steven

相關問題