簡體   English   中英

php到mysql插入腳本中的特殊字符替換

[英]special characters replacement in a php to mysql insert script

很抱歉,如果以前有人問過這個問題,但我不了解我使用google找到的大多數內容。 我正在學習如何執行所有操作,因此請耐心等待。

我已經創建了一個有效的“ INSERT” php腳本,可以將數據插入到mysql 5.x數據庫中,它可以正常工作,但是我所遇到的問題是,如果用戶在腳本的字段中輸入帶有“或”的單詞我向用戶吐出“插入新記錄時出錯”,我需要知道如何使腳本在嘗試將信息插入數據庫之前自動將其替換為\\。

我現在所擁有的是...

<?php

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

include('../connect/connect-mysql.php');

$Colorist = $_POST['Colorist'];
$Active = $_POST['Active'];

$sqlinsert = "INSERT INTO colorist (Colorist, Active) VALUES ('$Colorist', '$Active')";

if (!mysqli_query($dbcon, $sqlinsert)) {
    die('error inserting new record');
    }//end of nested if statement
$newrecord = "New record added";

} //end of main if


?>
<html>
<head>
</head>
<body>
<form method="post" action="insertcolorist.php">
<input type="hidden" name="submitted" value="true" />
<fieldset>
<legend>New Colorist Data</legend>
<table border="1" width="100%" style="border-collapse: collapse">
<tr><th colspan="2"><font face="Verdana" size="2">Colorist Data</font></th></tr>
<tr><th><font face="Verdana" size="1"><label>Colorist: </label></font></th><td><font size="1" face="Verdana"><input type="text" size="150" name="Colorist" /></font></td></tr>
<tr><th><font face="Verdana" size="1"><label>Is the Colorist Active: </label></font></th><td><font size="1" face="Verdana"><select size="1" name="Active"><option value="">Select...</option><option value="Yes">Yes</option><option value="No">No</option></option></select></font></td></tr>
</table>
</fieldset>
<br>
<input type="submit" value="add new colorist" />
</form>
<?php
echo $newrecord // New record added statement added at the top
?>

有一個用於清除輸入字符串的mysqli函數。 就像使用一樣簡單

$Colorist = mysqli_real_escape_string($_POST['Colorist']);
$Active = mysqli_real_escape_string($_POST['Active']);

對於這種最常見的問題,這應該足夠了

暫無
暫無

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

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