从select到DB的结果

时间:2017-10-04 14:10:22

标签: php html sql

我有一个由循环形成的selet的下拉列表,我需要从select中选择所选的值来写入数据库,它现在将我写为值的数字,我需要一个来自下拉列表 我的代码

<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') 
    );
}

?>

1 个答案:

答案 0 :(得分: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') 
    );
}

?>