通过选择菜单选择特定行 - PHP / Mysql

时间:2017-01-12 15:08:02

标签: php mysql

嗨,大家好我正在为CMS的管理区工作。

我想知道的是如何从mysql数据库中选择一个商店的下拉菜单,然后使用该商店的详细信息将其提交到另一个页面以便开发票。我在PHP上的表现并不是很好,我看了一下但似乎无法找到答案。这是代码。谢谢!

<?php
session_start();
if (!isset($_SESSION['username'])){
    echo "You aren't logged in, please do so below<br>";
    include('loginform.php');
    exit();
}
?>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<?php 

$con = mysqli_connect('localhost', 'root', '', 'aurora');
if (!isset($con)) {
    echo "Connection to Aurora System failed.";
} 
$username = $_SESSION['username'];
echo "$username";

$query = "SELECT area FROM users WHERE username = '$username'";
$result = mysqli_query($con, $query);
while($row = mysqli_fetch_assoc($result)){
    $area = $row['area'];
}

$query2 = "SELECT * FROM shops WHERE county = '$area'";
$area_result = mysqli_query($con, $query2);

?>

<form action="time.php" method="POST">
<?php
echo '<br><select name="username">';
while($row2 = mysqli_fetch_assoc($area_result)){
    echo '<option value="'. $row2['shop_id'].'">' . $row2['shopname'] . ' -  ' .$row2['contactname']. ", "  .$row2['streetaddress'].  '</option>';

}
    $shopid = $row2['shop_id'];
    echo $shopid;
?>
</select><br><input type="submit" value="Register" name="register_button">   <br></form>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

卸下:

echo $shopid;

它可能无法解决您的问题,但您绝对不希望在select元素中添加额外的字符串

答案 1 :(得分:0)

不确定你要问的是什么,但我认为我自己代码的这一部分可能是它:

<select class="form-control" name="categoria">
                        <option value="">Selecciona una categoría</option>
                        <?php 
                        $q = 'SELECT * FROM categoria';
                        $resultado = mysqli_query($db,$q);
                        while($row = mysqli_fetch_assoc($resultado)){
                            $selected = "";
                            if(!empty($categoria) && $categoria == $row["categoria"]){
                                $selected = 'selected="selected"';
                            }
                            echo '<option value="'.$row["categoria"].'" '.$selected.'>'.$row["categoria"].'</option>';
                        }