MySQL插入查询不起作用(PHP)

时间:2022-09-25 20:06:56

I need help in understanding why my simple insert query doesn't work.

我需要帮助来理解为什么我的简单插入查询不起作用。

the code below:

代码如下:

<?php

// db info
define ( 'DB_HOST', '' );`enter code here`
define ( 'DB_USER', '' );
define ( 'DB_PASSWORD', '' );
define ( 'DB_NAME', '' );

create_db_connection ();
function create_db_connection() {
    global $con;

    $con = mysql_connect ( DB_HOST, DB_USER, DB_PASSWORD ) or die ( "connection not established" );
    $db = mysql_select_db ( DB_NAME, $con ) or die ( "database not connected" );
}


$sql= "insert into 'tbl_BonAproval' (OrderId,RestId) values (20001,98765)";


if (mysqli_query ( $db, $sql )) {
    echo "Successfully inserted " . mysqli_affected_rows ( $db ) . " row";
} else {
    echo "Error occurred: " . mysqli_error ( $db );
}
function close_mysql_connection() {
    global $con;
    mysql_close ( $con );
}

$result = mysql_query ( "SELECT * FROM `tbl_BonAproval`" );
$row = mysql_fetch_array ( $result );
print_r ( $row ); // print array
echo $row ['table field name']; // print field data

// $sql=mysql_query('INSERT INTO tbl_BonAproval (OrderId, RestId) VALUES (24553, 01001);');
                               // echo $sql;



mysql_close ( $conn );
class CallBack {
}

?>

3 个解决方案

#1


4  

You should try MySQLi instead of its predecessor MySQL. Here's a simplified code of yours:

你应该尝试MySQLi而不是它的前身MySQL。这是您的简化代码:

<?php

$conn=mysqli_connect("YourHost","YourUsername","YourPassword","YourDatabase");

if(mysqli_connect_errno()){

echo "Error".mysqli_connect_error();
}

mysqli_query($conn,"INSERT INTO tbl_BonAproval (OrderId, RestId) VALUES ('20001','98765')");

$result = mysqli_query ($conn, "SELECT * FROM tbl_BonAproval");
while($row = mysqli_fetch_array ($result)){
echo $row['table field name']; /* print field data */ 
}

?>

#2


2  

Don't mix mysql and mysqli. In order to use mysqli you should open a mysqli connection. And use back ticks for table name in insert query.

不要混用mysql和mysqli。为了使用mysqli,你应该打开一个mysqli连接。并在插入查询中使用返回刻度表作为表名。

EG:

$connection = mysqli_connect('HOST', 'USERNAME', 'PASS', 'TABLE'); //use mysqli connection

$sql= "insert into `tbl_BonAproval` (OrderId,RestId) values ('20001','98765')"; //for insert query

Sidenote: mysql_* is deprecated, use mysqli_* or PDO

旁注:不推荐使用mysql_ *,使用mysqli_ *或PDO

#3


1  

Change the insert query to this:

将插入查询更改为:

$sql= "insert into `tbl_BonAproval` (OrderId,RestId) values (20001,98765)";

You can't use single quotes to encapsulate the table or column names.

您不能使用单引号来封装表名或列名。

#1


4  

You should try MySQLi instead of its predecessor MySQL. Here's a simplified code of yours:

你应该尝试MySQLi而不是它的前身MySQL。这是您的简化代码:

<?php

$conn=mysqli_connect("YourHost","YourUsername","YourPassword","YourDatabase");

if(mysqli_connect_errno()){

echo "Error".mysqli_connect_error();
}

mysqli_query($conn,"INSERT INTO tbl_BonAproval (OrderId, RestId) VALUES ('20001','98765')");

$result = mysqli_query ($conn, "SELECT * FROM tbl_BonAproval");
while($row = mysqli_fetch_array ($result)){
echo $row['table field name']; /* print field data */ 
}

?>

#2


2  

Don't mix mysql and mysqli. In order to use mysqli you should open a mysqli connection. And use back ticks for table name in insert query.

不要混用mysql和mysqli。为了使用mysqli,你应该打开一个mysqli连接。并在插入查询中使用返回刻度表作为表名。

EG:

$connection = mysqli_connect('HOST', 'USERNAME', 'PASS', 'TABLE'); //use mysqli connection

$sql= "insert into `tbl_BonAproval` (OrderId,RestId) values ('20001','98765')"; //for insert query

Sidenote: mysql_* is deprecated, use mysqli_* or PDO

旁注:不推荐使用mysql_ *,使用mysqli_ *或PDO

#3


1  

Change the insert query to this:

将插入查询更改为:

$sql= "insert into `tbl_BonAproval` (OrderId,RestId) values (20001,98765)";

You can't use single quotes to encapsulate the table or column names.

您不能使用单引号来封装表名或列名。