【问题标题】:php database data savingphp数据库数据保存
【发布时间】:2016-05-15 16:51:19
【问题描述】:

我正在使用 php 制作注册表单,但是当我点击注册按钮时,它会在电子邮件和密码字段中保存 1,1 而不是给定的电子邮件和密码。

  <?php
include("connection.php")
?>
<?php

$email=isset($_POST['email']);
$password=isset($_POST['password']);

if(isset($_POST['register']))
{
    $q= "insert into admin (email, password) values ('$email', '$password')";
    $qr=mysqli_query($con, $q);
    if($qr)
    {
        echo "data added sucessfully";
    }
    else
    {
        die();
    }
}

?>

<!DOCTYPE html>
<html>
<head>
    <title>Log in</title>
</head>
<body>
<form method="post" action="">
<input type="email" name="email">
<br>
<input type="password" name="password">
<br>
<button type="submit" name="register" value="register">register</button>
</form>
</body>
</html>

【问题讨论】:

标签: php mysql database


【解决方案1】:

http://php.net/manual/en/function.isset.php

Isset 函数返回一个布尔值,试试:

if(isset($_POST['email'])){
    $email=$_POST['email'];
}
if(isset($_POST['password'])){
    $password=$_POST['password'];
}

【讨论】:

  • 或者您可以使用三元运算符:$email = (isset($_POST['email']) ? $_POST['email'] : "");,因为如果没有设置所有字段会发生什么? 未定义变量
【解决方案2】:

它在您的数据库中输入1 的原因是因为isset() 用于POST 数组。

旁注:您可能打算使用三元运算符http://php.net/manual/en/language.operators.comparison.php

RTM: http://php.net/manual/en/function.isset.php bool isset ( mixed $var [, mixed $... ] ) 返回一个布尔值。

$email=isset($_POST['email']);
$password=isset($_POST['password']);

您需要删除isset()

$email=$_POST['email'];
$password=$_POST['password'];

然后检查它们是否不为空。

if(!empty($_POST['email']) && !empty($_POST['password']) )

并放在if(isset($_POST['register'])){...}

我不知道你为什么要将代码嵌入到 textarea。
编辑:我看到你在编辑中删除了它。

还要确保您的列类型能够存储字符串而不是整数,并且足够长以容纳所存储的数据。

您当前的代码对SQL injection 开放。使用prepared statements,或PDOprepared statements


密码

我还注意到您可能以纯文本形式存储密码。如果您打算使用它,则不建议这样做。

使用以下方法之一:

其他链接:

关于列长度的重要旁注:

如果您决定使用password_hash() 或 crypt,请务必注意,如果您当前密码列的长度低于 60,则需要将其更改为该长度(或更高)。手册建议长度为 255。

您需要更改列的长度并使用新的哈希值重新开始以使其生效。否则,MySQL 将静默失败。


脚注:

您还应该检查您的查询是否有错误。

错误报告是另一个可以帮助您的方法。

【讨论】:

    【解决方案3】:

    我觉得应该是下面这样的

    $email = isset($email) ? $电子邮件:“” $password = isset($password) ? $密码:“”

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-23
      • 2012-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多