Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 4 123 ... LastLast
Results 1 to 15 of 50
  1. #1
    New Coder
    Join Date
    Aug 2012
    Posts
    22
    Thanks
    12
    Thanked 0 Times in 0 Posts

    How to insert multiple checkbox values into one mysql table (field) using php

    Hi Guys,

    Looking for some desperately.

    I am new to php, with a lot search I have been able to put together some form of working script. The problem I am now facing is I have multiple checkboxes for the users to select from but the selected checkbox value does not get inserted into the table instead shows as “Array”. With some help I plan to get all the selected checkbox values into one table.

    I have two files one "testpage.html" which contains the form and the other "test_post.php" to process the form data. Please find below html and php file codes. Have to // in front of every code in html so the code can be seen.

    Really appreciate your help in advance .

    HTML form:
    PHP Code:
    <!DOCTYPE html>
    <html>
    <body background="blue">
    <form action="test_post.php" method="post">

    Full Name: <input name="FullName" type="text" id="FullName" /><br />
    <br />
    Number: <input name="Number" type="text" id="Number" /><br />
    <br />
    Email Address: <input name="EmailAddress" type="text" id="EmailAddress" /><br /><br />
    <br />
    Address: <input name="Address" type="text" id="Address" /><br />
    <br />
    Which animal do you want to keep?<br />
    <input type="checkbox" name="petathome[]" value="Dog" />Dog<br />
    <input type="checkbox" name="petathome[]" value="Cat" />Cat<br />
    <input type="checkbox" name="petathome[]" value="Lion" />Lion<br />
    <input type="checkbox" name="petathome[]" value="Tiger" />Tiger<br />
    <input type="checkbox" name="petathomel[]" value="Leopard" />Leopard
      
    <input type="submit" name="submit" value="Submit" />
      
    </form>
    </body>
    </html>



    PHP code:
    <?php
    $host
    ="localhost"// Host name 
    $username="username"// Mysql username 
    $password="password"// Mysql password 
    $db_name="dbname"// Database name 
    $tbl_name="tblname"// Table name 

    // Connect to server and select database.
    mysql_connect("$host""$username""$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB");

    // Get values from form 
    $FullName=$_POST['FullName'];
    $MobileNumber=$_POST['Number'];
    $EmailAddress=$_POST['EmailAddress'];
    $Address=$_POST['Address'];
    $petathome=$_POST['petathome'];

    // Insert data into mysql 
    $sql="INSERT INTO $tbl_name(FullName,Number, EmailAddress, Address, petathome)VALUES('$FullName', '$Number', '$EmailAddress', '$Address', '$petathome')";
    $result=mysql_query($sql);

    // if successfully insert data into database, displays message "Successful". 
    if($result){
    echo 
    "Successful";
    echo 
    "<BR>";
    echo 
    "<a href='testpage.html'>Back to main page</a>";
    }

    else {
    echo 
    "ERROR";
    }
    ?> 

    <?php 
    // close connection 
    mysql_close();
    ?>
    Last edited by Inigoesdr; 08-09-2012 at 02:49 AM.

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    $petathome=$_POST['petathome'];

    // You have to loop through the array of checked box values ...
    $pets="";
    foreach($petathome as $entry){
    $pets .= $entry.",";
    }

    // Insert data into mysql
    $sql="INSERT INTO $tbl_name(FullName,Number, EmailAddress, Address, petathome)VALUES('$FullName', '$Number', '$EmailAddress', '$Address', '$pets')";

  • Users who have thanked mlseim for this post:

    mohammedsali (08-06-2012)

  • #3
    New Coder
    Join Date
    Aug 2012
    Posts
    22
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Hi Mlseim,

    Thanks a ton for the reply.

    Still seem to be facing a problem, after entering the above into the php file and get the message:

    ERROR

    Any suggestions?

    Thanks & Regards,
    mohammedsali

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Repost your entire script again with the changes made.

  • Users who have thanked mlseim for this post:

    mohammedsali (08-07-2012)

  • #5
    New Coder
    Join Date
    Aug 2012
    Posts
    22
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Hi Mlseim,

    Thank you for taking the time to help me

    The html and php code is as below.

    HTML form:
    PHP Code:
    <!DOCTYPE html>
    <html>
    <body background="blue">
    <form action="test_post.php" method="post">

    Full Name: <input name="FullName" type="text" id="FullName" /><br />
    <br />
    Number: <input name="Number" type="text" id="Number" /><br />
    <br />
    Email Address: <input name="EmailAddress" type="text" id="EmailAddress" /><br /><br />
    <br />
    Address: <input name="Address" type="text" id="Address" /><br />
    <br />
    Which animal do you want to keep?<br />
    <input type="checkbox" name="petathome[]" value="Dog" />Dog<br />
    <input type="checkbox" name="petathome[]" value="Cat" />Cat<br />
    <input type="checkbox" name="petathome[]" value="Lion" />Lion<br />
    <input type="checkbox" name="petathome[]" value="Tiger" />Tiger<br />
    <input type="checkbox" name="petathomel[]" value="Leopard" />Leopard

    <input type="submit" name="submit" value="Submit" />

    </form>
    </body>
    </html>



    PHP code:
    <?php
    $host
    ="localhost"// Host name
    $username="username"// Mysql username
    $password="password"// Mysql password
    $db_name="dbname"// Database name
    $tbl_name="tblname"// Table name

    // Connect to server and select database.
    mysql_connect("$host""$username""$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");

    // Get values from form
    $FullName=$_POST['FullName'];
    $MobileNumber=$_POST['Number'];
    $EmailAddress=$_POST['EmailAddress'];
    $Address=$_POST['Address'];
    $petathome=$_POST['petathome'];

    // You have to loop through the array of checked box values ...
    $pets="";
    foreach(
    $petathome as $entry){
    $pets .= $entry.",";
    }

    // Insert data into mysql
    $sql="INSERT INTO $tbl_name(FullName, Number, EmailAddress, Address, petathome)VALUES('$FullName', '$Number', '$EmailAddress', '$Address', '$pets')"

    // if successfully insert data into database, displays message "Successful".
    if($result){
    echo 
    "Successful";
    echo 
    "<BR>";
    echo 
    "<a href='testpage.html'>Back to main page</a>";
    }

    else {
    echo 
    "ERROR";
    }
    ?>

    <?php
    // close connection
    mysql_close();
    ?>
    Please let me know if you want me to start all over again. With fresh table and form, php file. If so please let me know what is to be done.

    Thank you once again

    Regards,
    mohammedsali
    Last edited by Inigoesdr; 08-09-2012 at 02:50 AM.

  • #6
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Try this:

    PHP Code:
    <?php
    $host
    ="localhost"// Host name
    $username="username"// Mysql username
    $password="password"// Mysql password
    $db_name="dbname"// Database name
    $tbl_name="tblname"// Table name

    // Connect to server and select database.
    mysql_connect("$host""$username""$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");

    // Get values from form
    $FullName=$_POST['FullName'];
    $MobileNumber=$_POST['Number'];
    $EmailAddress=$_POST['EmailAddress'];
    $Address=$_POST['Address'];
    $petathome=$_POST['petathome'];

    // Loop through the array of checked box values ...
    $pets="";
    $flag=0;
    foreach(
    $petathome as $entry){
    $pets .= $entry."|";
    $flag=1;
    }
    if(
    $flag==1){
    $pets=rtrim($pets);
    }

    // Insert data into mysql
    $sql="INSERT INTO $tbl_name(FullName, Number, EmailAddress, Address, petathome)VALUES('$FullName', '$Number', '$EmailAddress', '$Address', '$pets')"
    $result=mysql_query($sql);

    // if successfully insert data into database, displays message "Successful".
    if($result){
    echo 
    "Successful";
    echo 
    "<BR>";
    echo 
    "<a href='testpage.html'>Back to main page</a>";
    }

    else {
    echo 
    "ERROR";
    }
    ?>

    <?php
    // close connection
    mysql_close();
    ?>



    .

  • Users who have thanked mlseim for this post:

    mohammedsali (08-07-2012)

  • #7
    New Coder
    Join Date
    Aug 2012
    Posts
    22
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Hi mlseim,

    That worked like a charm. Thanks a ton

    Need one more help with the checkbox is it possible to have multiple check boxes and have one check box with a text filled for user to add their choice which is not part of the check boxes. And this check box, text filed data is added along with the other checkbox values, data.

    Thanks again you are a life saver

    Regards,
    mohammedsali

  • #8
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    You'll need a text box.
    You will not need to add another checkbox though.
    If they put text in the box, that means they want it added to the list.
    If they leave the text box blank, it won't get added.

    Try this change first.
    I'm hoping that using the same name ("petathome[]") for the textbox
    will append it to the array, along with the checkbox values.

    PHP Code:
    <!DOCTYPE html>
    <
    html>
    <
    body background="blue">
    <
    form action="test_post.php" method="post">

    Full Name: <input name="FullName" type="text" id="FullName" /><br />
    <
    br />
    Number: <input name="Number" type="text" id="Number" /><br />
    <
    br />
    Email Address: <input name="EmailAddress" type="text" id="EmailAddress" /><br /><br />
    <
    br />
    Address: <input name="Address" type="text" id="Address" /><br />
    <
    br />
    Which animal do you want to keep?<br />
    <
    input type="checkbox" name="petathome[]" value="Dog" />Dog<br />
    <
    input type="checkbox" name="petathome[]" value="Cat" />Cat<br />
    <
    input type="checkbox" name="petathome[]" value="Lion" />Lion<br />
    <
    input type="checkbox" name="petathome[]" value="Tiger" />Tiger<br />
    <
    input type="checkbox" name="petathome[]" value="Leopard" />Leopard<br />
    Also: <input type="text" name="petathome[]"><br />
    <
    input type="submit" name="submit" value="Submit" />

    </
    form>
    </
    body>
    </
    html
    You are allowing user (form) values to be put into your MySQL table.
    That can be a security risk (SQL Injections) ... so escape them first ...

    PHP Code:
    // Get values from form 
    $FullName=mysql_real_escape_string($_POST['FullName']); 
    $MobileNumber=mysql_real_escape_string($_POST['Number']); 
    $EmailAddress=mysql_real_escape_string($_POST['EmailAddress']); 
    $Address=mysql_real_escape_string($_POST['Address']); 
    $petathome=mysql_real_escape_string($_POST['petathome']); 

    EDIT,
    I noticed a type on your Leopard checkbox line.
    I fixed it in my form (example above).
    It might still be bad in your form.

    <input type="checkbox" name="petathomel[]" value="Leopard" />Leopard



    .
    Last edited by mlseim; 08-07-2012 at 06:40 PM.

  • Users who have thanked mlseim for this post:

    mohammedsali (08-07-2012)

  • #9
    New Coder
    Join Date
    Aug 2012
    Posts
    22
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Hi mlseim,

    Thanks again

    There is a error when processing the script, all the fields expect the petathome fields are filled.

    The error is:
    Warning: mysql_real_escape_string() expects parameter 1 to be string, array given in /database/test_post.php on line 17
    Warning: Invalid argument supplied for foreach() in /database/test_post.php on line 22

    The html code is:
    PHP Code:
    <!DOCTYPE html>
    <html>
    <body background="blue">
    <form action="test_post.php" method="post">

    Full Name: <input name="FullName" type="text" id="FullName" /><br />
    <br />
    Number: <input name="Number" type="text" id="Number" /><br />
    <br />
    Email Address: <input name="EmailAddress" type="text" id="EmailAddress" /><br /><br />
    <br/>
    Address: <input name="Address" type="text" id="Address" /><br />
    <br />
    Which animal do you want to keep?<br />
    <input type="checkbox" name="petathome[]" value="Dog" />Dog<br />
    <input type="checkbox" name="petathome[]" value="Cat" />Cat<br />
    <input type="checkbox" name="petathome[]" value="Lion" />Lion<br />
    <input type="checkbox" name="petathome[]" value="Tiger" />Tiger<br />
    <input type="checkbox" name="petathome[]" value="Leopard" />Leopard<br />
    Also: <input type="text" name="petathome[]"><br />
    <input type="submit" name="submit" value="Submit" />

    </form>
    </body>
    </html>  

    The full php code is:
    <?php
    $host
    ="host"// Host name 
    $username="username"// Mysql username 
    $password="password"// Mysql password 
    $db_name="dbname"// Database name 
    $tbl_name="tblname"// Table name

    // Connect to server and select database.
    mysql_connect("$host""$username""$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");

    // Get values from form 
    $Name=mysql_real_escape_string($_POST['Name']); 
    $Number=mysql_real_escape_string($_POST['Number']); 
    $EmailAddress=mysql_real_escape_string($_POST['EmailAddress']); 
    $Address=mysql_real_escape_string($_POST['Address']); 
    $petathome=mysql_real_escape_string($_POST['petathome']);  

    // Loop through the array of checked box values ...
    $pets="";
    $flag=0;
    foreach(
    $petathome as $entry){
    $pets .= $entry.", ";
    $flag=1;
    }
    if(
    $flag==1){
    $pets=rtrim($pets);
    }

    // Insert data into mysql
    $sql="INSERT INTO $tbl_name(Name, Number, EmailAddress, Address, petathome)VALUES('$Name', '$Number', '$EmailAddress', '$Address', '$pets')"
    $result=mysql_query($sql);

    // if successfully insert data into database, displays message "Successful".
    if($result){
    echo 
    "Successful";
    echo 
    "<BR>";
    echo 
    "<a href='testpage.html'>Back to main page</a>";
    }

    else {
    echo 
    "ERROR";
    }
    ?>

    <?php
    // close connection
    mysql_close();
    ?>
    Last edited by Inigoesdr; 08-09-2012 at 02:50 AM.

  • #10
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Change this line:
    $petathome=mysql_real_escape_string($_POST['petathome']);

    To this:
    $petathome=$_POST['petathome'];


    Add the line in red ...

    if($flag==1){
    $pets=rtrim($pets);
    }
    $pets=mysql_real_escape_string($pets);

  • Users who have thanked mlseim for this post:

    mohammedsali (08-07-2012)

  • #11
    New Coder
    Join Date
    Aug 2012
    Posts
    22
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Hi mlseim,

    Works perfectly, thank you

    But when replace checkbox with radio the user is able to select any one radio options but what the enter in "Also:" textbox also gets entered into the database.

    Is there a way around this by putting a radio button in front of "Also:" and only if this radio button is select the text filed is enabled? Any thing you think will help will be great.
    Regards,
    mohammedsali
    Last edited by mohammedsali; 08-07-2012 at 08:18 PM.

  • #12
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Why are you inserting many values into a single field?
    Normalize that structure and create a second table to handle a many to one / many to many relationship. As it sits right now, you cannot properly select, update, or delete a single petsathome within this dataset without iterating EVERY record that's within it. If you have say 100K records, and you try searching for everyone whom has a Lion at home, you need to get every record in order to determine first if they have a lion at home. This is not ideal; doing so will see the time required even just for selections increment with each record added.
    And heaven forbid if you need to delete Lion or update Dog.

  • Users who have thanked Fou-Lu for this post:

    mohammedsali (08-07-2012)

  • #13
    New Coder
    Join Date
    Aug 2012
    Posts
    22
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Hi Fou-Lu,

    I am very new to MySQL and php not sure of how to create relationship(s) and don't think i would like to get into it as it may be very complex for me to handle later on.

    As far as the filtering and making changes to petathome goes the data under it is user filled and will not be changed.

    I do have a simple, raw .php file to search, filter based on FullName and export the complete database that i would like to make into one, but will come to that next.

    Thank you once again Fou-Lu for the suggestions, i really appreciate it

    Regards,
    mohammedsali

  • #14
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Fou-Lu,
    I'm guessing there are so many other things not quite right, there's no
    way the OP will be able to process it all. It appears as he said, a learning thing.
    But one might say, "learn it right, from the beginning". No way we're going to be
    able to do an online course using codingforums. So, I'm just letting it go with the flow.

    Mo ...
    OK ... so now you're wanting radio buttons instead of checkboxes?
    Create the new form with radio buttons, but the value of the one
    in front of <text> box will be value="x"
    Next, there will be some javascripting to detect if the radio button is
    clicked, and then enable the text box. I think you should do some Googling
    for: javascript radio button enable text box

    See what you find.

    If you can get the javascript to enable the text box, the PHP processing
    script will see if the radio button value is "x". If it is, it will use the text
    in the text box instead of the radio button value. If the radio button
    value is NOT "x", it will use the radio button value.



    .
    Last edited by mlseim; 08-07-2012 at 10:41 PM.

  • Users who have thanked mlseim for this post:

    mohammedsali (08-07-2012)

  • #15
    New Coder
    Join Date
    Aug 2012
    Posts
    22
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Hi mlseim,

    Thank you for pointing me the right direction

    Found a script and was able to make changes to fit my html file and it works

    The changes where needed only in the html file

    Html code:
    PHP Code:
    <!DOCTYPE html>
    <
    html>
    <
    body background="blue">
    <
    form action="test_post.php" method="post">

    Full Name: <input name="FullName" type="text" id="FullName" required="required" /><br />
    <
    br />
    Number: <input name="Number" type="text" id="Number" required="required" /><br />
    <
    br />
    Email Address: <input name="EmailAddress" type="text" id="EmailAddress" required="required" /><br /><br />
    <
    br/>
    Address: <input name="Address" type="text" id="Address" required="required" /><br />
    <
    br />
    Which animal do you want to keep?<br />
    <
    script type="text/javascript">
    var 
    currentEnabled null;
    function 
    enableElement(elem) {
    if (
    currentEnabled) {
    currentEnabled.disabled true;
    }
    elem.disabled false;
    currentEnabled elem;
    }
    </script>
    <form action="">
    <input type="radio" name="petathome[]" value="Dog"
    onclick="enableElement(this.form.elements['inp1']);"> Dog
    <br>
    <input type="radio" name="petathome[]" value="Cat"
    onclick="enableElement(this.form.elements['inp2']);"> Cat
    <br>
    <input type="radio" name="petathome[]" value="Lion"
    onclick="enableElement(this.form.elements['inp3']);"> Lion
    <br>
    <input type="radio" name="petathome[]" value="Tiger"
    onclick="enableElement(this.form.elements['inp4']);"> Tiger
    <br>
    <input type="radio" name="petathome[]" value="Leopard"
    onclick="enableElement(this.form.elements['inp5']);"> Leopard
    <br>
    <input type="radio" name="petathome[]" value="Others"
    onclick="enableElement(this.form.elements['inp6']);">
    Others: <input type="text" name="inp6" disabled="disabled">

    <br>
    <br />

    <input type="submit" name="submit" value="Submit" />

    </form>
    </body>
    </html> 
    Last edited by Inigoesdr; 08-09-2012 at 02:50 AM.


  •  
    Page 1 of 4 123 ... LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •