-1

I need to insert the value of form into the data table.

here is my form for taking a test.

<form method="post" action="">
    1<input type="text" name="answer">
    3<input type="text" name="answer">
    5<input type="text" name="answer">
    7<input type="text" name="answer">
    <input type="submit" name="submit">
</form>

how to store this all answer in one field of the data table

i have tried this code but its not storing all value

if(isset($_POST['submit']))
{
    $answer = json_encode($_POST['answer']);
    $query = "INSERT INTO `test` (`answer`) VALUES ('$answer')";
    mysqli_query($con, $query);

    if (mysqli_query($con, $query)) {
        echo "inserted ";   
    } else {
        echo "Error updating record: " . mysqli_error($con);
    }
}
2
  • 1
    name the field answer[] then you can access as an array in php. How you process that array in PHP is up to you of course Commented Jun 11, 2020 at 6:07
  • So what sort of values does it store and what sort of values does it not store? Commented Jun 12, 2020 at 9:57

1 Answer 1

3

Try changing your HTML code to this.

<form method="post" action="">
    1<input type="text" name="answer[]">
    3<input type="text" name="answer[]">
    5<input type="text" name="answer[]">
    7<input type="text" name="answer[]">
    <input type="submit" name="submit">
</form>

When you submit the form the $_POST['answer'] would look something like this

Array
(
    [answer] => Array
        (
            [0] => first answer
            [1] => second answer
            [2] => third answer
            [3] => fourth answer
        )

)

Above is a printout of $_POST variable like so print_r($_POST)

Now you can easily get all the answer inputs inside PHP and have it converted to json like so

$answer = json_encode($_POST['answer']);

However since you are saving this inside mysql I would suggest using serialize() instead of json_encode(). Please read about serialize() https://www.php.net/manual/en/function.serialize.php

Sign up to request clarification or add additional context in comments.

4 Comments

Do you have any explanation why you would suggest serialize instead of json? If you serialize the the value, you have made the data in the database language specific. If anything, I would argue that they should normalize their database instead of storing multiple values in one column.
@MagnusEriksson you are correct and I agree with you. I suggested serialize() because it is faster and it's something I would use if I was building something like this. If this data is going places I would suggest json_encode for portability. Great question, thank you.
"I suggested serialize() because it is faster" - Actually, serialize and unserialize (which needs to be taken into consideration) are pretty slow so I wouldn't automatically agree with that.
It's only a suggestion, nobody actually needs to take my word for it. I've just found it to be more faster. This is a great topic to open for a discussions I'm sure there would be many mixed answers regarding this. Thank you for bringing it up @MagnusEriksson

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.