使用PHP在mySQL表中递增主键

时间:2022-06-29 09:06:11

I'm attempting to use a form post to add rows to a mySQL table. Each row has a primary I'm calling quoteID. When a new form is submitted, it should add itself as a row in the table, with a quoteID of one greater than the previous quoteID. It current looks something like this:

我正在尝试使用表单post向mySQL表添加行。每一行都有一个我称之为quoteID的主元素。当提交新表单时,它应该将自己添加为表中的一行,其quoteID大于先前的quoteID。它的电流是这样的:

<?
session_start();
if(!session_is_registered(myusername)){
header("location:login.php");
}
include 'verify.php';
$con = mysql_connect("localhost","user","$password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("internal", $con);

$previousOrderID = mysql_query("SELECT * FROM sourcingQuote ORDER BY quoteID DESC LIMIT 1"); 
$newOrderID = $previousOrderID + 1;

mysql_close($con);
?>

At the moment there are 4 rows in this table, with quoteID's of 1,2,3 and 4. The odd thing is, if I attempt:

此时该表中有4行,quoteID为1、2、3和4。奇怪的是,如果我尝试:

<? echo $previousOrderID; ?><br>
<? echo $newOrderID; ?><br>

The output result is:

输出结果是:

Resource id #3

4

Regardless of how many rows are in the table, $previousOrderID is fixed to 3. How can I correct this? More so, it's correct when I do addition to it, but why does it output 'Resource id #3' in the first place?

不管表中有多少行,$previousOrderID被固定为3。我该如何改正呢?更重要的是,当我添加它时,它是正确的,但是为什么它首先输出‘Resource id #3’呢?

4 个解决方案

#1


2  

you can do this by choosing the auto increment in phpmyadmin

您可以通过选择phpmyadmin中的自动增量来实现这一点。

使用PHP在mySQL表中递增主键

or by sql example the table name is demo and the colon is demo whit the auto increment and its promary key

或者通过sql示例,表名是demo,冒号是demo,演示自动增量及其promary键

CREATE TABLE IF NOT EXISTS `demo` (
  `demmo` int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`demmo`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

#2


1  

The best way you can do to avoid duplicate is to set the QuoteID as AUTO_INCREMENTed PRIMARY KEY

要避免重复,最好的方法是将QuoteID设置为auto_increment主键

CREATE TABLE sourcingQuote
(
    QuoteID INT AUTO_INCREMENT,
    -- other columns,
    CONSTRAINT t_pk PRIMARY KEY (QuoteID)
)

#3


1  

No, that is a resource link, you have to get the data:

不,那是一个资源链接,你必须得到数据:

<?php
    $previousOrder = mysql_query("SELECT * FROM `sourcingQuote` ORDER BY `quoteID` DESC LIMIT 1"); 
    $previousOrder = mysql_fetch_assoc($previousOrder);
    $previousOrderID = $previousOrder['quoteID'];
    $newOrderID = $previousOrderID + 1;

    echo $previousOrderID."<br />";
    echo $newOrderID."<br />";
?>

#4


0  

you can make 'quoteID' as auto increment in mysql phpmyadmin.....

你可以在mysql phpmyadmin中添加“quoteID”作为自动增量。

and u gettin the Resource id #3.. beacuse u r priting the resource link.. u can print the datas by

然后是资源id #3。因为你在撬开资源链接。你可以把数据打印出来

$previousOrderID = mysql_query("SELECT * FROM sourcingQuote ORDER BY quoteID DESC LIMIT 1"); 
$previousOrderID = mysql_fetch_assoc($previousOrderID);
$newOrderID = $previousOrderID['ID'] + 1;

#1


2  

you can do this by choosing the auto increment in phpmyadmin

您可以通过选择phpmyadmin中的自动增量来实现这一点。

使用PHP在mySQL表中递增主键

or by sql example the table name is demo and the colon is demo whit the auto increment and its promary key

或者通过sql示例,表名是demo,冒号是demo,演示自动增量及其promary键

CREATE TABLE IF NOT EXISTS `demo` (
  `demmo` int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`demmo`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

#2


1  

The best way you can do to avoid duplicate is to set the QuoteID as AUTO_INCREMENTed PRIMARY KEY

要避免重复,最好的方法是将QuoteID设置为auto_increment主键

CREATE TABLE sourcingQuote
(
    QuoteID INT AUTO_INCREMENT,
    -- other columns,
    CONSTRAINT t_pk PRIMARY KEY (QuoteID)
)

#3


1  

No, that is a resource link, you have to get the data:

不,那是一个资源链接,你必须得到数据:

<?php
    $previousOrder = mysql_query("SELECT * FROM `sourcingQuote` ORDER BY `quoteID` DESC LIMIT 1"); 
    $previousOrder = mysql_fetch_assoc($previousOrder);
    $previousOrderID = $previousOrder['quoteID'];
    $newOrderID = $previousOrderID + 1;

    echo $previousOrderID."<br />";
    echo $newOrderID."<br />";
?>

#4


0  

you can make 'quoteID' as auto increment in mysql phpmyadmin.....

你可以在mysql phpmyadmin中添加“quoteID”作为自动增量。

and u gettin the Resource id #3.. beacuse u r priting the resource link.. u can print the datas by

然后是资源id #3。因为你在撬开资源链接。你可以把数据打印出来

$previousOrderID = mysql_query("SELECT * FROM sourcingQuote ORDER BY quoteID DESC LIMIT 1"); 
$previousOrderID = mysql_fetch_assoc($previousOrderID);
$newOrderID = $previousOrderID['ID'] + 1;