开发者

PHP Mysql increment a column during insert

Sorry, let me try to explain .... recordID auto increments and is my primary key ..... LISTINGID refers to the ID in a different table. In this table 1 need to increment recordListingID for each record that has the same LISTINGID. My insert statement inserts upto 10 records that have the same LISTINGID I need the the recordListingID to start at 1 and so on.

Hi guys

I am inserting records into mysql from php it can be 1 or more records I need one of the cols to increment with the first entry being 1 here is my insert code

开发者_如何学JAVAmysql_query("INSERT INTO car_listing_images (recordID, recordText, recordListingID,   LISTINGID) VALUES ('', '$fileName', '??', '$listingid')");

where I have put ?? is the col that needs to increment. How can i achieve this?

Thanks in advance!


You can use the built in Auto_Increment function of MySql

  AUTO_INCREMENT

http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html

MySql would increment the specified field by 1 (Or what ever interval you set)


You may use user-defined variables


A preface: If you need to have 2 fields that increment, but are not tied to each other, you are probably doing it wrong. It might be better to not have recordListingID and instead just use the recordID as they will probably be the same number.

If your tables are running InnoDB you can create transactions. Then you can try something like this:

<?php

mysql_query('START TRANSACTION');

$recordListingId = mysql_result(mysql_query("SELECT count(*)+1 as num FROM car_listing_images WHERE LISTINGID=$listingid"), 'num', 0);

mysql_query("INSERT INTO car_listing_images (recordID, recordText, recordListingID,   LISTINGID) VALUES ('', '$fileName', '$recordListingId', '$listingid')");

mysql_query('COMMIT');
?>

If you don't have innodb, try using a stored procedure.


Alter your table structure to include the AUTO_INCREMENT attribute for your recordListingID column, then omit the column from your insert to have it auto-populate with an incremental number.

I.e: mysql_query("INSERT INTO car_listing_images (recordID, recordText, LISTINGID) VALUES ('', '$fileName', '$listingid')");


You can initialise the variable as 1 and use the post-increment operator:

$value = 1;
for(...){
    $sql = 'INSERT ...';
    $value++;
}


I don't know if I really understand your question.

If you're wanting to increment a column with every record, it should be defined as an AUTO_INCREMENT column. This way, in your INSERT statement if you insert NULL into that column, it will go up every time.

Alternatively you could do fieldname+1, but AUTO_INCREMENT is always preferred.


As per my comment, you could do something like this:

$row = mysql_fetch_assoc(mysql_query("SELECT MAX(recordListingID) AS `max` FROM car_listing_images"));
$next_id = $row['max'] + 1;
mysql_query("INSERT INTO car_listing_images (recordID, recordText, recordListingID,   LISTINGID) VALUES ('', '$fileName', '$next_id', '$listingid')");

I would still seriously recommend against this, it's a much much better implementation to use an AUTO_INCREMENT field.


After your last comment I would suggest the following. You will not be able to do this in MySQL directly (unless you use a variable, but this may go wrong if you're inserting multiple RecordIDs too).

$next_id = 0;
foreach ($insert as $insert_stmt) {
    $next_id++;
    mysql_query("INSERT INTO car_listing_images (recordID, recordText, recordListingID,   LISTINGID) VALUES ('', '$fileName', '$next_id', '$listingid')");
}


i think i know what you were/are trying to accomplish. there may be a better way to it, but what i did was to "select max(id)+1 as nextId from table" and store that in a variable that i used in the secondary id field that you called "recordListingID."

that way the first row you insert will have the same id and recordListingID, but each other row will auto_inc the id, but the recordListingID will be the same as the first.

in my situation, there is only one user doing this at a time, so there is no chance of errors, but in a multi-user situation, you may want to modify this to watch out for people doing inserts at the same time. like maybe marking the last row so your query knows it's finished so you could so something like "select max(id)+1 as nextId from table where lastRow =1" or something like that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