Home > Article > Backend Development > What to do if php cannot add data
Solution to the problem that php cannot add data: 1. Open the corresponding php file; 2. Check the "mysqli_query" statement; 3. Change the sentence "if (mysqli_query($conn, $a))" to Just "if($a)".
The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.
What should I do if php cannot add data?
Problem description:
The database can be connected successfully, but PHP cannot insert data. Can anyone check if there is a problem with the code? ?
<?php /*连接数据库*/ if( @ $_POST["登入注册"]=="登入" ) { /*连接数据库*/ $servername = "localhost"; $username = "root"; $password = "123456"; $dbname = "student"; // 创建连接 $conn = new mysqli($servername, $username, $password,$dbname); /*编码,是中英文顺利编入进去*/ mysqli_set_charset($conn,'utf8'); $x =$_POST["用户名"]; $y =$_POST["密码"]; $Z =$_POST["性别1"]; $O =$_POST["学历1"]; $a=mysqli_query($conn,"INSERT INTO student.plus1 (用户名, 密码, 性别, 学历) VALUES ('$x','$y','$Z','$O')"); if (mysqli_query($conn, $a)) { echo "新记录插入成功"; } else { echo "Error: " . $a . "<br>" . mysqli_error($conn); } mysqli_close($conn); } ?>
Problem solution:
$a=mysqli_query($conn,"INSERT INTO student.plus1 (用户名, 密码, 性别, 学历) VALUES ('$x','$y','$Z','$O')");
This sentence means that the insert statement has been executed. Mysqli_query executes the query statement and returns the object. If the insert statement is successfully executed, true will be returned.
if (mysqli_query($conn, $a)) Just change this sentence to if ($a).
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What to do if php cannot add data. For more information, please follow other related articles on the PHP Chinese website!