2

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.

3 Answers 3

3

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' " 
            );
}
Sign up to request clarification or add additional context in comments.

2 Comments

Implode is a good solution as long as he/she wants add CSV values in database.
@RajivPingale what we code just depends upon requirements.
0

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: https://www.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) ""

?>

Comments

-1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.