[英]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
不會存儲,因為它包含撇號。
如何解決此問題?
您的代碼對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.