gpt4 book ai didi

mysql - PHP/MySQL - 转义字符串的最佳使用和实践

转载 作者:可可西里 更新时间:2023-11-01 06:32:19 26 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Best way to prevent SQL Injection in PHP

进行查询时转义字符串的最佳方法是什么?mysql_real_escape_string() 看起来不错,但我不知道如何正确使用它。

这段代码是否正确地完成了工作?

<?php
/* Let's say that the user types "'#""#''"\{(})#&/\€ in a textfield */
$newStr = mysql_real_escape_string($str);
$query = "INSERT INTO table username VALUES ($str)";
mysql_query($query);
?>

编辑:

现在我有了这段代码:

      $email = $_POST['email'];
$displayName = $_POST['displayName'];
$pass = $_POST['pass1'];

$email = mysqli_real_escape_string($link, $email);
$displayName = mysqli_real_escape_string($link, $displayName);
$pass = mysqli_real_escape_string($link, $pass);

$insert = "INSERT INTO profiles (email, displayName, password)
VALUES ('$email', '$displayName', md5('$pass'))";
mysqli_query($link, $insert)
or die(mysqli_error($link));

但是我得到这个错误:您的 SQL 语法有误;检查与您的 MySQL 服务器版本对应的手册,了解在第 1 行的 '!"#!#^!"#!"#!"#^'''''' 附近使用的正确语法

如果用户输入:'**!"#!#^!"#!"*#!"#^''''

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