1
所以我无法连接或插入数据到我的数据库中。我正在使用表单来收集我需要的信息。然后我尝试插入到我的数据库中。我不确定问题是什么,但我认为我在连接时遇到了问题。我正在使用两个文件来尝试完成此操作。你如何使用php插入到mysql数据库?
addQuite.html文件
<!DOCTYPE html>
<html>
<head>
<title>AddQuote</title>
<link href="styles.css" type="text/css" rel="stylesheet" />
</head>
<body>
<h2> Add a Quote <h2>
<form action="index.php" method="get">
<div>
Quote:<br>
<textarea rows="6" cols="60" name="quote" id="quote">
</textarea>
<br>
Author: <input type="text" name="author" id="author"/> <br>
<input class = "input2" type="submit" value="Save Quotation"/>
</div>
</form>
</body>
</html>
这是我的index.php文件这是我在哪里尝试连接,并插入到我的数据库
<!DOCTYPE html>
<html>
<head>
<title>Quotes</title>
<link href="styles.css" type="text/css" rel="stylesheet" />
</head>
<body>
<h1> Quotes </h1>
<form action="addQuote.html">
<input class="input1" type="submit" value="Add Quote"/>
</form>
<?php
//connet to server
$db = new PDO("mysql:host=server;dbname=quotes", "root" , "");
//check connections
if($db===false){
die("ERROR: Could not connect. ");
}
//get name and quote
$name = $_GET['author'];
$quote = $_GET['quote'];
//attemp insert query execution
$sql = "INSERT INTO quotations (name, quote, rating) VALUES
('$name', '$quote', 0)";
?>
</body>
</html>
如果您在本地系统中运行代码,请检查您的主机名,通常它是'localhost'。 –
你目前的代码很容易被sql注入。你应该确保这不会通过准备或逃避而发生 –