2011-10-13 48 views
0

我有一个显示mysql查询结果的PHP页面。每个返回的记录都会分配一个复选框,其值等于行ID列。PHP Mysql声明 - 多个语句

mysql语句原地插入选定的行到一个新的表。

我想添加第二个mysql查询,更新所选ID匹配的另一个表的状态。查询低于:

UPDATE despgoods_alldetails,loaddetails SET despgoods_alldetails.locstatus = 'LoadCreated' WHERE despgoods_alldetails.despgoodsid = $ VAL

的作品是目前网页PHP代码(我知道一些不正确的代码):

> <?php 
>  mysql_connect("localhost", "hulamin_hulamin", "Hulamin2011")or die("cannot connect");  
>  mysql_select_db("hulamin_loc")or die("cannot select DB"); 
>  $sql="SELECT `despgoodsid`,`crtd dept`,`customer`,`loc cust rel`,`case no`,`gross mass`,`case width`,`case length` from 
> despgoods_alldetails where transporttypename= 'localpmb' and 
> locstatus='unplanned' and customer <> 'customer'"; 
>  $result=mysql_query($sql); 
>  $count=mysql_num_rows($result); putenv("TZ=Africa/Johannesburg"); ?> <table border=0> 
>  <tr> 
>   <td> 
>    <form name="form1" method="post"> 
>     <table border=0 
>      <tr> 
>       <th>&nbsp;</th> 
>       <th width=150>Dispatch Area</th>      
>       <th width=150>Customer</th> 
>      <th width=150><center>Release Number</th> 
>      <th width=130><center>Case Number</th> 
>      <th width=80><center>Weight</th> 
>      <th width=80><center>Width</th> 
>      <th width=80><center>Length</th> 
>     </tr> <?php 
>  while($rows=mysql_fetch_array($result)){ ?> 
>      <tr> 
>       <td><input type="checkbox" name=check[] value="<?php echo $rows['despgoodsid']; ?>"></td> 
>       <td><?php echo $rows['crtd dept']; ?></td> 
>       <td><?php echo $rows['customer']; ?></td> 
>      <td><center><?php echo $rows['loc cust rel']; ?></td> 
>       <td><center><?php echo $rows['case no']; ?></td> 
>       <td><center><?php echo $rows['gross mass']; ?></td> 
>      <td><center><?php echo $rows['case width']; ?></td> 
>      <td><center><?php echo $rows['case length']; ?></td> 
>      
>      </tr>         
> 
> <?php 
>  } ?> 
>      <tr> 
>       <td colspan=3><input name="Next" type="submit" id="Next" value="Next"></td> 
>      </tr> 
>      <?php 
>      
>      
>        
>        $check=$_POST['check']; 
>        
>       if($_REQUEST['Next']=='Next'){ { 
>        $sql="INSERT INTO loaddetails (despgoodsid,dispatcharea,Customer, casenumber, weight, loadstatus) 
>       SELECT `despgoodsid`,`crtd dept`,Customer,`case no`,`gross mass`,'loadplanned' 
>       FROM despgoods_alldetails WHERE `despgoodsid` = '$val'"; 
>        
>        foreach($check as $key=>$value) 
>        { 
>        $sql="INSERT INTO loaddetails (despgoodsid,dispatcharea,Customer, casenumber, weight, loadstatus) 
>       SELECT `despgoodsid`,`crtd dept`,Customer,`case no`,`gross mass`,'loadplanned' 
>       FROM despgoods_alldetails WHERE `despgoodsid` = '$value'"; 
>        $final=mysql_query($sql); 
>        if($final) 
>        { 
>        echo "<meta http-equiv=\"refresh\" content=\"0;URL=http://www.hulaminloc.co.za/planningplatform/planlocalpmbstep2.php\">"; 
>        }           } 
>         } 
>         } 
>     mysql_close(); ?> </table> </form> </td> </tr> </table> 

我怎样才能添加我的更新语句运行除了选择语句每个选定的行?

任何帮助表示赞赏。

感谢和问候, 莱恩史密斯

回答

1

你可以创建一个触发器,它会火的时候,数据被保存在二级表;检查MySQL手册触发器,如果​​你认为这可能是你需要的

+0

嗨,上面引用的PHP页面将数据插入loaddetails表。然后我需要改变插入行的产品表的状态。我不确定触发器是否会对我所选的行进行调整。感谢@mishu – Smudger

+0

考虑到需要更新的表是必须更新触发器的查询的一部分,在这里不是这种情况;所以我认为你不能避免使用两个单独的SQL查询;只需运行插入后已有的更新查询 – mishu

