显示下拉列表中的数据

时间:2014-10-09 11:11:43

标签: php mysql

我想根据从下拉列表中选择的内容显示数据库中的数据。例如,如果我选择tbl3并单击“提交”,则必须显示包含tbl3的所有数据。

期待一个合适的答案。谢谢大家!

这是我的代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
error_reporting(0);

include("../connection/connection.php");
//
if(!isset($_SESSION['u']))
{ 
header("location../resto/index.php");
//$cust_id=!isset($_GET['cust_id'])?0:$_GET['cust_id']; 

?>
<form method="post" action="">
<p>Table No.
  <select id="cust" name ="cust">
    <?php

$query1 = mysql_query("SELECT table_num, item, price, ca_id FROM tblcash WHERE  status='FINISHED' GROUP BY table_num");
$sid=!isset($_GET['id'])?0:$_GET['id'];
while($row = mysql_fetch_array($query1)){


        $ca_id = $row['ca_id'];
    $lamesa = $row['table_num'];
    if($_GET['id']==$row['ca_id']){



echo "<option value=\"$ca_id\" selected>$lamesa</option>";
    }
    else{
        echo "<option value=\"$ca_id\">$lamesa</option>";
    }
}

?>
  </select>
  <input type="submit" name="select_cust" />
</p>
<p>
  <?php

if(isset($_GET['id']))
{ 

$result = mysql_query("SELECT table_num, item, price FROM tblcash WHERE table_num=$sid AND status='FINISHED' ") or die(mysql_error());
echo "<table width='400' border='1' align='center'>";
echo "<tr>";
//echo "<td>Code</td>";
echo "<td align='center' style='font-size:20px;'>Tbl #</td>";
echo "<td align='center' style='font-size:20px;'>Item</td>";
echo "<td align='center' style='font-size:20px;'>Price</td>";
echo "<tr>";

while ($row=mysql_fetch_array($result))


{

    echo "<tr>";
    echo "<td>" .$row['table_num'];
    echo "<td>" .$row['item'];
    echo "<td>" .$row['price'];
    echo "<tr>";

}

echo "</table>";
}
}
?>
</p>
<br />

  <?php
if(isset($_GET['id'])){
$query3 = mysql_query("SELECT  SUM(price) FROM tblcash WHERE table_num=$sid AND status='FINISHED' GROUP BY table_num ");

$price = mysql_fetch_array($query3);

$price = round($price[0],2);

echo  "<div style=\"margin:0 auto;width:50%\" ><h2>TOTAL PRICE IS:$price</h2></div>";
}
?>

  <br />
  <br />
  <?php if(isset($_GET['id'])){ ?>
</p>
<div style="margin:0 auto;width:50%" ><input type="submit" name="save" value="SAVE" /></div>
<?php } ?>
</form>

</body>
</html>

<?php 

if(isset($_POST['select_cust'])){

    $selected = $_POST['cust'];
    header("location:counter2.php?id=$selected");



}


?>

<?php

if(isset($_POST['save'])){
    $customer_id = $_GET['id'];




    $query4 = mysql_query("SELECT  COUNT(*) FROM tblcash WHERE table_num=$customer_id AND status='FINISHED'  GROUP BY table_num") or die(mysql_error());

    if(mysql_num_rows($query4)>0){


    $message ='';
$test_pending=mysql_query("SELECT  COUNT(*) FROM tblcash WHERE table_num=$customer_id AND status='PENDING'  GROUP BY table_id");

if(mysql_num_rows($test_pending)>0){
    $message =' But there are dishes that are not yet cooked.';
}


    $query11 = mysql_query("SELECT  SUM(price) FROM tblcash WHERE table_num=$customer_id AND status='FINISHED'  GROUP BY table_num") or die(mysql_error());

$price1 = mysql_fetch_array($query11);

$price1 = round($price1[0],2);






    $query_delete = mysql_query("DELETE FROM tblcash WHERE table_num=$customer_id AND status='FINISHED'") or die(mysql_error());

    if($query_delete){


        $query_get_name = mysql_query("SELECT * from tblcustomer WHERE table_num=$customer_id") or die(mysql_error());
        $row = mysql_fetch_array($query_get_name);

        $tbl_name = $row['table_num'];


            $query_ins = mysql_query("INSERT INTO tblcashtrans(ca_id,table_num,total_price) VALUES ($ca_id,'$tbl_name',$price1)") or die(mysql_error());

            if($query_ins){
                 echo "<script>
                    alert(\"Successfully saved items.$message\");
                    window.location='cash2.php';
            </script>";

            }
            else{
                  echo '<script>
                    alert("Unsuccessfully saved items")
                    window.location="cash2.php";
            </script>';

            }


    }
    else{

         echo '<script>
                    alert("Unsuccessfully saved  items")
                    window.location="cash2.php";
            </script>';
        }

    }
    else{

         echo '<script>
                    alert("No Item to be saved")
                    window.location="cash2.php";
            </script>';
    }

}


?>

0 个答案:

没有答案