PHP Mysql 在插入期间增加一列

发布于 2024-11-05 19:43:15 字数 493 浏览 3 评论 0原文

抱歉,让我尝试解释一下.... recordID 自动递增并且是我的主键..... LISTINGID 指的是不同表中的ID。在此表中,1 需要为具有相同 LISTINGID 的每个记录增加 recordListingID。我的插入语句最多插入 10 条具有相同 LISTINGID 的记录,我需要 recordListingID 从 1 开始,依此类推。

大家好,

我正在从 php 将记录插入到 mysql 中,它可以是 1 条或多条记录,我需要其中一个列来递增,第一个条目为 1 这是我

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

放置的插入代码?是需要递增的 col。我怎样才能做到这一点?

提前致谢!

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

mysql_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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

梦初启 2024-11-12 19:43:15

您可以使用MySql内置的Auto_Increment函数

  AUTO_INCREMENT

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

MySql 会将指定字段增加 1(或者您设置的间隔)

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)

泪痕残 2024-11-12 19:43:15

前言:如果您需要有 2 个递增的字段,但彼此不相关,那么您可能做错了。最好不要使用 recordListingID,而只使用 recordID,因为它们可能是相同的数字。

如果您的表正在运行 InnoDB,您可以创建事务。然后你可以尝试这样的事情:

<?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');
?>

如果你没有 innodb,请尝试使用 存储过程

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.

九命猫 2024-11-12 19:43:15

更改表结构以包含 AUTO_INCRMENT 属性对于您的 recordListingID 列,然后从插入中省略该列,以使其自动填充增量数字。

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

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')");

逆蝶 2024-11-12 19:43:15

您可以将变量初始化为 1 并使用 后增量运算符

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

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

$value = 1;
for(...){
    $sql = 'INSERT ...';
    $value++;
}
薄情伤 2024-11-12 19:43:15

我不知道我是否真正理解你的问题。

如果您想为每条记录增加一列,则应将其定义为 AUTO_INCRMENT 列。这样,在您的 INSERT 语句中,如果您将 NULL 插入该列,它每次都会上升。

或者,您可以执行 fieldname+1,但 AUTO_INCRMENT 始终是首选。


根据我的评论,您可以这样做:

$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')");

我仍然强烈建议不要这样做,使用 AUTO_INCRMENT 字段是一个更好更好的实现。


在您上次发表评论后,我建议如下。您将无法直接在 MySQL 中执行此操作(除非您使用变量,但如果您也插入多个 RecordID,则可能会出错)。

$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 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')");
}
沫雨熙 2024-11-12 19:43:15

我想我知道你曾经/正在努力实现什么目标。可能有更好的方法,但我所做的是“从表中选择 max(id)+1 作为 nextId”并将其存储在我在辅助 id 字段中使用的变量中,您将其称为“recordListingID”。

这样,您插入的第一行将具有相同的 id 和 recordListingID,但其他每一行都会 auto_inc id,但 recordListingID 将与第一行相同。

在我的情况下,一次只有一个用户执行此操作,因此不会出现错误,但在多用户情况下,您可能需要修改此设置以留意同时执行插入的人。就像标记最后一行这样你的查询就知道它已经完成,所以你可以这样“从表中选择 max(id)+1 作为 nextId,其中lastRow = 1”或类似的东西。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文