-1

I want the $colours to be put into the database as a list red,yellow,blue,green and an N/A at the beginning these are later to be called upon for a drop down box where the user selects one

    include'dbc.php';
    $colours = "N/A";

    if (isset($_POST['cb']) && is_array($_POST['cb'])) {
      foreach ($_POST['cb'] as $value) {
        $colours. = $value  ",";
      }
    };

    $sql="INSERT INTO items (category,title,description,qdescription,price,colours)
    VALUES ('$_POST[category]','$_POST[title]','$_POST[description]','$_POST[qdescription]','$_POST[price]','$colours')";

    if (!mysql_query($sql)){
      die('Error: ' . mysql_error());
    }

    mysql_close($db);
Lix
  • 47,311
  • 12
  • 103
  • 131
smilerjd
  • 3
  • 3

4 Answers4

3

The problem here is that you have a space between the . and = characters. If you want to concatenate strings, just leave out that space -

$colours .= $value . ",";

Notice also that I've added an additional . character after $value because you are wanting to do add that , as well.


One last note here would be about the use of the mysql_* functions. 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.

Until you migrate to these newer methods, you'll also want to take care to sanitize your data before inserting it into an SQL statement. As your code currently stands, it is extremely vulnerable to SQL injection.

Community
  • 1
  • 1
Lix
  • 47,311
  • 12
  • 103
  • 131
  • @smi - glad to help! Please take a look at my notes on the use of `mysql_*` functions... Happy coding! – Lix Feb 22 '13 at 08:12
1

change this

    $colours. = $value  ",";

to

$colours .= $value .",";
echo_Me
  • 37,078
  • 5
  • 58
  • 78
0

change this

$colours. = $value  ",";

to

$colours .= $value .",";
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
0

You have a space between your . and your = on line 13.

Nick Pickering
  • 3,095
  • 3
  • 29
  • 50