从php插入到SQL时要保持反斜杠

时间:2020-03-19 09:19:35

标签: php mysqli mysql-real-escape-string

我是新来的。 我正在尝试从php进行INSERT查询,并且该字符串包含反斜杠。它是乳胶代码,可能在各处包含几个。

我尝试过QUOTE,但没有成功(它只会跳过反斜杠,并且在进行过多调整后设法使该代码无法正常工作),所以当我继续前进时,目前我无法重现工作代码进行不同的尝试。

str_replace(将反斜杠替换为字符串“ CHR(92)”,希望一旦传递到数据库后将其解释为反斜杠,但只打印了CHR(92))。

$str1 = "\\";
$str2 = "CHR(92)";

$newStudentAnswer = str_replace($str1,$str2,$studentAnswer);

//connect to the data base
$link = connect_DataBase();

// Attempt insert query execution
$sql = "INSERT INTO QuestionInstances (problemType, studentAnswer) VALUES
            (1,'$newStudentAnswer')";
check_Insert($link,$sql);

// Close connection
mysqli_close($link);

这是我最后一次尝试,是从类似问题(SQL server - Insert string record containing backslash)中“适应”的

$studentAnswer = $_POST['text'];
$newStudentAnswer = @$studentAnswer;

echo "preview =" . $newStudentAnswer;

//connect to the data base
$link = connect_DataBase();

// Attempt insert query execution
$sql = "INSERT INTO QuestionInstances (problemType, studentAnswer) VALUES
            (1,'$newStudentAnswer')";
check_Insert($link,$sql);

// Close connection
mysqli_close($link);

到目前为止,我已经成功上传了没有反斜杠的字符串。 现在,由于它们包含乳胶代码,因此必不可少。

0 个答案:

没有答案