-1

How would i put the $newd values into the database. The way I'm doing it now just puts one value in. I want all values.

<?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"); 

}
?>
hassan
  • 7,812
  • 2
  • 25
  • 36
youngkid7
  • 93
  • 10
  • 1
    Your code is vulnerable to [**SQL injection attacks**](https://en.wikipedia.org/wiki/SQL_injection). You should use [**mysqli**](https://secure.php.net/manual/en/mysqli.prepare.php) or [**PDO**](https://secure.php.net/manual/en/pdo.prepared-statements.php) prepared statements with bound parameters as described in [**this post**](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). – Alex Howansky Apr 28 '17 at 18:17
  • Possible duplicate of [How to insert array of data into mysql using php](http://stackoverflow.com/questions/15013211/how-to-insert-array-of-data-into-mysql-using-php) – vv01f Apr 28 '17 at 18:25

2 Answers2

1

You need a dot here, you are not concatenating the new values :)

$newd .=
mayersdesign
  • 5,062
  • 4
  • 35
  • 47
-1

You have to define first $newd variable on top else it will give error but please do the things as suggested by Alex Howansky in comment

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

   // OR you can use

    $newd = implode(",", $checked );
Amit Gaud
  • 756
  • 6
  • 15