0

Been trying to get the values ​​of the checkbox in to the database but failed, anyone have tips on how I can get the values ​​of the checkbox in to my datbasen?

Then a question, how can I run post action in the same file?

My code so long:

<?php
$con=mysqli_connect("db_info");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$sql="INSERT INTO mass (gift, name, epost, interest) 
VALUES 
('$_POST[gift_nor]','$_POST[name_nor]','$_POST[epost_nor]','$_POST[interest_nor]')";


if (!mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error($con));
  }
echo "1 record added";

mysqli_close($con);
?>


<form method="post" action="">

    <input type="radio" name="gift_nor" value="present_1">present_1<br>
    <input type="radio" name="gift_nor" value="present_2">present_2<br />
    <input type="radio" name="gift_nor" value="present_3">present_3

    <hr />

    Namn: <input type="text" name="name_nor">
    Epostadress: <input type="text" name="epost_nor">

    <hr />

    <input type="checkbox" name="interest_nor[]" value="int_1">I have a bike<br>
    <input type="checkbox" name="interest_nor[]" value="int_2">I have a car<br />
    <input type="checkbox" name="interest_nor[]" value="int_3">I have a computer

    <hr />        

    <input name="Send" type="submit" id="Skicka">
</form>
1

1 Answer 1

3

use implode, do something like this

<?php
if (isset($_POST['Send']))
{
$con=mysqli_connect("db_info");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
  $check=implode(',', $_POST['interest_nor']); //it convert checkbox array into string

    $sql="INSERT INTO mass (gift, name, epost, interest) 
    VALUES 
    ('$_POST[gift_nor]','$_POST[name_nor]','$_POST[epost_nor]','$check')";
if (!mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error($con));
  }
echo "1 record added";

mysqli_close($con);
}
?>
Sign up to request clarification or add additional context in comments.

4 Comments

It saves the values ​​in the database now, but may be this error when I visit the page where the form are: Warning: implode() [function.implode]: Invalid arguments passed in /storage/content/67/130567/s10.se/public_html/db.php on line 18
tried what you wrote last, but when no value is sent to the database.
I copied what you wrote in the box above (the entire php code). Have I missed something? As I did so, nothing happens.
I have copied exactly what you typed in full. But now sends it does not collect any data to the database.

Your Answer

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