使用pdo时数据不插入数据库

6qfn3psc  于 2021-06-20  发布在  Mysql
关注(0)|答案(3)|浏览(341)

我正在学习pdo,我试着用crud方法。我试图插入数据到数据库使用pdo,但它没有插入。下面是我的代码

<?php 
    $username = $_POST['username'];
$password = password_hash($_POST['password'], PASSWORD_BCRYPT ));
try {
    $query = $connect->prepare("INSERT INTO users(username, password) VALUES(?,?)");
        $query->execute(array($username, $password));
        echo "data";
    }
    catch (PDOException $event) {
        echo $event->getMessage();
    }
    ?>

我把这个索引文件命名为index.php

<?php 
require_once 'db.php';
session_start();
session_regenerate_id();
?>
<!DOCTYPE html>
<html>
<head>
  <title>Sign-Up/Login Form</title>
</head>
<?php
if ($_SERVER['REQUEST_METHOD'] == '$_POST') {
    if (isset($_POST['login'])) {
        require 'login.php';
    }
    elseif (isset($_POST['register'])) {
        require 'register.php';
    }
}
?>
<body>
    <form action="index.php" method="POST">
        <input type="text" name="username">
        <input type="password" name="password">
        <input type="submit" name="register" value="Submit">
    </form>
</body>
</html>

我的db.php看起来像

<?php 
try {
$connect = new PDO('mysql:dbname=pdologin;host=localhost', 'root', '$$$$'); 
$connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (Exception $event) {
    $event->getMessage();

}
?>
wfsdck30

wfsdck301#

问题是你的代码永远不会到达你的 require 脚本( login.php 或者 register.php )因为你的条件是不正确的。
您有: if ($_SERVER['REQUEST_METHOD'] == '$_POST') 应该是的 if ($_SERVER['REQUEST_METHOD'] == 'POST')

jvidinwx

jvidinwx2#

先连接

$connect = mysqli_connect("localhost","root","root","my_db");

然后在执行时删除参数

$query->execute();

试试这个

<?php 
    $connect = mysqli_connect("localhost","root","root","my_db");
    $username = $_POST['username'];
    $password = password_hash($_POST['password'], PASSWORD_BCRYPT );
    try {
        $query = $connect->prepare("INSERT INTO users(username, password) VALUES('$username', '$password')");
        $query->execute();
    }
    catch (PDOException $e) {
        echo $e->getMessage();
    }
    ?>
o7jaxewo

o7jaxewo3#

在学习或编写一些需要连接的小脚本时,您将得到如下结果,从长远来看,将这些东西 Package 到函数中或使用一个小助手或框架可以使这变得简单一些。好主意学习,但它仍然乏味的锅炉板无论多少年你写这东西。

<?php
//db settings that are typically in a config somewhere
$db_servername = "localhost";
$db_username = "username for your database";
$db_password = "password for your database";
$db_name = "your_db_name";        

try {
    $connect = new PDO("mysql:host=$db_servername;dbname=$db_name, $db_username, $db_password");
    // set the PDO error mode to exception
    $connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    //echo "Connected successfully";
 }catch(PDOException $e){
    //echo "Connection failed: " . $e->getMessage();
 }
 $sth = $connect->prepare("INSERT INTO users(username, password) VALUES(:username,:password)");

 $username = $_POST['username'];
 $password = password_hash($_POST['password'], PASSWORD_BCRYPT );

 $sth->bindValue(':username', $username, PDO::PARAM_STR);
 $sth->bindValue(':password', $password, PDO::PARAM_STR);
 $sth->execute();

例如,我的团队现在只编写数据库绑定代码

<?php
//array of ids to insert
$binds['ids'] = array(1,3,4,5,6,7,9,08098);
//Database class is auto included with every script
$success = Database::query('insert into my_table (id) values(:ids)',$binds);

相关问题