Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to update one row in my database like this.

if (isset($_POST['submit'])) {
$sizes = array($_POST['size_king'], 
               $_POST['size_queen'],
               $_POST['size_double']
              );

mysqli_query($con, "UPDATE beds 
                SET `Available Sizes` = '$sizes' 
                WHERE ID = '$prod_id' " 
            );
}

Can anyone please help me?

I want this data to only update one row, and the data must be separated by a comma.

I am thinking maybe a FOR loop, but I'm not quite sure.

share|improve this question

3 Answers 3

up vote 3 down vote accepted

just use implode() function .

if (isset($_POST['submit'])) {
$sizes = array($_POST['size_king'], 
               $_POST['size_queen'],
               $_POST['size_double']
              );
$sizes=implode(",",$sizes);
mysqli_query($con, "UPDATE beds 
                SET `Available Sizes` = '$sizes' 
                WHERE ID = '$prod_id' " 
            );
}
share|improve this answer
    
Implode is a good solution as long as he/she wants add CSV values in database. –  Rajiv Pingale Mar 3 at 12:05
1  
@RajivPingale what we code just depends upon requirements. –  Rajeev Ranjan Mar 3 at 12:06
1  
So easy. AWESOME! Thanks. –  beginner Mar 3 at 12:10

The PHP implode function will serve your purpose.

implode joins array elements into a string separated by the glue we specify. The syntax is:

string implode ( string $glue , array $pieces )

Refer to: http://in3.php.net/manual/en/function.implode.php

Example:

<?php

$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);

echo $comma_separated; // lastname,email,phone

// Empty string when using an empty array:
var_dump(implode('hello', array())); // string(0) ""

?>
share|improve this answer

there is a catch when you create database never name your table as "Available Beds" i mean don't use space try using "AvailabaleSizes" or Available_Sizes or "availableSizes" after this change write your query like below.

($con, "UPDATE `beds`   SET Available_Sizes = '$sizes' WHERE ID = '$prod_id'");
share|improve this answer
    
I don't think that solves the problem. –  I Can Has Kittenz Mar 3 at 12:21
    
Have u tried? U should check ur html also –  Shehroz Asmat Mar 3 at 15:36
    
Implode fixed my problem. Thanks –  beginner Mar 4 at 12:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.