2017-10-04 91 views
-1

我有一個由循環組成的selet的下拉列表,我需要從select中選擇要寫入數據庫的選定值,它現在將我寫爲數值的數字,並且我需要從下拉列表中的字符串 我的代碼select select into DB的結果

<form action="" id="equipment" method="post"> 
    <table> 
     <tr> 
      <td><label for="title">Add title</label></td> 
      <td><input type="text" name="title" id="title" value="" /></td> 
     </tr> 
     <tr> 
      <td><label for="anonce">Add anonce</label></td> 
      <td><textarea type="textarea" rows="4" cols="50" name="anonce" id="anonce" ></textarea></td> 
     </tr> 
     <tr> 
      <td><label for="url">Add url page of equipment</label></td> 
      <td><input type="text" name="url" id="url" /></td> 
     </tr> 
     <tr> 
      <?php echo rel_select(); ?> 
     </tr> 
     <tr> 
      <td><button type="submit" name="submit">Add equipment</button></td> 
     </tr> 

    </table> 
</form> 

<?php 

function rel_select(){ 
    global $wpdb; 
    $table_cat = $wpdb->prefix . 'rel_cat'; 
    $rel_cat = $wpdb->get_col("SELECT name FROM $table_cat"); 
?> 
<select name="cat-select"> 
<option value="">Select category</option> 
<?php 
foreach($rel_cat as $key => $value): 
    $key = 1; 
echo '<option value="'.$key.'">'.$value.'</option>'; 
endforeach; 
?> 
</select> 
<?php 
} ?> 


<?php 
global $wpdb; 
$table_name = $wpdb->prefix . 'rel_eq'; 
if (isset($_POST['submit'])){ 
    $wpdb->insert($table_name, array(
     'title' => $_POST['title'], 
     'url' => $_POST['url'], 
     'relcat' => $_POST['cat-select'], 
     'anonce' => $_POST['anonce']), 
     array('%s', '%s') 
    ); 
} 

?> 
+0

將選項的值設置爲$值。 –

+0

在選擇標籤中,「value」將是系統將要處理的值。在你的情況下,它是你的'$鑰匙'。您的「價值」僅用於顯示,而不是將要處理的實際數據。 https://www.w3schools.com/tags/tag_option.asp – hungrykoala

+1

'echo'';' –

回答

0

這裏是你想要的代碼。

<form action="" id="equipment" method="post"> 
    <table> 
     <tr> 
      <td><label for="title">Add title</label></td> 
      <td><input type="text" name="title" id="title" value="" /></td> 
     </tr> 
     <tr> 
      <td><label for="anonce">Add anonce</label></td> 
      <td><textarea type="textarea" rows="4" cols="50" name="anonce" id="anonce" ></textarea></td> 
     </tr> 
     <tr> 
      <td><label for="url">Add url page of equipment</label></td> 
      <td><input type="text" name="url" id="url" /></td> 
     </tr> 
     <tr> 
      <?php echo rel_select(); ?> 
     </tr> 
     <tr> 
      <td><button type="submit" name="submit">Add equipment</button></td> 
     </tr> 

    </table> 
</form> 

<?php 

function rel_select(){ 
    global $wpdb; 
    $table_cat = $wpdb->prefix . 'rel_cat'; 
    $rel_cat = $wpdb->get_col("SELECT name FROM $table_cat"); 
?> 
<select name="cat-select"> 
<option value="">Select category</option> 
<?php 
foreach($rel_cat as $key => $value): 
    $key = 1; 
echo '<option value="'.$value.'">'.$value.'</option>'; 
endforeach; 
?> 
</select> 
<?php 
} ?> 


<?php 
global $wpdb; 
$table_name = $wpdb->prefix . 'rel_eq'; 
if (isset($_POST['submit'])){ 
    $wpdb->insert($table_name, array(
     'title' => $_POST['title'], 
     'url' => $_POST['url'], 
     'relcat' => $_POST['cat-select'], 
     'anonce' => $_POST['anonce']), 
     array('%s', '%s') 
    ); 
} 

?>