2016-02-07 83 views
-5
$db_user="root";<br> 
$db_pswd="";<br> 
$db_host="localhost";<br> 
$db_dbname="szabist";<br> 

$db_con=mysqli_connect($db_host,$db_user,$db_pswd,$db_dbname);<br> 
    if($db_con->connect_error){<br> 
     die("connecting error" . $db_con->connect_error()); <br> 
    }<br> 

$cat_query = $db_con->query('SELECT classname FROM category');<br> 
$cat_row = mysqli_fetch_array($cat_query);<br> 
echo ' 
    <table class="table-condensed"> 
     <tr> 
      <td><label>Book Title:</label></td> 
      <td><input type="text" name="book_title"></td> 
     </tr> 
     <tr> 
      <td><label>Category:</label></td> 
      <td><select name="category_id"> 
      <option value="'.$cat_row['classname'].'">'.$cat_row["classname"].'</option> 
      </select></td> 
     </tr>'; 
+0

<選擇name = 「CATEGORY_ID」>類別值<選項值=「」。$ cat_row [ '類名'] '」>'。$ cat_row [ 「類名」] 「。 –

+1

現在,你有什麼問題?你想要選擇包含查詢中的值的選項嗎? –

+0

寫一個關於你的具體問題的問題,我們不是在這裏猜測。這個'$ cat_row = mysqli_fetch_array($ cat_query);
'會拋出一個錯誤......或者我猜我浪費時間閱讀,第1行會拋出一個錯誤.. – chris85

回答

0
$db_user="root"; 
$db_pswd=""; 
$db_host="localhost";  
$db_dbname="szabist"; 

$db_con=mysqli_connect($db_host,$db_user,$db_pswd,$db_dbname); 
    if($db_con->connect_error){ 
     die("connecting error" . $db_con->connect_error); 
    } 

function add_new_book(){ 

global $db_con;  

$cat_query = $db_con->query('SELECT classname FROM category'); 
$cat_row = mysqli_fetch_array($cat_query); 
echo ' 
    <table class="table-condensed"> 
     <tr> 
      <td><label>Book Title:</label></td> 
      <td><input type="text" name="book_title"></td> 
     </tr> 
     <tr> 
      <td><label>Category:</label></td> 
      <td><select name="category_id"> 
      <option value="'.$cat_row['classname'].'">'.$cat_row["classname"].'</option> 
      </select></td> 
     </tr>'; }