将Common ID分配给MySQL数组中的多行插入

时间:2011-10-02 22:54:20

标签: php mysql

我有一个PHP脚本,它根据MySQL数组中的选定行将行插入表中。

将行插入新表的代码是:

$sql="insert into loaddetails (CaseNo,GrossMass,CaseStatus,Customer)
select `case no`,`gross mass`,`case status`, customer from
availablestock where `case no` = '$val'";

我想为所有插入的行分配相同的ID,以便多个stock项共享相同的LoadID。

如何修改我的代码以执行此操作,以便所有插入的记录共享相同的ID,并且ID对于加载是唯一的。

我以为我可以使用下面的代码来获取最大ID并将其递增一个

SELECT max(loadid)+1 from loaddetails

我怎样才能实现这一目标?我意识到我的PHP代码并不完美但功能正常,我只需添加功能以允许使用公共ID插入行以产生如下结果:

enter image description here

提前感谢您的帮助。

此致 莱恩史密斯

完整的代码是:

<?php
    mysql_connect("localhost", "user", "password")or die("cannot connect");    
    mysql_select_db("databasename")or die("cannot select DB");
    $sql="SELECT `case no`,`customer`,`gross mass`, `case status` from availablestock where transporttypename= 'localpmb'";
    $result=mysql_query($sql);
    $count=mysql_num_rows($result);
?>
<table border=1>
    <tr>
        <td>
            <form name="form1" method="post">
                <table>
                    <tr>
                        <td>#</td>
                        <td>Case Number</td>
                        <td>Customer</td>    
                        <td>Weight</td> 
                        <td>Status</td> 
                    </tr>
<?php
    while($rows=mysql_fetch_array($result)){
?>
                    <tr>
                        <td><input type="checkbox" name=check[]  value="<?php echo $rows['case no']; ?>"></td>
                        <td><?php echo $rows['case no']; ?></td>
                        <td><?php echo $rows['customer']; ?></td>
                        <td><?php echo $rows['gross mass']; ?></td>
                        <td><?php echo $rows['case status']; ?></td>
                    </tr>                                   

<?php
    }
?>
                    <tr>
                        <td><input name="planlocalpmb" type="submit" id="planlocalpmb" value="planlocalpmb"></td>
                    </tr>
                    <?php



                            $check=$_POST['check'];

                        if($_REQUEST['planlocalpmb']=='planlocalpmb'){
 {
                            $sql="insert into loaddetails (CaseNo,GrossMass,CaseStatus,Customer) select `case no`,`gross mass`,`case status`, customer from availablestock where `case no` = '$val'";

                            foreach($check as $key=>$value)
                            {
                            $sql="insert into loaddetails (CaseNo,GrossMass,CaseStatus,Customer) select `case no`,`gross mass`,`case status`, customer from availablestock where `case no` = '$value'";
                            $final=mysql_query($sql);
                            if($final)
                            {
                            echo "<meta http-equiv=\"refresh\" content=\"0;URL=php.php\">";
                            }                                            } 
                                }
                                }
                    // Check if delete button active, start this



// if successful redirect to php.php

mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>

2 个答案:

答案 0 :(得分:0)

INSERT INTO loaddetails (LoadID, CaseNo, GrossMass, CaseStatus, Customer)
SELECT (SELECT MAX(loadid)+1 FROM loaddetails) LoadID, CaseNo, GrossMass, CaseStatus, Customer
FROM availablestock WHERE CaseNo = '$val'

确保LoadID有索引!

您可能需要先锁定表格。我不确定。

我没有检查你的其余代码,但我注意到你有一个SQL注入漏洞。

答案 1 :(得分:0)

您也可以尝试:

INSERT INTO loaddetails 
  (LoadID, CaseNo, GrossMass, CaseStatus, Customer)
SELECT  m.NewLoadId, CaseNo, GrossMass, CaseStatus, Customer
FROM availablestock 
  CROSS JOIN
    ( SELECT MAX(loadid)+1 AS NewLoadId 
      FROM loaddetails
    ) AS m
WHERE CaseNo = '$val'