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は、クエリを実行する前にクエリを分離している可能性があります。クエリを2つの部分に分割してみてください。(また、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'];

編集:

また、1つのmysqli_query()タグで2つのクエリを実行することはできないため、両方のクエリを2つの別々のクエリに分割する必要があります。

以下に示すように、それらを分割する必要があります。

 // 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_multi_query()一度に複数のクエリを実行するために使用できるmysqli関数があります。

参照してください: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 に答える