HTML 表单未将值传递给 PHP (mysqli_real_escape_string)
Posted
技术标签:
【中文标题】HTML 表单未将值传递给 PHP (mysqli_real_escape_string)【英文标题】:HTML form not passing values to PHP (mysqli_real_escape_string) 【发布时间】:2015-08-31 01:06:55 【问题描述】:<form type="POST" action="includes/login.php">
<input type="email" name="email" placeholder="email" />
<input type="password" name="password" placeholder="parola" />
<input type="submit" value="Login">
</form>
PHP
<?php
require_once 'config.php';
if(isset($_POST['email']))
$email = mysqli_real_escape_string($_POST['email']);
else
echo "Nu ati completat adresa de e-mail. <br />";
if(isset($_POST['password']))
$email = mysqli_real_escape_string($_POST['password']);
else
echo "Nu ati completat parola. <br />";
if(isset($_POST['email']) && ($_POST['password']))
$query = ("SELECT * FROM `users` WHERE password = '$password' AND email = '$email'");
$result = mysqli_query($link, $query);
$row = mysqli_fetch_array($result);
$count_rows = mysqli_num_rows($result);
if ($count_rows == 1)
$_SESSION["login"] = "OK";
header("Location: ../index.php");
else
header("Location: ../login.php");
?>
我尝试从 MySQL 切换到 MySQLi,我确信它与此有关。即使输入有名称,我的表单也没有将值传递给 PHP 脚本。在这里对 *** 进行了一些研究,发现了许多关于表单不传递数据的问题,但通常存在拼写错误或缺少名称,这不是我的情况(我认为)。
(我知道密码还不安全,我会尽快添加一个 SHA256 或其他东西,所以不要紧张)
尝试回显查询,但密码和电子邮件地址应为空白。
SELECT * FROM `users` WHERE password = '' AND email = ''
我也收到此警告:
警告:mysqli_real_escape_string() 需要 2 个参数,1 个在第 4 行的 C:\xampp\htdocs\breloc\includes\login.php 中给出
我脚本中的第 4 行是:
$email = mysqli_real_escape_string($_POST['password']);
【问题讨论】:
如果未打开连接,mysqli_real_escape_string
将不起作用。如果不使用mysql_real_escape_string
,变量是否包含值?为什么你还在用 mysql 而不是 mysqli?:/
所以我需要像为mysqli_query提供的那样提供链接参数吗?例如:mysqli_query($link, $query)
为提交按钮命名并在按钮点击后使用代码
@MateiPanchios,没错!
您已将 $email 变量用于电子邮件和密码
【参考方案1】:
string mysqli_real_escape_string ( mysqli $link , string $escapestr )
从Docs 开始,第一个参数必须是mysqli resource
并且在您的代码中缺少它,并且还要更改
<form type="POST">
进入
<form method="post">
所以你的代码看起来像
mysqli_real_escape_string($link,$_POST['email']);// and been repeated at all those occurences
【讨论】:
投反对票的原因 我认为 【参考方案2】:根据文档http://php.net/manual/de/mysqli.real-escape-string.php 您必须提供 mysqli 资源作为函数的第一个参数。
【讨论】:
【参考方案3】:改变你的form标签
<form type="POST">
到
<form method="POST">
【讨论】:
【参考方案4】:您应该在<form>
标签中使用method
而不是type
,如下所示:
<form method="POST" action="includes/login.php">
【讨论】:
【参考方案5】:-
将
type="post"
更改为method="post"
将数据库连接字符串添加到您的mysqli_real_escape_string
函数。
【讨论】:
【参考方案6】:<form method="POST" action="includes/login.php">
<input type="email" name="email" placeholder="email" />
<input type="password" name="password" placeholder="parola" />
<input type="submit" value="Login" name="submit">
</form>
<?php
require_once 'config.php';
if(isset($_POST['submit']))
if(!empty($_POST[email]))
$email = mysqli_real_escape_string($link,$_POST['email']);
else
echo "Nu ati completat adresa de e-mail. <br />";
if(!empty($_POST['password']))
$password = mysqli_real_escape_string($link,$_POST['password']);
else
echo "Nu ati completat parola. <br />";
if(!empty($_POST['email']) && !empty($_POST['password']))
$query = ("SELECT * FROM `users` WHERE password = '".$password."' AND email = '".$email."'");
$result = mysqli_query($link, $query);
$row = mysqli_fetch_array($result);
$count_rows = mysqli_num_rows($result);
if ($count_rows == 1)
$_SESSION['login'] = "OK";
header("Location: ../index.php");
else
header("Location: ../login.php");
?>
【讨论】:
【参考方案7】:设置'方法'不输入
<form method="POST" action="includes/login.php">
<input type="email" name="email" placeholder="email" />
<input type="password" name="password" placeholder="parola" />
<input type="submit" value="Login">
</form>
不要忘记连接到您的数据库并将该连接传递给您的 mysqli_query 和 mysqli_real_escape_string 函数
<?php
require_once 'config.php';
$con=mysqli_connect("localhost","my_user","my_password","my_db");
if(isset($_POST['email']))
$email = mysqli_real_escape_string($con, $_POST['email']);
else
echo "Nu ati completat adresa de e-mail. <br />";
if(isset($_POST['password']))
$email = mysqli_real_escape_string($con,$_POST['password']);
else
echo "Nu ati completat parola. <br />";
if(isset($_POST['email']) && ($_POST['password']))
$query = ("SELECT * FROM `users` WHERE password = '$password' AND email = '$email'");
$result = mysqli_query($con, $query);
$row = mysqli_fetch_array($result);
$count_rows = mysqli_num_rows($result);
if ($count_rows == 1)
$_SESSION["login"] = "OK";
header("Location: ../index.php");
else
header("Location: ../login.php");
?>
【讨论】:
以上是关于HTML 表单未将值传递给 PHP (mysqli_real_escape_string)的主要内容,如果未能解决你的问题,请参考以下文章
WordPress 中带有 jQuery 的 AJAX:AJAX 调用未将属性值传递给 PHP 函数
将表单值传递给 PHP 包含在 HTML DOM innerHTML 中
Laravel 中的 Ajax Jquery 未将值保存到数据库