- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在这里,我正在创建注册新用户表单,但我在mysql_real_escape_string()
中遇到了问题.so,我使用了替代方案mysqli_real_escape_string()
我尝试过
mysqli_real_escape_string($conn,$_POST['user_name'])
但没有工作。在这里,我创建了一个单独的 connect.php(用于与服务器和数据库的连接)文件以节省我的时间,并且我已使用
include 'connect.php';
我不确定是否制作单独的 connect.php 文件 mysqli_real_escape_string($conn,$_POST['user_name'])
可以用也可以不用。
<?php
$server='localhost';
$username='root';
$password='';
$db_name='web_forum_db';
if(!$conn=mysqli_connect($server,$username,$password))
{
exit('Error: could not establish database connection');
}
if (!mysqli_select_db($conn,$db_name))
{
exit('Error: could not select the database');
}
mysqli_close($conn);
?>
<小时/>
<?php
include 'connect.php';
include 'header.php';
echo '<h3>Sign up</h3>';
if($_SERVER['REQUEST_METHOD'] !='POST')
{
echo '<form method="POST" action="">
Username:<input type="text" name="user_name"/>
Password: <input type="password" name="user_pass">
Password again:<input type="password" name="user_pass_check">
E-mail:<input type="email" name="user_email">
<input type="submit" value="Add category"/>
</form>';
}
else
{
$errors = array();/*declare the array for later use*/
if(isset($_POST['user_name']))
{
//the user name exists
if (!ctype_alnum($_POST['user_name'])) {
$errors[]='The username can only contain letters and digits.';
}
if(strlen($_POST['user_name'])>30)
{
$errors[]='The username cannot be longer than 30 characters.';
}
}
else
{
$errors[]='The username field must not be empty.';
}
if(isset($_POST['user_pass']))
{
if($_POST['user_pass'] !=$_POST['user_pass_check'])
{
$errors[]='The two passwords did not match.';
}
}
else
{
$errors[]='The password field cannot be empty.';
}
if (!empty($errors))/*check for an empty array, if there are errors, they`re in this array*/ {
echo 'Uh-oh.. a couple of fields are not filled in correctly..';
echo '<ul>';
foreach ($errors as $key => $value)/*walk through the array so all the errors get displayed*/
{
echo '<li>' . $value. '</li>';/*This generates a nice error list*/
}
echo '</ul>';
}
else
{
$sql = "INSERT INTO users(user_name,user_pass,user_email,user_date,user_level) VALUES('".mysqli_real_escape_string($conn,$_POST['user_name'])."',
'".sha1($_POST['user_pass'])."',
'".mysqli_real_escape_string($conn,$_POST['user_email'])."',
NOW(),0)";
$result=mysqli_query($conn,$sql);
if(!$result)
{
//something went wrong, display the error
echo 'Something went wrong while registering. Please try again later.';
//echo mysql_error(); //debugging purposes, uncomment when needed
}
else
{
echo 'Successfully registered. You can now <a href="signin.php">sign in </a> and start posting! :-)';
}
}
}
include 'footer.php';
?>
<小时/>
显示的错误:
Warning: mysqli_real_escape_string(): Couldn't fetch mysqli in C:\xampp\htdocs\web_forum\signup.php on line 56
Warning: sha1() expects parameter 1 to be string, object given in C:\xampp\htdocs\web_forum\signup.php on line 57
Warning: mysqli_real_escape_string(): Couldn't fetch mysqli in C:\xampp\htdocs\web_forum\signup.php on line 58
Warning: mysqli_query(): Couldn't fetch mysqli in C:\xampp\htdocs\web_forum\signup.php on line 60
以下代码位于第 56、57、58 和 60 行:
$sql = "INSERT INTO users(user_name,user_pass,user_email,user_date,user_level) VALUES('".mysqli_real_escape_string($conn,$_POST['user_name'])."',
'".sha1($_POST['user_pass'])."',
'".mysqli_real_escape_string($conn,$_POST['user_email'])."',
NOW(),0)";
$result=mysqli_query($conn,$sql);
最佳答案
您将在建立 mysqli 连接后立即关闭它。
只需从 connect.php
中删除此行:
mysqli_close($conn);
但无论如何,正如 Jens 所说,您应该使用准备好的语句而不是 mysqli_real_escape_query
。
关于php - mysqli_real_escape_string() 中出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40459892/
这个问题在这里已经有了答案: 9年前关闭。 Possible Duplicate: Best way to prevent SQL Injection in PHP 我想知道 sql 注入(injec
我想消除sql注入(inject),我应该使用mysqli_real_escape_string()或者在mysqli中清楚吗? 例如 $nick = mysqli_real_escape_strin
我是 PHP 新手。我试图将一个变量的值插入到 MariaDB 表中,并试图使用 mysqli_real_escape_string 转义“$value”。我的想法来自 here .它向表中插入了一个
我有这个代码: $var = mysqli_real_escape_string($connection,$_POST['var']); $sql = "UPDATE users SET var =
有人有这个解决方案吗?我从今天晚上突然出现蓝屏错误后就遇到了这个问题。我正在使用这个简单的代码在项目中实现 session ,但在那个神秘的重启之后,这个东西表现得很奇怪!!!! 这段代码应该可以工作
我有以下 PHP 函数: public function signup() { $mysql = mysqli_connect(HOSTNAME, USERNAME, PASSWORD,
我在电子邮件地址上使用 mysqli_real_escape_string(),它返回一个空字符串。它可以对任何电子邮件地址执行此操作。 "url", "username"=>"userna
这个问题已经有答案了: PHP parse/syntax errors; and how to solve them (20 个回答) 已关闭 7 年前。 为什么即使我使用 mysqli_real_e
这个问题已经有答案了: mysqli::query(): Couldn't fetch mysqli (4 个回答) 已关闭 6 年前。 在这里,我正在创建注册新用户表单,但我在mysql_real_
所以目前我的代码采用程序格式,因此为了防止自己在 mysqli_real_escape_string 函数中添加“链接”,我创建了一个如下所示的方法: // Used to create a
我正在尝试改造我的一个项目以使用 real_escape_string。我已经阅读了文档、教程和 stackoverflow 相关问题,看起来我做得正确,但显然有些问题。我非常有信心它就是 real_
我在程序 mysqli 中创建了以下脚本,因为我刚刚从旧的 mysql 开始。我不确定查询周围的引号类型是否可以防止注入(inject)。一些用户用 ' 发布示例,另一些用户用 " 发布示例,所以现在
有人知道为什么 mysqli 在某些服务器上不起作用吗? $mysqli = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE); ech
我在 php 中使用 mysqli 扩展来连接数据库。我有一个如此简单的问题。使用 mysqli 而不是 mysql 更好吗?为什么有必要使用 mysqli_real_escape_string ?这
在进行 MySQL INSERT 查询之前,我尝试同时使用 mysqli_real_escape_string 和 trim。我的代码如下: 使用上面的代码我得到“错误”消息,但是如果我删除 mys
如果我不使用 mysql_real_escape_string 函数,代码可以正常工作。但是这个函数没有返回任何东西!我读到问题可能是由于我没有 mysql 连接,但事实并非如此! 请帮忙! 最佳答
所以我忙于 PHP,在我想知道的时候将数据插入 mysql:我看到一些帖子说使用单引号将数据插入数据库是不好的做法。示例之一:Why are VALUES written between quotes
我正在使用 mysqli_real_escape_string 的 OOP 方法转义字符串。我将输入的内容保存到 session 变量中以确保它正确转义。它似乎正确转义,但是当我检查输入数据库的内容时
我在电子邮件地址上使用 mysqli_real_escape_string(),它返回一个空字符串。它对任何电子邮件地址都执行此操作。 "url", "username"=>"usernam
在 mysqli_real_escape_string() 的 PHP 文档中,写道 Caution Security: the default character set The character
我是一名优秀的程序员,十分优秀!