簡體   English   中英

將用戶提交的撇號存儲在數據庫PHP中

[英]Store user submitted apostrophe in database PHP

該腳本允許用戶在數據庫中存儲單詞:

script: 腳本:

<html>
<body>

<form action="welcome.php" method="post">
Sentance: <input type="text" name="name"><br>
<input type="submit">
</form>

</body>
</html> 

script : 腳本:

<html>
<body>
<?php 
echo $_POST["word"];

mysql_connect("xxxx","xxxx","xxxx");
mysql_select_db("xxxx");
$word1 = $_POST['word'];
mysql_query("INSERT INTO words VALUES ('$word1');");
?>
<br>

</body>
</html> 

問題在於,如果用戶提交了包含撇號的單詞,該單詞將不會存儲在數據庫中。

例如單詞:

hello'world

不會存儲,因為它包含撇號。

如何解決此問題?

哇,您應該清理輸入內容,並且不要使用mysql_query作為過時的東西。

查看mysqli_real_escape_string來轉義這些非常不好的字符。

然后查看mysqliPDO替換mysql_query。

現在擁有的代碼使您可以進行注入。

您的代碼對sql注入很開放。 首先,我強烈建議您停止使用mysql_系列函數,因為它們已被棄用。 如果必須,請進行轉義以避開用戶輸入。 但是,我強烈建議您使用PDO

$dbh = new PDO('mysql:host=localhost,port=3306,username=user,password=pass', 'user', 'pass');
$stmt = $dbh->prepare("INSERT INTO words VALUES (:word)");
$stmt->bindParam(':word', $word);
$word = $_POST['word'];
$stmt->execute();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM