我已经尝试过查看类似的问题,但是,我正在试图了解自己的错误。
我的设置:
XAMPP GymPHP(编写代码) localhost上的数据库叫做健身房 数据库中的表称为包含9列的记录
我创建了一个表单,现在正尝试在表单和数据库之间建立连接,以便将数据从表单移动到数据库中。
我的代码:
form.php的:
<html>
<head>
<title>Gym Form</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="Login.php" method="post">
<span>Gym Membership Registration</span><br><br>
<Span>Title: </Span><input type ="text" Value =" " name ="Title" /><br>
<Span>First Name: </Span><input type ="text" Value =" " name ="Fname" /><br>
<Span>Last Name: </Span><input type ="text" Value =" " name ="Lname" /><br><br>
<Span>Gender: </Span><select name ="Gender">
<option value ="Junior">Male</option>
<option value ="Adult">Female</option>
<option value ="Senior">Private</option>
</select><br>
<Span>DOB: </Span><input type ="date" name ="DOB" /><br><br>
<Span>MembershipExpiry: </Span> <input type ="date" name ="MemX" /><br>
<Span>MembershipType: </Span><select name = "MemType">
<option value ="Junior">Junior</option>
<option value ="Adult">Adult</option>
<option value ="Senior">Senior</option>
</select><br><br>
<Span>Email Address: </Span><input type ="email" name ="Email" /><br><br>
<input type="Submit" name="submit" value ="Submit Form">
</form>
</body>
Connect.php:
<?php
$conn = mysql_connect("localhost", "root", "");
mysql_select_db("gym");
if(!$conn)
echo"Error Connecting to Database!";
else
echo"Connected to Database!";
的login.php:
include "Connect.php";
$title = $_POST['Title'];
$fname = $_POST['Fname'];
$lname = $_POST['Lname'];
$gender = $_POST['Gender'];
$dob = $_POST['DOB'];
$memx = $_POST['MemX'];
$memtype = $_POST['MemType'];
$email = $_POST['Email'];
$sql = mysql_query("INSERT INTO records (Title, Fname, Lname, Gender, DOB, MemX, MemType, Email) values ('$title', '$fname', '$lname', '$gender', '$dob', '$memx', '$memtype', '$email')", $conn);
我填写表单并收到一条消息,说明已建立连接,但是,当我检查数据库时...没有添加记录?请帮助,谢谢。
答案 0 :(得分:0)
以下代码将帮助您找到错误并解决弃用错误。
<?php
$con=mysqli_connect("localhost","root","","gym");
// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Perform queries
mysqli_query($con, "INSERT INTO records (Title, Fname, Lname, Gender, DOB,MemX, MemType, Email) values ('" . $title . "', '" . $fname . "', '" . $lname . "', '" . $gender . "', '" . $dob . "', '" . $memx . "', '" . $memtype . "', '" . $email . "');" or die(mysqli_error($con));
mysqli_close($con);
?>