PHP和MySQL选择框

时间:2013-12-03 20:06:29

标签: php mysql database

我为我的网站创建了注册表,但我有一个问题。

我为city,country,state和business_category创建了一个数据库。

此代码仅适用于其他国家/地区的原因无效?

这是注册表单页码选择选项:

    <?php   
  include("includes/function.php");

?>

<!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>
<style>
   body {
      margin:0px;
      padding:0px; 
       }
.registiration_form {
    width:415px;
    height:auto;
    margin-left:auto;
    margin-right:auto;
    margin-top:200px;
    }      
.name_surname{
    width:415px;
    height:40px;
    float:left;
    }
.sirket_adi {
    width:415px;
    height:40px;
    float:left;
    margin-top:10px;
    }
.sirket_adi input{

        width:420px;
        height:40px;
        text-align:left;
        text-indent:10px;
        font-size:18px;
        font-weight:bold;
        color:#a9b2bd;
        border: 1px solid #6ca8c6;  
        border-radius: 3px;
        float:left;
            } 
.email  {
    width:415px;
    height:40px;
    float:left;
    margin-top:10px;
    }
.email input{

        width:420px;
        height:40px;
        text-align:left;
        text-indent:10px;
        font-size:18px;
        font-weight:bold;
        color:#a9b2bd;
        border: 1px solid #6ca8c6;  
        border-radius: 3px;
        float:left;
            } 
.email-tekrar{
    width:415px;
    height:40px;
    float:left;
    margin-top:10px;
    }
.email-tekrar input{

        width:420px;
        height:40px;
        text-align:left;
        text-indent:10px;
        font-size:18px;
        font-weight:bold;
        color:#a9b2bd;
        border: 1px solid #6ca8c6;  
        border-radius: 3px;
        float:left;
            } 
.password{
    width:415px;
    height:40px;
    float:left;
    margin-top:10px;
    }
.password input{

        width:420px;
        height:40px;
        text-align:left;
        text-indent:10px;
        font-size:18px;
        font-weight:bold;
        color:#a9b2bd;
        border: 1px solid #6ca8c6;  
        border-radius: 3px;
        float:left;
            } 
.sirket_kategorisi{
    width:415px;
    height:20px;
    float:left;
    margin-top:10px;
    }
.sirket_kategorisi select{
        width:400px;
        height:20px;
        border: 1px solid #6ca8c6;  
        border-radius: 12px;    
        margin-left:12px;
            }
.kaydol {
    width:415px;
    height:60px;
    float:left;
    margin-top:30px;
    }
.kaydol_buton {
    float:left;
    width:300px;
    height:60px;
    margin-left:auto;
    margin-right:auto;
    }
