无法从数据库中获取最近添加的数据

时间:2022-04-23 02:34:25

I created a data through an insert page where data will be inserted using mysql. When I try to fetch the new inserted data, it won't fetch the data.

我通过插入页面创建了一个数据,使用mysql插入数据。当我尝试获取新插入的数据时,它不会获取数据。

For example:

INSERT INTO bag(bid,bookno,name) VALUES ('','$bn','$n');

My data supposedly:

我的数据应该是:

           bid   bookno     name
            1      23      mystery
            2      56      mystery
            3      89      mysteryy

When I fetch the name column, it fetches the name="mystery" smoothly, but it cannot fetch the name ="mysteryy" data which is just recently inserted to my database.

当我获取名称列时,它可以顺利地获取name =“mystery”,但它无法获取刚刚插入我的数据库的name =“mysteryy”数据。

My fetch query is...

我的获取查询是......

SELECT * FROM bag WHERE name='$name';

Where $name=$_POST['nam'];

This is my HTML code:

这是我的HTML代码:

<td><input type="checkbox" name="chk[]"/></td>
<td><input type="text" name="bookno[]" id="bookno" /></td>
<td><input type="text" name="nam[]" id="nam" /></td>

The PHP insert code:

PHP插入代码:

foreach($_POST['nam'] as $row=>$pro)
{
    if($pro!="")
    {
    $n=$pro;
    $bn=$_POST['bookno'][$row];
    }
}

$s=INSERT INTO bag(bid,bookno,name) VALUES ('','$bn','$n');

2 个解决方案

#1


0  

Try using LIKE in your SELECT statement. (and converting your code into MySQLi):

尝试在SELECT语句中使用LIKE。 (并将您的代码转换为MySQLi):

$name=mysqli_real_escape_string($yourConnection,$_POST['nam']); /* PRACTICE USING MYSQLI REAL ESCAPE STRING SO YOU CAN PREVENT SOME OF THE SQL INJECTION */

$result=($yourConnection,"SELECT * FROM bag WHERE name LIKE '%$name%'");


About the $yourConnection variable:

关于$ yourConnection变量:

$yourConnection=mysqli_connect("YourHost","Username","Password","YourDatabase");

if(mysqli_connect_errno()){

echo "Error".mysqli_connect_error();
}

#2


1  

Assuming your bid column is set to auto increment, you'll want to set that field to null or just omit it. If that's your primary index and you pass it an empty string every time,it will only save the first one.

假设您的出价列设置为自动增量,您需要将该字段设置为null或只是省略它。如果那是你的主索引并且你每次都传递一个空字符串,它只会保存第一个。

insert into bag(bid,bookno,name) values (null,'$bn','$n');

or simply

insert into bag(bookno,name) values ('$bn','$n');

#1


0  

Try using LIKE in your SELECT statement. (and converting your code into MySQLi):

尝试在SELECT语句中使用LIKE。 (并将您的代码转换为MySQLi):

$name=mysqli_real_escape_string($yourConnection,$_POST['nam']); /* PRACTICE USING MYSQLI REAL ESCAPE STRING SO YOU CAN PREVENT SOME OF THE SQL INJECTION */

$result=($yourConnection,"SELECT * FROM bag WHERE name LIKE '%$name%'");


About the $yourConnection variable:

关于$ yourConnection变量:

$yourConnection=mysqli_connect("YourHost","Username","Password","YourDatabase");

if(mysqli_connect_errno()){

echo "Error".mysqli_connect_error();
}

#2


1  

Assuming your bid column is set to auto increment, you'll want to set that field to null or just omit it. If that's your primary index and you pass it an empty string every time,it will only save the first one.

假设您的出价列设置为自动增量,您需要将该字段设置为null或只是省略它。如果那是你的主索引并且你每次都传递一个空字符串,它只会保存第一个。

insert into bag(bid,bookno,name) values (null,'$bn','$n');

or simply

insert into bag(bookno,name) values ('$bn','$n');