数据未使用 php 插入 sqlite3 数据库
Posted
技术标签:
【中文标题】数据未使用 php 插入 sqlite3 数据库【英文标题】:Data not inserting in sqlite3 database using php 【发布时间】:2021-08-05 10:17:57 【问题描述】:大家好,我有一个名为 signup.db 的 sqlite 数据库和一个注册表,我有一个注册页面的 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);
-
在 $sql 字符串中
$pass 应该是 $password
$sql = "INSERT INTO Signup (First, Last, Email, Password, Male, Female, Dateofb)
VALUES ('$first', '$last',
'$email', ******'$pass'*******, '$male', '$female', '$date');";
我为你创建了沙盒。工作正常。您可以将此代码复制到单独的 test.php 文件中并尝试运行它
查看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();
?>
【讨论】:
还是没有插入数据 编辑了答案。你能试试吗?如果有错误,请告诉我 没有错误只是信息没有插入 要么插入数据,要么报错。除非您分享完整的代码和错误,否则我们无法为您提供帮助 兄弟我告诉你没有错误,让我试试你发送的新代码以上是关于数据未使用 php 插入 sqlite3 数据库的主要内容,如果未能解决你的问题,请参考以下文章