Handling data from multiple select field

后端 未结 3 1868
眼角桃花
眼角桃花 2021-01-20 22:39

I have a multiple select field designed as below:


                        
    
提交评论

  • 2021-01-20 23:24

    I had the same problem and this helped me. collect data in a form

    <form action="page.php" method="post">
    <select name="requests[]" multiple="multiple=" class="selectpicker">
    <option value="one">one</option>
    <option value="two">two</option>
    <option value="three">three</option>
    <option value="four">four</option>
    <option value="five">five</option>
    <option value="six">six</option>
    </select>
    <input type="submit" value="submit" />
    </form>
    

    using this as your page.php

        <?php 
         $contents = ob_get_contents(); //used this this to generate a variable that    can be posted ?> 
        <?php ob_start(); ?>
        <?php
        $requests=$_POST['requests'];
        if ($requests){
        foreach ($requests as $request){echo $request,','."";}
        } 
        ?>
        <?php $contents = ob_get_contents();//a variable that was generated ?>
        <?php 
        $h =$contents; // just renaming
        $sql = "INSERT INTO mytable (requests) VALUES (:h)";//dont forget to         connect to you database
        $q = $db->prepare($sql);
        $q->execute(array(':h'=>$h));
        header("location: mypage.php?"); //redirect to a page after posting into    mytable
        ?>
    
    0 讨论(0)
  • 2021-01-20 23:27

    First and foremost:

    Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

    It has become more important than ever that you follow this advice, ext/MySQL has now been deprecated and using it will emit E_DEPRECATED errors beginning with PHP 5.5, it will one day be completely removed from core language. Having said that, the following applies to all drivers for all databases.

    For the rest of this explanation I shall assume that you are unable to use MySQLi or PDO for some reason (note that the only satisfactory reason here is that they are not available, "I don't know how to use them" is not an excuse) and that you are forced to use ext/MySQL. If you are able to use either of the newer drivers, then you are able to use prepared statements, and none of this applies. So, with that in mind...

    Next let's take a look at what is wrong with the previous answer. This centres around escaping user input. It uses mysql_real_escape_string() is a way that really makes no sense whatsoever. This should be used for escaping a single string literal, and absolutely nothing else, ever. It cannot be used to effectively escape numbers, and it cannot be used to escaped parts of SQL that are not only values.

    The following two code snippets show the correct way to do this, dependent on what the data is and, crucially, its type.

    Here's what we would do if the values are strings (usually a CHAR or VARCHAR field):

    // First create an array of individually escaped values with quotes added
    $deds = array();
    foreach ($_POST['deductions'] as $ded) {
      $deds[] = "'".mysql_real_escape_string($ded)."'";
    }
    
    // Now join them together in an SQL syntax
    $deds_joined = join('), (', $deds);
    
    // Now they can safely be used in the query
    $query = "INSERT INTO mytable (deduction) VALUES ($deds_joined)";
    

    But often in this scenario, the values would simply be numbers, and in this case all we need to do is ensure that PHP is representing them with the correct data type, since they will be automatically safe when they are converted back to strings to be used in the query:

    // First convert the array values to integers
    $deds = array();
    foreach ($_POST['deductions'] as $ded) {
      $deds[] = (int) $ded;
    }
    
    // Now join them together in an SQL syntax
    $deds_joined = join('), (', $deds);
    
    // Now they can safely be used in the query
    $query = "INSERT INTO mytable (deduction) VALUES ($deds_joined)";
    

    This code obviously assumes that the data is of an integer type, floats can easily be handled by simply changing the (int) cast to (float).

    It's also worth noting that the string approach can be safely and successfully used for numeric values as well, because MySQL will also convert the values to the correct type. But in general it is better and more efficient to pass the data in with the correct type representation within the query.

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