Checkbox values into mysql query

后端 未结 2 617
梦谈多话
梦谈多话 2021-01-24 23:23

i have this code which permits me to retrieve data from a checkbox, there isn\'t any mysql included yet, so can you help me modify this?

The form is like this:



        
相关标签:
2条回答
  • 2021-01-24 23:38

    Use

    foreach($_POST[checkbox] as $key => $value {
        echo PHP_EOL . "Key is => " . $key . " Value is => " . $value . PHP_EOL;
    }
    

    Try this and see the output, you'll see yourself how to proceed further.

    0 讨论(0)
  • 2021-01-24 23:51

    Your POST variable ($_POST['checkbox']) is actually already an array. First, to figure out what you are actually working with, do this:

    echo '<pre>';
    print_r ($_POST['checkbox']);
    echo '</pre>';
    

    Then view your script and have a look at the output. Chances are you'll see an array with some keys and values. Using that you can decide how to proceed.

    If it were me I would do something like the following to accomplish your task:

    $sql = "SELECT `table_id_column`, `another_column` ";
    foreach ($_POST['checkbox'] as $key => $value) {
      $sql .= ", `$value`";
    }
    $sql .= " FROM `hostess` ORDER BY `another_colmn` ASC";
    

    Please keep in mind that allowing an SQL statement to be modified in this manner is very bad practice. You'll want to introduce some security into this before putting it on a production environment.

    Luke

    0 讨论(0)
提交回复
热议问题