2017-10-04 95 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') 
    ); 
} 

?>