簡體   English   中英

PHP和MySQL選擇框

[英]PHP and MySQL select box

我為我的網站創建注冊表格,但是有一個問題。

我為城市,國家/地區,州和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>';
     } 

 }
 ?>

首先,您需要將要列出的表傳遞給查詢函數

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

然后,您可以在適當的位置致電:

query('city');

query('country');

注意:請使用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
    }
}

連接不是全局的。 您必須將其傳遞到函數內部。

$conn = mysql_connect(...);

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

    ... 
}

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

在使用它時,請停止使用mysql_函數。 它們不再維護,已正式棄用 看到紅色框了嗎? 相反,要了解准備好的語句 ,並使用PDOMySQLi- 本文將幫助您確定哪一個。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM