回应mySQL数据库中的整数

时间:2021-10-31 03:54:07

I'm trying to make a website in which a user inputs details on one screen, and they are posted onto the following script. This script is meant to store these details in a database along with a unique integer ID (which it does), and then generate two links containing the unique ID of the record just created. Since the database creates the ID rather than the page before, I've tried to query the database for the most recent record (i.e. the one with the highest unique ID value) and use that number in the link, however with the current script the ID doesn't seem to show up in the page. Is it a variable type thing? Is there a simpler way to get the ID of the page just created? Here's the code:

我正在尝试建立一个用户在一个屏幕上输入详细信息的网站,并将它们发布到以下脚本中。此脚本用于将这些详细信息与唯一的整数ID(它执行)一起存储在数据库中,然后生成包含刚刚创建的记录的唯一ID的两个链接。由于数据库之前创建的是ID而不是页面,我试图在数据库中查询最新的记录(即具有最高唯一ID值的记录)并在链接中使用该数字,但是使用当前脚本ID似乎没有显示在页面中。这是一个变量型的东西吗?有没有更简单的方法来获取刚刚创建的页面的ID?这是代码:

$css = $_POST['css'];
$shopName = strip_tags($_POST['title']);
$email = $_POST['email'];

$con = mysql_connect("***","***","***");
if (!$con)
{
    die('Could not connect to database: '. mysql_error());
}

mysql_select_db("***", $con);

$sql = "INSERT INTO wps_Shops (shopName, shopEmail, shopStyle)
    VALUES ('$shopName', '$email', '$css')";

$quer = mysql_query($sql);

$result = mysql_query("SELECT *
                    FROM wps_Shops
                    ORDER BY shopId DESC
                    LIMIT 1");

$lastShop = mysql_fetch_array($result);

$id = strval($lastShop['id']);

echo ("Id: ".$id);

if ($quer)
{
    echo("<h1>Shop created</h1>");
    echo("<p><a href=\"shop.php?id=$id\">Go to shop</a></p>");
    echo("<p><a href=\"addproducts.php?id=$id\">Add products</a></p>");
}

mysql_close($con);

4 个解决方案

#1


Right after you do the mysql_query() for the insert, you can use mysql_insert_id() to get the ID of the inserted row.

在为插入执行mysql_query()之后,可以使用mysql_insert_id()来获取插入行的ID。

mysql_query("INSERT INTO........");
$id=mysql_insert_id();

#2


You need mysql_insert_id.

你需要mysql_insert_id。

#3


Is it called 'id' or 'shopId' ? But you should use: http://php.net/mysql_insert_id

它被称为'id'或'shopId'?但是你应该使用:http://php.net/mysql_insert_id

#4


Alternatively, you can make the following MySQL query:

或者,您可以进行以下MySQL查询:

$query = "SELECT * FROM wps_Shops WHERE id=LAST_INSERT_ID()";
$result = mysql_query($query);

Though I'm confused as to why you used ORDER BY shopId but in a later line call:

虽然我很困惑为什么你使用ORDER BY shopId,但在后来的线路电话中:

$id = strval($lastShop['id']);

Also, is there really any need to make that strval() call? PHP already does that type conversion for you when you call echo(). And, currently, any result data returned from either, mysql_query() or mysqli_query(), is returned in string format, regardless of that column's data type in MySQL.

此外,是否真的需要进行strval()调用?当你调用echo()时,PHP已经为你做了类型转换。而且,目前,从mysql_query()或mysqli_query()返回的任何结果数据都以字符串格式返回,而不管MySQL中该列的数据类型如何。

#1


Right after you do the mysql_query() for the insert, you can use mysql_insert_id() to get the ID of the inserted row.

在为插入执行mysql_query()之后,可以使用mysql_insert_id()来获取插入行的ID。

mysql_query("INSERT INTO........");
$id=mysql_insert_id();

#2


You need mysql_insert_id.

你需要mysql_insert_id。

#3


Is it called 'id' or 'shopId' ? But you should use: http://php.net/mysql_insert_id

它被称为'id'或'shopId'?但是你应该使用:http://php.net/mysql_insert_id

#4


Alternatively, you can make the following MySQL query:

或者,您可以进行以下MySQL查询:

$query = "SELECT * FROM wps_Shops WHERE id=LAST_INSERT_ID()";
$result = mysql_query($query);

Though I'm confused as to why you used ORDER BY shopId but in a later line call:

虽然我很困惑为什么你使用ORDER BY shopId,但在后来的线路电话中:

$id = strval($lastShop['id']);

Also, is there really any need to make that strval() call? PHP already does that type conversion for you when you call echo(). And, currently, any result data returned from either, mysql_query() or mysqli_query(), is returned in string format, regardless of that column's data type in MySQL.

此外,是否真的需要进行strval()调用?当你调用echo()时,PHP已经为你做了类型转换。而且,目前,从mysql_query()或mysqli_query()返回的任何结果数据都以字符串格式返回,而不管MySQL中该列的数据类型如何。