【问题标题】:PHP: Inserting Values from the Form into MySQLPHP:将表单中的值插入 MySQL
【发布时间】:2016-09-18 23:26:07
【问题描述】:

我在终端的mysql 中创建了一个users 表,我正在尝试创建简单的任务:从表单中插入值。这是我的dbConfig file

<?php
$mysqli = new mysqli("localhost", "root", "pass", "testDB");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
?>

这是我的Index.php

<!doctype html>
<html>
 <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="description" content="$1">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link rel="stylesheet" type="text/css" href="style.css">

    <title>test</title>

    <?php
    include_once 'dbConfig.php';
    ?>

</head>
<body>
     <?php
    if(isset($_POST['save'])){
        $sql = "INSERT INTO users (username, password, email)
        VALUES ('".$_POST["username"]."','".$_POST["password"]."','".$_POST["email"]."')";
    }

    ?>

    <form method="post"> 
    <label id="first"> First name:</label><br/>
    <input type="text" name="username"><br/>

    <label id="first">Password</label><br/>
    <input type="password" name="password"><br/>

    <label id="first">Email</label><br/>
    <input type="text" name="email"><br/>

    <button type="submit" name="save">save</button>
    <button type="submit" name="get">get</button>
    </form>

</body>
</html>

点击我的保存按钮后,没有任何反应,数据库仍然是空的。我尝试了echo'ing INSERT 查询,它从表单中获取所有值。在我尝试从终端检查这是否有效后,我登录到我的sql 尝试从用户表中返回所有数据,但我得到了一个空集。

【问题讨论】:

  • 使用函数mysqli_query进行SQL查询
  • 您的代码只是echo正在执行 SQL 查询...
  • 您需要mysqli_query($mysqli, $sql);查询。您目前只是 echoing 查询,基本上只是将其输出到屏幕上。
  • mysqli_query($mysqli, $sql); mysqli_query

标签: php html mysql


【解决方案1】:

以下代码只是声明了一个包含 MySQL 查询的字符串变量:

$sql = "INSERT INTO users (username, password, email)
    VALUES ('".$_POST["username"]."','".$_POST["password"]."','".$_POST["email"]."')";

它不执行查询。为此,您需要使用一些函数,但让我先解释一下其他内容。

永远不要相信用户输入:您不应该将用户输入(例如来自$_GET$_POST 的表单输入)直接附加到您的查询中。有人可以以这种方式小心地操纵输入,从而对您的数据库造成极大的破坏。这就是所谓的 SQL 注入。你可以阅读更多关于它here

为了保护您的脚本免受此类攻击,您必须使用准备好的语句。更多关于准备好的语句here

在您的代码中包含准备好的语句,如下所示:

$sql = "INSERT INTO users (username, password, email)
    VALUES (?,?,?)";

注意? 如何用作值的占位符。接下来你应该使用mysqli_prepare准备声明:

$stmt = $mysqli->prepare($sql);

然后开始将输入变量绑定到准备好的语句:

$stmt->bind_param("sss", $_POST['username'], $_POST['email'], $_POST['password']);

最后执行准备好的语句。 (这是实际插入发生的地方)

$stmt->execute();

注意虽然不是问题的一部分,但我强烈建议您不要以明文形式存储密码。相反,您应该使用password_hash 来存储密码的哈希

【讨论】:

  • 感谢您提供如此详细的答复。我会明确地研究一下,因为这是我第一次尝试 PHP,我只关注功能,但我会在未来尝试改进。
  • @notrealme 如果您刚刚开始,我强烈建议您查看 PDO。它比 mysqli 更容易使用,尤其是使用准备好的语句。
  • 我尝试了这个并且收到一个错误,抱怨 mysqli_prepare 需要 2 个参数。
  • @dimlucas 参数绑定中的“sss”是什么?
  • @A.Volg 's' 代表字符串。如果其中一个参数是数字,我们将使用 'd' 表示小数
【解决方案2】:

你的代码有两个问题。

  1. 在表单中未找到任何操作。
  2. 您还没有执行查询mysqli_query()

dbConfig.php

<?php

$conn=mysqli_connect("localhost","root","password","testDB");

if(!$conn)
{
die("Connection failed: " . mysqli_connect_error());
}

?>

index.php

 include('dbConfig.php');

<!Doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="description" content="$1">
<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet" type="text/css" href="style.css">

<title>test</title>


</head>
<body>

 <?php

  if(isset($_POST['save']))
{
    $sql = "INSERT INTO users (username, password, email)
    VALUES ('".$_POST["username"]."','".$_POST["password"]."','".$_POST["email"]."')";

    $result = mysqli_query($conn,$sql);
}

?>

<form action="index.php" method="post"> 
<label id="first"> First name:</label><br/>
<input type="text" name="username"><br/>

<label id="first">Password</label><br/>
<input type="password" name="password"><br/>

<label id="first">Email</label><br/>
<input type="text" name="email"><br/>

<button type="submit" name="save">save</button>

</form>

</body>
</html>

【讨论】:

    猜你喜欢
    • 2020-09-02
    • 2014-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-07
    • 1970-01-01
    • 2016-07-08
    • 1970-01-01
    相关资源
    最近更新 更多