+0

注意:它看起来像代码将在系列中的第一次插入后重定向 – mishu

0

我虽然我会在这里发布完整的工作代码。对于所有的帮助和耐心,大到@mishu。

<?php 
    mysql_connect("localhost", "user", "password")or die("cannot connect");  
    mysql_select_db("database")or die("cannot select DB"); 
    $sql="SELECT `despgoodsid`,`crtd dept`,`customer`,`loc cust rel`,`case no`,`gross mass`,`case width`,`case length` from despgoods_alldetails where transporttypename= 'localpmb' and locstatus='unplanned' and customer <> 'customer'"; 
    $result=mysql_query($sql); 
    $count=mysql_num_rows($result); 
    putenv("TZ=Africa/Johannesburg"); 

?> 
<table border=0> 
    <tr> 
     <td> 
      <form name="form1" method="post"> 
       <table border=0 
        <tr> 
         <th>&nbsp;</th> 
         <th width=150>Dispatch Area</th>       
         <th width=150>Customer</th> 
         <th width=150><center>Release Number</th> 
         <th width=130><center>Case Number</th> 
         <th width=80><center>Weight</th> 
         <th width=80><center>Width</th> 
         <th width=80><center>Length</th> 
        </tr> 
<?php 
    while($rows=mysql_fetch_array($result)){ 
?> 
        <tr> 
         <td><input type="checkbox" name=check[] value="<?php echo $rows['despgoodsid']; ?>"></td> 
         <td><?php echo $rows['crtd dept']; ?></td> 
         <td><?php echo $rows['customer']; ?></td> 
         <td><center><?php echo $rows['loc cust rel']; ?></td> 
         <td><center><?php echo $rows['case no']; ?></td> 
         <td><center><?php echo $rows['gross mass']; ?></td> 
         <td><center><?php echo $rows['case width']; ?></td> 
         <td><center><?php echo $rows['case length']; ?></td> 

        </tr>         

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



          $check=$_POST['check']; 

         if($_REQUEST['Next']=='Next'){ 
{ 



$maxloadid = ' 
select 
max(loadid) +1 
from 
loaddetails 
'; 
$resultmaxloadid = mysql_query($maxloadid); // run the query 

// there was an error - output the message 
if (!$resultmaxloadid) 
{ 
echo 'there was an error in your query'; 
echo mysql_error(); 
die; 
} 

$loadid = mysql_result($resultmaxloadid, 0); 

foreach($check as $key=>$value) // 
{ 
// get the details for one of the selected values 
$query = ' 
SELECT 
`despgoodsid`, 
`crtd dept`, 
`Customer`, 
`case no`, 
`gross mass` 
FROM 
despgoods_alldetails 
WHERE 
`despgoodsid` = "'. mysql_real_escape_string($value) . '" 
'; 

$result = mysql_query($query); // run the query 

// there was an error - output the message 
if (!$result) 
{ 
echo 'there was an error in your query'; 
echo mysql_error(); 
die; 
} 

// we got here, everything is fine 

if (!mysql_num_rows($result)) // the above query did not fetch any records, so there's a problem 
{ 
continue; // skip this record.. "continue" will ignore the rest of the foreach loop and go to the next step 
} 

$details = mysql_fetch_assoc($result); 

// inset the values 
$query = ' 
INSERT INTO 
loaddetails 
SET 
despgoodsid = "'.$details['despgoodsid'].'", 
dispatcharea= "'.$details['crtd dept'].'", 
Customer = "'.$details['Customer'].'", 
casenumber = "'.$details['case no'].'", 
weight = "'.$details['gross mass'].'", 
loadstatus= "loadplanned", 
loadid = "'.$loadid.'" 
'; 

$result = mysql_query($query); // run the query 

// there was an error - output the message 
if (!$result) 
{ 
echo 'there was an error in your query'; 
echo mysql_error(); 
die; 
} 

// update the status 
$query = ' 
UPDATE 
despgoods_alldetails 
SET 
locstatus ="LoadCreated" 
WHERE 
despgoodsid = "'.mysql_real_escape_string($value) . '" 
'; 
$result = mysql_query($query); // run the query 

// there was an error - output the message 
if (!$result) 
{ 
echo 'there was an error in your query'; 
echo mysql_error(); 
die; 
} 


} // end of foreach statement 
if($final) 
{ 
echo "<meta http-equiv=\"refresh\" content=\"0;URL=http://www.hulaminloc.co.za/planningplatform/index.phpx`\">"; 
} 



           } 
           } 

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

再次感谢@Mishu和网站上的所有其他贡献者。