.kaydol_buton input{
    width: 300px;
    height: 60px;
    padding: 0;
    -webkit-box-sizing: content-box;
    -moz-box-sizing: content-box;
    box-sizing: content-box;
    color: rgba(255,255,255,1);
    font: normal 37px/60px "Lucida Grande";
    border: none;
    border:1px solid #6ca8c6;
    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;
    border-radius: 5px;
    background-image: rgba(235,235,235,1);
    background-image: -webkit-linear-gradient(top, #299c0a 0%,#207001 100%);
    background-image: -moz-linear-gradient(top, #299c0a 0%,#207001 100%);
    background-image: -o-linear-gradient(top, #299c0a 0%,#207001 100%);
    background-image: -ms-linear-gradient(top, #299c0a 0%,#207001 100%);
    background-image: linear-gradient(top, #299c0a 0%,#207001 100%);
    -webkit-box-shadow: rgba(183,183,183,1) 0px 1px 2px 0px, inset rgba(255,255,255,1) 0px 51px 10px -50px;
    -moz-box-shadow: rgba(183,183,183,1) 0px 1px 2px 0px, inset rgba(255,255,255,1) 0px 51px 10px -50px;
    box-shadow: rgba(183,183,183,1) 0px 1px 2px 0px, inset rgba(255,255,255,1) 0px 51px 10px -50px;
}
.ulke_sehir{
    width:415px;
    height:30px;
    float:left;
    margin-top:3px;
    }
.ulke {
    float:left;
    width:150px;
    height:30px;
    margin-left:55px;
    }
.ulke select{
        width:150px;
        height:30px;
        border: 1px solid #6ca8c6;  
        border-radius: 12px;
        }
.sehir {
    float:left;
    width:150px;
    height:30px;
    margin-left:10px;
    }
.sehir select{
        width:150px;
        height:30px;
        border: 1px solid #6ca8c6;  
        border-radius: 12px;
        }
.name {
    float:left;
    width:200px;
    height:40px;
    }
.name input{
            float:left;
        width:200px;
        height:40px;
        text-align:left;
        text-indent:10px;
        font-size:18px;
        font-weight:bold;
        color:#a9b2bd;
        border: 1px solid #6ca8c6;  
        border-radius: 3px;
            } 
.surname{
    float:left;
    width:200px;
    height:40px;
    margin-left:15px;
    }
.surname input{
        width:200px;
        height:40px;
        text-align:left;
        text-indent:10px;
        font-size:18px;
        font-weight:bold;
        color:#a9b2bd;
        border: 1px solid #6ca8c6;  
        border-radius: 3px;
        float:left;
            } 

</style>
</head>

<body>
<form method='post' action='kaydol.php'>
<div class="registiration_form">
<div class="name_surname">
  <div class="name">
  <input type="name" value="Ad" name="name" autocomplete="off" />
  </div>
  <div class="surname">
  <input type="surname"  value="Soyad" name="surname" autocomplete="off"/>
  </div>
</div>
<div class="sirket_adi">
<input type="sirketadi" value="Şirket Adı" name="business_name" autocomplete="off" />
</div>

<div class="email"><input type="email" name="email" value="E-Mail" /></div>
<div class="email-tekrar"> <input type="emailagain"  value="E-Mail Tekrar" /></div>
<div class="password"> <input type="password"  name="password"value="Şifre" /></div>
<div class="sirket_kategorisi"><select name="business_category"><?php query()?>
</select>
<?php close()?>
</div>
<div class="ulke_sehir">
 <div class="ulke"><select name="country"><?php query()?>
</select>
<?php close()?>
</div>
 <div class="sehir"><select name="city"><?php query()?>
</select>
<?php close()?></div>
</div>
<div class="kaydol">
  <div class="kaydol_buton">
  <input type="submit" name="submit"  value="Gönder"/>
  </div>
</div>
</div>
</form>
</body>
</html>
<?php 
   if(isset($_POST['submit'])){
      echo $name = $_POST['name'];
       echo $surname = $_POST['surname'];
       echo $business_name = $_POST['business_name'];
       echo $email = $_POST['email'];
       echo $password = $_POST['password'];
       echo $business_category = $_POST['business_category'];
       echo $country = $_POST['country'];
       echo $city = $_POST['city'];


       }


?>

这段代码是我选择框的function.php代码

<?php 
include("connect.php");
function connect() {
mysql_connect($db_host,$db_user,$db_pass) or die('happy coding ' . mysql_error());
    mysql_select_db($db_name);
}


function close() {


mysql_close();

   }


function query() {
     $myData = mysql_query("SELECT * FROM country");

 while($record = mysql_fetch_array($myData)){
      echo '<option value="' . $record['country_name'] . '">' . $record['country_name'] . '</option>';
     } 

 }
 ?>

2 个答案:

答案 0 :(得分:0)

首先,您需要将要列出的表传递给查询函数

function query( $place) {
   $sql = "SELECT * FROM {$place}";
}

然后在适当的位置,您可以致电:

query('city');

query('country');

N.B请使用mysqliPDO函数代替旧的mysql_函数。

$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

function query($place) {
    global $mysqli;  // I don't like globals but this will fix your problem

    // create SQL statement as before
    $sql = "SELECT * FROM {$place}";
    $result = $mysqli->query($sql);
    while ($row = $result->fetch_array()) {
      // echo stuff here or build result string
    }
}

答案 1 :(得分:0)

连接不是全局的。您必须将它传递给函数内部。

$conn = mysql_connect(...);

function query( $conn, $statement ) {
    $query = mysql_query( $statement );

    ... 
}

query( $conn, "SELECT ... ");

在您使用此功能时,请停止使用mysql_功能。它们不再被维护and are officially deprecated。请参阅red box?转而了解prepared statements,并使用PDOMySQLi - this article将帮助您确定哪个。