有一段时间我一直试图弄清楚php和mysql表单以及它们是如何工作的。此时我停了下来,因为我的代码不会将数据插入到mysql数据库中。
在我的脚本中,我收到错误
"抱歉,发生了错误!"
我的代码:
<?php
session_start();
if(isset($_SESSION['users'])!="")
{
}
include('../System/config.php');
if(isset($_POST['btn-signup'])){
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
$region = $_POST['region'];
$country = $_POST['country'];
$state = $_POST['state'];
$city = $_POST['city'];
$name = $_POST['name'];
$surname = $_POST['surname'];
$homepage = $_POST['homepage'];
if(mysql_query("INSERT INTO users(Username, Password, Email, Region, Country, State, City, Name, Surname, Homepage) VALUES('$username', '$password', '$email', '$region', '$country', '$state', '$city', '$name', '$surname', '$homepage')"))
{
?>
<script>alert('You have sucesfully registred your account!');</script>
<?php
}
else
{
?>
<script>alert('Sorry, there was an error!');</script>
<?php
}
}
?>
<!DOCTYPE html>
<head>
<style>
</style>
</head>
<body>
<center>
<div id="login-form">
<form method="post">
<table align="center" width="30%" border="0">
<tr>
<td>Username<input type="text" name="username" placeholder="Username" required /></td>
</tr>
<tr>
<td>Password<input type="password" name="password" placeholder="Password" required /></td>
</tr>
<tr>
<td>Email<input type="email" name="email" placeholder="Email" required /></td>
</tr>
<tr>
<td>
Region<select name="region" required><option value=''>Select one</option>
<?php
include('/../System/config.php');
try {
$conn = new PDO("mysql:host=$servername;dbname=$database", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT Region_ID, Region_Name FROM Regions ORDER BY Region_Name");
$stmt->execute();
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach($stmt as $v) {
echo "<option value='" . $v['Region_ID'] ."'>" . $v['Region_Name'] ."</option>";
}
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
$conn = null;
?>
</td>
</tr>
<tr>
<td>
Country<select name="country" required><option value=''>Select one</option>
<?php
include('/../System/config.php');
try {
$conn = new PDO("mysql:host=$servername;dbname=$database", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT Country_ID, Country_Name FROM Countries ORDER BY Country_Name");
$stmt->execute();
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach($stmt as $v) {
echo "<option value='" . $v['Country_ID'] ."'>" . $v['Country_Name'] ."</option>";
}
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
$conn = null;
?>
</td>
</tr>
<tr>
<td>
State<select name="state" required><option value=''>Select one</option>
<?php
include('/../System/config.php');
try {
$conn = new PDO("mysql:host=$servername;dbname=$database", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT State_ID, State_Name FROM States ORDER BY State_Name");
$stmt->execute();
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach($stmt as $v) {
echo "<option value='" . $v['State_ID'] ."'>" . $v['State_Name'] ."</option>";
}
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
$conn = null;
?>
</td>
</tr>
<tr>
<td>
City<select name="city" required><option value=''>Select one</option>
<?php
include('/../System/config.php');
try {
$conn = new PDO("mysql:host=$servername;dbname=$database", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT City_ID, City_Name FROM Cities ORDER BY City_Name");
$stmt->execute();
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach($stmt as $v) {
echo "<option value='" . $v['City_ID'] ."'>" . $v['City_Name'] ."</option>";
}
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
$conn = null;
?>
</td>
</tr>
<tr>
<td>Name<input type="text" name="name" placeholder="Name" required /></td>
</tr>
<tr>
<td>Surname<input type="text" name="surname" placeholder="Surname" required /></td>
</tr>
<tr>
<td>Homepage<input type="text" name="homepage" placeholder="Homepage" required /></td>
</tr>
<tr>
<td><button type="submit" name="btn-signup">Register Account</button></td>
</tr>
<tr>
<td><a href="users_login.php">Login</a></td>
</tr>
</table>
</form>
</div>
</center>
</body>
</html>