Arrayed input value inserts empty record in mysql

后端 未结 2 1667
我在风中等你
我在风中等你 2021-01-16 05:42

I use 4 input boxes with same name.I will fill only one input box at a time and insert in DB using php.But doing following method inserts three empty records along with valu

2条回答
  •  北荒
    北荒 (楼主)
    2021-01-16 05:52

    The following code produces 1 INSERT statement. If as you suggest you are only using 1 value from your input, and can enforce this you can omit $value = rtrim($value, ","); which removes the trailing ,.

    I would also advise you to use either mysqli or PDO because mysql_functions are deprecated.

     $value = "";
    if(isset($_POST['submit'])){
        if(isset($_POST['abc'])){
            foreach($_POST['abc'] as $c) {
            if(!empty($c)){
            $value .= "'".$c."',";}
            }
            $value = rtrim($value, ",");
        }
        $insert="INSERT INTO item(productid) VALUES ($value)";
        echo $insert;//Remove after testing
    }   
    

提交回复
热议问题