gpt4 book ai didi

php - 如何删除SQL注入(inject)中手动添加的反斜杠?

转载 作者:行者123 更新时间:2023-11-29 18:25:51 31 4
gpt4 key购买 nike

我正在使用 MySQL 和 PHP 来管理我网站上的数据。

我有一个以 TeX 格式输入的段落,其中有很多数学公式。我想插入我的数据库并在其他地方检索相同的内容。

在检索时,我使用 mathjax 来显示数学部分。问题出在插入数据时。

当我插入数据时

$\int f d\mu $ ( Note that this '\' is required to run the math formula)

我获取插入的数据为

$\\int f d \\mu $

有两个反斜杠而不是一个。

如何通过手动添加反斜杠(必填)来防止每次添加反斜杠。

stripslashes() 有帮助或有其他解决方案吗?

提前致谢。

<form action="" method="POST">



<input type="hidden" name="id" value=<?php echo $print->Id; ?> >

<table class="speaker-abstract" style="width:49%; float:left;">

<tr>

<th> Update the Abstract Details Here </th>
</tr>

<tr>
<td>
<label> Title of the Talk </label>
<input style="width:95%" type="text" name="title1" value="<?php echo $print->title_of_the_talk1; ?>">
</td>


</tr>

<tr>
<td>
<label> Abstract of the Talk (Type in TeX format) </label>

<textarea style="width:95%" name="abstract1" rows="10" ><?php echo $print->Abstract1; ?></textarea>

</td>

</tr>

</table>

<table class="speaker-abstract" style="width:49%; float:left;">
<tr>

<th> If any other, update the Abstract Details Here </th>
</tr>

<tr>
<td>
<label> Title of the Talk </label>
<input style="width:95%" type="text" name="title2" value="<?php echo $print->title_of_the_talk2; ?>">
</td>

</tr>

<tr>
<td>
<label> Abstract of the Talk (Type in TeX format) </label>
<textarea style="width:95%" name="abstract2" rows="10" > <?php echo $print->Abstract2; ?> </textarea>

</td>

</tr>

</table>

<input style="float:right;" type="submit" name="update_abstract" value="Submit/Update">

</form>

提交表单并显示数据的 php 代码如下。

<?php

$success_msg="Updated Successfull !!!";
$search= "Search the Speaker Here";

if (isset($_POST['update_abstract'])){
$id=$_POST['id'];
$title1=$_POST['title1'];
$title2=$_POST['title2'];
$abstract1=$_POST['abstract1'];
$abstract2=$_POST['abstract2'];

$update=$wpdb->update(
'Invited_Speakers_auto',
array(
'title_of_the_talk1' => $title1,
'title_of_the_talk2' => $title2,
'abstract1' => $abstract1,
'abstract2' => $abstract2

),
array( 'id' => $id )

);
if ($update==true){

echo $success_msg.$abstract1;

}else
{
$success_msg="There is an error in the update";
}

}
?>

PS:

  1. 请注意,我需要用户插入的斜杠,我只是删除了 SQL 插入时自动插入的斜杠。

  2. stipslashes() 帮助我删除输出中添加的斜杠。

但我想在插入时删除反斜杠。

最佳答案

您可以将 MySqli 或 PDO 与预准备语句一起使用。顺便说一下,这是最著名的预防 SQL 注入(inject)的方法。 Learn more about Sql Injection Prevention from this fantastic answer!

以下代码成功将字符串插入 MySql 数据库并从中读取。希望对您有帮助!

 <?php
$_user = 'root';
$_password= 'root';
$_db = 'localtest';
$_host = 'localhost';
$_port = 3306;
$con = new mysqli($_host, $_user, $_password, $_db) or die(mysql_error);

$new = '$\int f d\mu $';

//insert into mysql table
$sql="INSERT INTO test (math) VALUES (?); ";
$stmt = $con-> prepare($sql);
$stmt -> bind_param("s", $new);
$stmt -> execute();

// Reading From DB
$id = 1;
$sql="SELECT math FROM test WHERE id = ?";
$stmt = $con->prepare($sql);
$stmt->bind_param("i", $id);
$stmt -> execute();
$stmt->store_result();
/* Get the number of rows */
$num_of_rows = $stmt->num_rows;

/* Bind the result to variables */
$stmt->bind_result($new);

if($num_of_rows < 1){
echo "Can't find any record!";
mysqli_close($con);
exit();
}
while ($stmt->fetch())
{
echo $new . PHP_EOL;

/* free results */
$stmt->free_result();

}
mysqli_close($con);
exit();
?>

更新

如果您无法使用 PDO 或 MySqli,那么 mysql_real_escape_string 可以帮助您正确转义特殊字符。

您还可以尝试在插入数据库之前使用htmlentities,然后在检索时使用html_entity_decode

关于php - 如何删除SQL注入(inject)中手动添加的反斜杠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46202635/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com