Home  >  Article  >  Backend Development  >  What to do if php cannot add data

What to do if php cannot add data

藏色散人
藏色散人Original
2022-11-01 09:21:311870browse

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)".

What to do if php cannot add data

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,&#39;utf8&#39;);
$x =$_POST["用户名"];
$y =$_POST["密码"];
$Z =$_POST["性别1"];
$O =$_POST["学历1"];
$a=mysqli_query($conn,"INSERT INTO student.plus1 (用户名, 密码, 性别, 学历) VALUES (&#39;$x&#39;,&#39;$y&#39;,&#39;$Z&#39;,&#39;$O&#39;)");
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 (&#39;$x&#39;,&#39;$y&#39;,&#39;$Z&#39;,&#39;$O&#39;)");

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn