簡體   English   中英

單擊PHP頁面上的Submit按鈕時執行多個sql查詢

[英]perform multiple sql queries when clicking a submit button on PHP Page

我有一個PHP頁面,單擊“提交”按鈕將處理一些MySQL查詢。

在MySQL PHPMyAdmin中,查詢有效100%,並且兩個查詢均執行。 但是,在我的PHP代碼中,查詢不會執行。

任何幫助將不勝感激,我敢打賭這對於一個體面的PHP程序員來說是一個簡單的方法。

在此先感謝瑞安。

我的代碼是:

<?php
    mysql_connect("localhost", "hulamin_hulamin", "Hulamin2011")or die("cannot connect");    
    mysql_select_db("hulamin_loc")or die("cannot select DB");
    $sql="SELECT `dispatcharea`,`customer`,`casenumber`,`weight` from loaddetails where loadid = 0 order by dispatcharea";
    $result=mysql_query($sql);
    $count=mysql_num_rows($result);
?>
<html>
<head>
<title>
Plan Local PMB Delivery - Step 2
</title>
</head>
<html>


<table border=0>
    <tr>
        <td>
            <form name="form1" method="post">
                <table border=1>
                    <tr>
                        <td width=150>Dispatch Area</td>                        
                        <td width=300>Customer</td>  
                        <td width=150>Case Number</td>
                        <td width=100>Weight</td> 
                    </tr>
<?php
    while($rows=mysql_fetch_array($result)){
?>
                    <tr>
                        <td><?php echo $rows['dispatcharea']; ?></td>
                        <td><?php echo $rows['customer']; ?></td>
                        <td><?php echo $rows['casenumber']; ?></td>
                        <td><?php echo $rows['weight']; ?></td>
                    </tr>    

<?php
  }
?>      
</table>            
    <input name="Next" type="submit" id="Next" value="Next">                

<?php
if($_REQUEST['Next']=='Next') {
    {
        $sql="update loaddetails set loadid= (select max(loadid)+1 from loadcounterid) where loadid=0; update loadcounterid set loadid= (select max(loadid) from loaddetails) where loadid>0;";

        $final=mysql_query($sql);
        if($final)
        {
            echo "<meta http-equiv=\"refresh\" content=\"0;URL=planlocalpmbstep3.php\">";
        }
    } 
}

?>
</table>

</form>
</td>
</tr>
</table>

再次感謝,瑞安

根據PHP文檔, mysql_query不支持多個查詢。 PHPMyAdmin可能在執行查詢之前將查詢分開。 嘗試將查詢分為兩部分。 (此外,PHP文檔說您不應該以分號結束mysql_queries,但似乎沒有受到傷害。)

如果要一次執行多個查詢,則可以轉換為使用mysqli函數。

有一個mysqli函數mysqli_multi_query()可用於一次執行多個查詢。

請參考: http : //php.net/manual/en/mysqli.multi-query.php

這是使用面向對象樣式的mysqli_multi_query()粗略地重寫代碼:

<?php

    $link = mysqli_connect('localhost', 'hulamin_hulamin', 'Hulamin2011', 'hulamin_loc');
    $sql = "SELECT `dispatcharea`,`customer`,`casenumber`,`weight` from loaddetails where loadid = 0 order by dispatcharea";

    $result = $link->query($sql);
    $count = $result->num_rows($result);
?>
<html>
<head>
<title>
Plan Local PMB Delivery - Step 2
</title>
</head>
<html>


<table border=0>
    <tr>
        <td>
            <form name="form1" method="post">
                <table border=1>
                    <tr>
                        <td width=150>Dispatch Area</td>                        
                        <td width=300>Customer</td>  
                        <td width=150>Case Number</td>
                        <td width=100>Weight</td> 
                    </tr>
<?php
    while($rows = $link->fetch_array($result)){
?>
                    <tr>
                        <td><?php echo $rows['dispatcharea']; ?></td>
                        <td><?php echo $rows['customer']; ?></td>
                        <td><?php echo $rows['casenumber']; ?></td>
                        <td><?php echo $rows['weight']; ?></td>
                    </tr>    

<?php
  }
?>      
</table>            
    <input name="Next" type="submit" id="Next" value="Next">                

<?php
      if($_REQUEST['Next']=='Next'){
 {
                            $multi_sql = "update loaddetails set loadid= (select max(loadid)+1 from loadcounterid) where loadid=0;";
                            $multi_sql .= "update loadcounterid set loadid= (select max(loadid) from loaddetails) where loadid>0";

                            $final = $link->multi_query($multi_sql);

                            if($final)
                            {
                            echo "<meta http-equiv=\"refresh\" content=\"0;URL=planlocalpmbstep3.php\">";
                            }                                            } 
                                }

