我想将数据插入mysql服务器但是出现了一些错误,请帮忙,因为我是php和sql的新手。
我得到的错误是错误:INSERT INTO table1(Name,RollNumber, 旅馆,EmailAddress,密码,验证密码)价值观 (sa,sa,asd @ sa.com,sa,sa)您的SQL语法中有错误;校验 与您的MySQL服务器版本对应的手册 语法在' @ sa.com,sa,sa)附近使用'在第1行。
这是代码 你好
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "ins1";
// Create connection
$conn = new mysqli($servername, $username, $password,$database);
// Check connection
if (mysqli_connect_error()) {
die("Database connection failed: " . mysqli_connect_error());
}
$Name=" ";
$RollNumber=" ";
$Hostel=" ";
$EmailAddress=" ";
$Password=" ";
$VerifyPassword=" ";
$Name=$_POST["Name"];
$RollNumber=$_POST["RollNumber"];
$Hostel=$_POST["Hostel"];
$EmailAddress=$_POST["EmailAddress"];
$Password=$_POST["Password"];
$VerifyPassword=$_POST["VerifyPassword"];
//$sql = "INSERT INTO table1 (Name, RollNumber, Hostel, EmailAddress, Password, VerifyPassword)
//VALUES ($Name,$RollNumber,$Hostel,$EmailAddress,$Password,$VerifyPassword)";
$sql = "INSERT INTO table1 (Name, RollNumber, Hostel, EmailAddress,Password, VerifyPassword) VALUES ($Name,$RollNumber,$EmailAddress,$Password,$VerifyPassword )" ;
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
<form action="ins12.php" method="post">
Name
<input name="Name" type="text" placeholder="Name">
</br>
Roll Number
<input name="RollNumber" type="text" placeholder="DE15B002">
</br>
Hostel
<input name="Hostel" type="text" placeholder="Pampa">
</br>
Email Address
<input name="EmailAddress" type="email" placeholder="abc@xyz.com">
</br>
Password
<input name="Password" type="password" placeholder="Password">
</br>
Verify password
<input name="VerifyPassword" type="password" placeholder="Verify Password">
</br>
<input type="submit">
</form>
</body>
<script type="text/javascript">
</script>
</html>
答案 0 :(得分:1)
可能会出现错误,因为您不会将插入值包装到引号中。
$sql = "INSERT INTO table1 (Name, RollNumber, Hostel, EmailAddress,Password, VerifyPassword) VALUES ('$Name','$RollNumber','$EmailAddress','$Password','$VerifyPassword')" ;