我正在写一个简单的代码php插入数据到mysql数据库使用wamp服务器。但是数据库中没有插入数据。有没有人能告诉我我的代码哪里错了?我正在写一个简单的php代码,以便使用wamp服务器在mysql数据库中插入数据。但是数据库中没有插入数据。有没有人能告诉我我的代码哪里错了?
<?php
$con=mysqli_connect('localhost','root','','my_first_database');
if (isset($_POST['submit'])) {
$name=$_POST['name'];
$fname=$_POST['fname'];
$city=$_POST['city'];
if(mysqli_query($con, "INSERT INTO student_record(Name,Fname,city) array_values( ('$name','$fname','$city'))")){
echo "data submitted";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form method="post">
<input type="text" name="name">
<br><input type="text" name="fname">
<br><input type="text" name="city">
<br><input type="submit" name="submit" value="submit">
</form>
</body>
</html>
发布于 2018-05-31 10:51:51
您将POST赋值给了变量。不要使用array_values,只要使用值和变量名即可。
另外,从变量值周围删除多余的圆括号。
尝试:
if(mysqli->query($con, "
INSERT INTO
student_record(Name, Fname, city)
VALUES
('$name', '$fname', '$city')")) {...//rest of Code
整个代码应该如下所示:
<?php
$con = mysqli_connect("localhost", "root", "yourpassword", "my_first_database");
if (isset($_POST['submit'])) {
$name=$_POST['name'];
$fname=$_POST['fname'];
$city=$_POST['city'];
if (mysqli->query($con, "
INSERT INTO
student_record(Name, Fname, city)
VALUES
('$name', '$fname', '$city')")) {
echo "data submitted";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form method="post" action="">
<input type="text" name="name">
<br>
<input type="text" name="fname">
<br>
<input type="text" name="city">
<br>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
https://stackoverflow.com/questions/50615889
复制相似问题