?>
</table>

</form>
</td>
</tr>
</table>

我認為這可能是您的問題

while($rows=mysql_fetch_array($result)){

應該

while($rows=mysql_fetch_assoc($result)){

稱他們為

echo $rows['dispatcharea'];

編輯:

您還需要將兩個查詢拆分為兩個單獨的查詢,因為您無法在一個mysqli_query()標記中運行兩個查詢。

您需要按如下所示拆分它們:

 // First update query
 $sql1="update loaddetails set loadid= (select max(loadid)+1 from loadcounterid) where loadid=0";

// Second update query
$sql2="update loadcounterid set loadid= (select max(loadid) from loaddetails) where loadid>0";

// Run both queries independently
$final_query1 = mysql_query($sql1);
$final_query2 = mysql_query($sql2);

// Check for query success
if ($final_query1 && $final_query2)
{
   // Success running the queries
}
else
{
   // Unsuccessful running the queries
}

我以為我只會發布現在可以正常工作的完整代碼。 感謝所有回答我的問題的人,對此深表感謝。 成為這個響應如此迅速的社區的一部分,真是太棒了。

我的代碼是:

<?php
    mysql_connect("localhost", "username", "password")or die("cannot connect");    
    mysql_select_db("dbname")or die("cannot select DB");
    $sql="SELECT `dispatcharea`,`customer`,`casenumber`,`weight` from loaddetails where loadid = 0 order by dispatcharea";
    $result=mysql_query($sql);
    $count=mysql_num_rows($result);
?>
<html>
<head>
<title>
Plan Local PMB Delivery - Step 2
</title>
</head>
<html>

<table>
<tr>
<td>
<center>
Load Number
</td>
<td>
<center>
Number of Cases
</td>
<td>
<center>
Load Weight
</td>
<td>
<center>
Other Detail
</td>
</tr>


<tr>
<td width=150>
<center>
<?php
$loadid = mysql_query('SELECT max(loadid)+1 FROM loaddetails');
if (!$loadid) {
    die('Could not query:' . mysql_error());
}
echo mysql_result($loadid, 0);
?>
</td>

<td width=150>
<center>
<?php
$nocases = mysql_query('SELECT count(*) FROM loaddetails where loadid = 0');
if (!$nocases) {
    die('Could not query:' . mysql_error());
}
echo mysql_result($nocases, 0);
?>
</td>

<td  width=150>
<center>
<?php
$weight = mysql_query('SELECT SUM(WEIGHT) FROM loaddetails where loadid = 0');
if (!$loadid) {
    die('Could not query:' . mysql_error());
}
echo mysql_result($weight, 0);
?>
</td>

<td  width=150>
<center>

</td>

</tr>

</table>




<hr>
<table border=0>
    <tr>
        <td>
            <form name="form1" method="post">
                <table border=1>
                    <tr>
                        <td width=150>Dispatch Area</td>                        
                        <td width=300>Customer</td>  
                        <td width=150>Case Number</td>
                        <td width=100>Weight</td> 
                    </tr>
<?php
    while($rows=mysql_fetch_array($result)){
?>
                    <tr>
                        <td><?php echo $rows['dispatcharea']; ?></td>
                        <td><?php echo $rows['customer']; ?></td>
                        <td><?php echo $rows['casenumber']; ?></td>
                        <td><?php echo $rows['weight']; ?></td>
                    </tr>    

<?php
  }
?>      
</table>            
    <input name="Next" type="submit" id="Next" value="Next">                

<?php
 // First update query 
 $sql1="update loaddetails set loadid= (select max(loadid)+1 from loadcounterid) where loadid=0"; 

// Second update query 
$sql2="update loadcounterid set loadid= (select max(loadid) from loaddetails) where loadid>0"; 



      if($_REQUEST['Next']=='Next'){
      // Run both queries independently 
$final_query1 = mysql_query($sql1); 
$final_query2 = mysql_query($sql2); 

if ($final_query1 && $final_query2) 
{ 
   // Success running the queries 
   echo "<meta http-equiv=\"refresh\" content=\"0;URL=planlocalpmbstep3.php\">";
} 
else 
{ 
   // Unsuccessful running the queries 
} 
}             

?>
</table>

</form>
</td>
</tr>
</table>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM