单击PHP页面上的提交按钮时执行多个sql查询

单击PHP页面上的提交按钮时执行多个sql查询,php,mysql,submit,onsubmit,Php,Mysql,Submit,Onsubmit,我有一个PHP页面,单击submit按钮可以处理一些MySQL查询 在MySQL PHPMyAdmin中,查询100%工作,两个查询都执行。但是,在我的PHP代码中,查询不会执行 任何帮助将不胜感激,我打赌这是一个体面的PHP程序员简单的一个 提前谢谢你,瑞安 我的代码是: <?php mysql_connect("localhost", "hulamin_hulamin", "Hulamin2011")or die("cannot connect"); mysql

我有一个PHP页面,单击submit按钮可以处理一些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>
再次感谢,
Ryan

我认为这可能是你的问题

while($rows=mysql_fetch_array($result)){
应该是

while($rows=mysql_fetch_assoc($result)){
称之为

echo $rows['dispatcharea'];
编辑:

您还需要将两个查询拆分为两个单独的查询,因为您不能在一个mysqli_查询标记中运行两个查询

您需要将其拆分,如下所示:

 // 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
}

我认为这可能是你的问题

while($rows=mysql_fetch_array($result)){
应该是

while($rows=mysql_fetch_assoc($result)){
称之为

echo $rows['dispatcharea'];
编辑:

您还需要将两个查询拆分为两个单独的查询,因为您不能在一个mysqli_查询标记中运行两个查询

您需要将其拆分,如下所示:

 // 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文档,它不支持多个查询。PHPMyAdmin可能在执行查询之前分离查询。尝试将查询分为两部分。另外,PHP文档说不应该以分号结束mysql_查询,但这似乎没有什么坏处。

根据PHP文档,不支持多个查询。PHPMyAdmin可能在执行查询之前分离查询。尝试将查询分为两部分。另外,PHP文档说不应该以分号结束mysql_查询,但这似乎没有什么坏处。

如果您想同时执行多个查询,可以改用mysqli函数

有一个mysqli函数mysqli_multi_query,可用于一次执行多个查询

请参阅:

下面是以面向对象的方式使用mysqli_multi_查询对代码的粗略重写:

<?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>

如果您想同时执行多个查询,可以改用mysqli函数

有一个mysqli函数mysqli_multi_query,可用于一次执行多个查询

请参阅:

下面是以面向对象的方式使用mysqli_multi_查询对代码的粗略重写:

<?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>

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

我的代码是:

<?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>

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

我的代码是:

<?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>


为什么不在对mysql_query的两个单独调用中运行这两个查询呢?Hu@mishu,你能告诉我如何在现有代码上这样做吗?我如何在一次点击按钮时有两个动作?谢谢你为什么不在mysql_query的两个单独调用中运行这两个查询?Hu@mishu,你能告诉我如何在现有的代码上这样做吗?我如何在一次点击按钮时有两个动作?默认情况下,Thanksmysql_fetch_数组同时返回数值数组和关联数组。这是一个正确的观点,但这只是因为默认的结果类型是“MYSQL\u-BOTH”。它并不认为内容没有用处,但仍然值得注意的是,特定的函数是可用的。可能是,但肯定不是在answerHi中,输出从数组正确返回。它是不更新的mysql查询。有什么建议吗?请参考编辑和代码添加。即使使用单个mysql\u查询函数以分号分隔,也不能运行两个查询。默认情况下,mysql\u fetch\u数组同时返回数字数组和关联数组。这是一个正确的观点,但这只是因为默认的结果类型是“MYSQL\u-BOTH”。它并不认为内容没有用处,但仍然值得注意的是,特定的函数是可用的。可能是,但肯定不是在answerHi中,输出从数组正确返回。它是不更新的mysql查询。有什么建议吗?请参考编辑和代码添加。即使使用一个mysql_查询函数以分号分隔,也不能运行两个查询。您是对的@arxanas,PHPMyAdmin将其分解为两个查询。我怎样才能把它分成两部分?谢谢你的回答@arxanas,PHPMyAdmin将其分解为两个查询。我怎样才能把它分成两部分?很高兴看到这篇文章被否决。mysqli_multi_query非常适合这个实现——特别是在一个字符串中执行多个更新mysql查询。嗨@Luke,我读了一些你提供的链接,我如何在单击提交按钮时执行所有mysql语句?Thanks@RyanSmith请看我贴在上面的代码。不管怎样,我已经从mysql的角度重写了整段代码。非常感谢,你们太棒了!现在工作得很好!干杯,伙计们!很高兴听到!D
您是将查询拆分,还是最终使用mysqli?看到这篇文章被否决令人失望。mysqli_multi_query非常适合这个实现——特别是在一个字符串中执行多个更新mysql查询。嗨@Luke,我读了一些你提供的链接,我如何在单击提交按钮时执行所有mysql语句?Thanks@RyanSmith请看我贴在上面的代码。不管怎样,我已经从mysql的角度重写了整段代码。非常感谢,你们太棒了!现在工作得很好!干杯,伙计们!很高兴听到!您是将查询拆分,还是最终使用了mysqli?