大家好,你可以看到我已經把因爲 我嘗試連接和信息寫入到數據庫中是非常基本和簡單的整個代碼,但我不明白我我做錯了嗎? 我想讓用戶通過填寫表單並在用戶給出他的信息後將信息放入數據庫中,我想將它放入數據庫中。數據庫本身是生成的,並且生成表和行。無法連接,並寫入mysql數據庫
<!DOCTYPE HTML>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
// define variables and set to empty values
$nameErr = $emailErr = $genderErr = $phoneErr = $cityErr = $countyErr = $adressErr = "";
$name = $email = $gender = $phone = $instagram = $facebook = $city = $country = $adress = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name1"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name1"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}
if (empty($_POST["phone"])) {
$phone = "Phone number is required";
} else {
$phone = test_input($_POST["phone"]);
}
if (empty($_POST["gender"])) {
$genderErr = "Gender is required";
} else {
$gender = test_input($_POST["gender"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<h2>Amsterdam event attending</h2>
<p><span class="error">* required field.</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>, <">
Name:
<input type="text" name="name1" value="<?php echo $name;?>">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
Adress:
<input type="text" name="adress" value="<?php echo $adress;?>">
<span class="error">*</span>
<br><br>
City:
<input type="text" name="city" value="<?php echo $city;?>">
<span class="error">*</span>
<br><br>
E-mail:
<input type="text" name="email" value="<?php echo $email;?>">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>
Instagram:
<input type="text" name="insta" value="">
<br><br>
Facebook:
<input type="text" name="face" value="">
<br><br>
Phone number:
<input type="text" name="phone" value="<?php echo $phone;?>">
<span class="error">* <?php echo $phoneErr;?></span>
<br><br>
Gender:
<input type="radio" name="gender" <?php if (isset($gender) && $gender=="female") echo "checked";?> value="female">Female
<input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?> value="male">Male
<span class="error">* <?php echo $genderErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if(isset($_POST['submit'])){
}
$servername = 'localhost';
$username = 'root';
$password = '1234';
$database = 'amsterdam';
$conn = mysqli_connect($servername, $username, $password, $database);
if (!$conn) {
die("Connection failed: " . mysqli_connect_errno());
}
$sql = "INSERT INTO inschrijvingen (name, adress, city, email, insta, face, phone, gender);
VALUES ($_POST[name1], $_POST[adress], $_POST[city], $_POST[email], $_POST[insta], $_POST[face], $_POST[phone], $_POST[gender])";
mysql_close($conn);
?>
</body>
</html>
是什麼error_log中說什麼? – Stuart