数据未使用 php 插入到 sqlite3 数据库中

Data not inserting in sqlite3 database using php

大家好,我有一个名为 signup.db 的 sqlite 数据库和一个注册 table,我有一个注册页面的 php 代码,但我们没有插入任何数据在提交时,即使点击提交我也没有收到 Amy 错误 *不要介意 SQL 注入 这只是测试 我将在制作下一个项目时使用 SQL 准备好的语句 代码

<?php 
if(isset($_POST['submit'])){
//connection to sqlite3 database
$dir = 'sqlite: sign.db';
 $db = new PDO($dir) or die ("Unable to open");
//select table

//saving data
$email = $_POST["Email"];
$first = $_POST["First"];
$last = $_POST["Last"];
$password = $_POST["Password"];
$male = $_POST["Male"];
$female = $_POST["Female"];
$date = $_POST["Dateofb"];
$sql = "INSERT INTO Signup (First, Last, Email, Password, Male, Female, Dateofb) VALUES ('$first', '$last', '$email', '$pass', '$male', '$female', '$date');";
$sql->execute();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="index.css"  >
<script src="index.js" ></script>
<title>Survey</title>
</head>
<body>
<form action="" method="post"  >
<div class="container" >
<div class="form" >

<input type="email" class="first"  id="email" placeholder="Email" required="required">
<input type="text" class="second"  id="first" placeholder="First name" required="required">
<input type="text" class="last"  id="last" placeholder="Last name" required="required">
<input type="password" class="pass"  id="pass" placeholder="Password" required="required">

<div class="day" >

<p class="bd" >Birthday Date:</p>

<input type="date" class="date" id="date"  >

</div>
<div>
<div class="malee" >
<input type="checkbox" class="male" id="male"  >
<p class="mal" >Male</p>
</div>
<div class="femalee" >
<input type="checkbox" class="female" id="female"  >
<p class="fem" >Female</p>
</div>
</div>
<div >
<input class="submit" id="submit" type="submit"   >
</div>
<div class="acc" >
already have account <a href="#" >Login</a>
</div></div>
</div>
</form>
</body>
</html>
        

(https://i.stack.imgur.com/vYkug.jpg)

您的代码有两个问题:

  1. 您应该在数据库连接上查询

$db->exec($sql);

  1. 在 $sql 字符串中

$pass should be $password

    $sql = "INSERT INTO Signup (First, Last, Email, Password, Male, Female, Dateofb) 
VALUES ('$first', '$last', 
'$email', ******'$pass'*******, '$male', '$female', '$date');";

我为你创建了沙盒。工作正常。您可以将此代码复制到单独的 test.php 文件中并尝试 运行 it

勾选这个sandbox

<?php 

try {
  $conn = new PDO("sqlite: sign.db");
  // set the PDO error mode to exception
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $email = "test2";
    $first =  "";
    $last =  "";
    $password =  "";
    $male =  "";
    $female =  "";
    $date =  "";
    $sql = "CREATE TABLE IF NOT EXISTS Signup (
      First text NOT NULL, 
      Last text NOT NULL, 
      Email text NOT NULL, 
      Password text NOT NULL, 
      Male text NOT NULL,
      Female text NOT NULL,
      Dateofb text NOT NULL
    );"; 
    
    $conn->exec($sql);
    
    $sql = "INSERT INTO Signup (First, Last, Email, Password, Male, Female, Dateofb) VALUES ('$first', '$last', '$email', '$password', '$male', '$female', '$date');";

    $conn->exec($sql);
    echo "New record created successfully \n";
    
    $sql = "SELECT * FROM Signup";
    $result = $conn->query($sql);
    while ($row = $result->fetch()) {
        echo $row['Email']."\n";
    }
    
} catch(PDOException $e) {
    echo $sql . "<br>" . $e->getMessage();
}

 ?>