数组中的所有值都进入数据库?

时间:2017-04-28 18:15:45

标签: php mysql arrays

我如何将$ newd值放入数据库中。我现在这样做的方式只是放入一个值。我想要所有的值。

<?php
$checked = $_POST['options'];
for($i=0; $i < count($checked); $i++){
    $newd = "" . $checked[$i] . ",";
}   
    if(isset($_POST['loginbtn'])){
        if(!empty($order)){

if($money){
    //making the sql command
    $sql = "INSERT INTO `orders`(`order`,`date`,`time`,`timepass`,`money`,`corder`,`cancel`,`category`) VALUES ('$order','$date','$timenow','$time','$money','$corder','$cancel','$newd')";

    //querying the sql
    $query = mysqli_query($db,$sql);

    $lastid = mysqli_insert_id($db);

    $twosql = "INSERT INTO `comments`(`order_id`, `comment`,`user`,`time`,`timepass`) VALUES ('$lastid','$comment','$username','$timenow','$time')";
    $twoquery = mysqli_query($db,$twosql);

    header("Location: moneyorder.php"); 

}
?>

2 个答案:

答案 0 :(得分:1)

这里需要一个点,你没有连接新值:)

$newd .=

答案 1 :(得分:-1)

你必须在顶部定义第一个$ newd变量,否则它会给出错误 但请按照Alex Howansky的评论

进行操作
$checked = $_POST['options'];
    $newd = '';
    for($i=0; $i < count($checked); $i++){
        $newd .= "" . $checked[$i] . ",";
    } 
$newd = rtrim($newd, ',');

   // OR you can use

    $newd = implode(",", $checked );