所以我有一个简单的网站,可以将您添加到SQL数据库中。现在我的数据库只包含一个表格,该表格包含用户的firstName
,lastName
和email
(稍后会添加)。但我的主要问题是我不太确定如何建立连接。
我知道我的PHP代码肯定包含很多错误,但是一旦我可以添加用户,这些错误有望得到解决。
我通过XAMPP托管我的sql和apache服务器。
我的jsfiddle:https://jsfiddle.net/2knt74r5/
我的PHP代码:
<?php
/*
IF CITIZEN FORM WAS SUBMITED
*/
if ($_POST["citEnter"]){
$title = $_POST["title"];
$firstName = $_POST["firstName"];
$lastName = $_POST["lastName"];
$email = $_POST["citEmail"];
}
/*
IF ORGANISATION FORM WAS SUBMITED
*/
if ($_POST["orgEnter"]){
$title = $_POST["title"];
$firstName = $_POST["firstName"];
$lastName = $_POST["lastName"];
$email = $_POST["orgEmail"];
}
/*
IF ANONYMOUS FORM WAS SUBMITED
*/
if ($_POST["anonEnter"]){
$title = "";
$firstName = "Anonymous";
$lastName = "";
if ($_POST["anonEmail"])
$email = $_POST["anonEmail"];
else
$email = "";
}
// I know that I need to establish a connection first..
$sql = "INSERT INTO MyGuests (firstname, lastname, email) VALUES ($firstName, $lastName, $email)";
?>
答案 0 :(得分:0)
在php中建立数据库连接你可以使用PDO类。 例如
$connection = new PDO("mysql:host=localhost;dbname=database_name;user=username;password=password");
$sql = "Insert into table xyz(z,b) values('a','b')";
$statement = $connection->prepare($sql);
$statement->execute();