我正在尝试将一些PHP
变量放入MYSQL
数据库中。
知道我的代码哪里出问题了吗?
$servername = "127.0.0.1";
$username = "root";
$password = "";
$DBname = "questionnaire";
$conn = new mysqli($servername, $username, $password,$DBname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$favisit = mysqli_real_escape_string($conn,$favisit);
$favanimal = mysqli_real_escape_string($conn,$favanimal);
$age = mysqli_real_escape_string($conn,$age);
$area = mysqli_real_escape_string($conn,$area);
$sql = "USE questionnaire; INSERT INTO info (favVisit, favAnimals, age,area)
VALUES ('$favisit','$favanimal','$age' ,'$area')";
if ($conn->query($sql) === TRUE) {
echo "Inputted";
} else {
echo "failed";
}
答案 0 :(得分:1)
该查询不需要USE调查表,因为我已经连接了它
答案 1 :(得分:-1)
在$ sql中创建字符串时,必须关闭字符串引号才能访问php变量。 例如$ string =“ your data'”。$ phpvar。“'; 试试这个:
$servername = "127.0.0.1";
$username = "root";
$password = "";
$DBname = "questionnaire";
$conn = new mysqli($servername, $username, $password,$DBname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$favisit = mysqli_real_escape_string($conn,$favisit);
$favanimal = mysqli_real_escape_string($conn,$favanimal);
$age = mysqli_real_escape_string($conn,$age);
$area = mysqli_real_escape_string($conn,$area);
$sql = "INSERT INTO info (favVisit, favAnimals, age,area)
VALUES ('".$favisit."','".$favanimal."','".$age."' ,'".$area."')";
if ($conn->query($sql) === TRUE) {
echo "Inputted";
} else {
echo "failed";
}