1

我有一个 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>

再次感谢,瑞安

4

4 回答 4

4

根据 PHP 文档,mysql_query不支持多个查询。PHPMyAdmin 可能在执行查询之前将它们分开。尝试将您的查询分成两部分。(另外,PHP 文档说你不应该用分号结束 mysql_queries,但这似乎没有什么坏处。)

于 2011-10-05T11:15:53.887 回答
1

我认为这很可能是你的问题

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
}
于 2011-10-05T11:14:50.893 回答
1

如果你想一次执行多个查询,你可以切换到使用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>
于 2011-10-05T11:25:12.963 回答
0

我以为我会发布我现在正在工作的完整代码。感谢所有回答我问题的人,非常感谢。成为这个反应迅速的社区的一员真是太棒了。

我的代码是:

<?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>
于 2011-10-05T21:38:57.627 回答