Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

please forgive naivety and innocence...I am not a programmer! I have spent the best part of 4 days on this and I am ready for a PHP lesson or intense therapy.

Scenario: DB built in mySQL. Table with all columns varchar(50) except ID and age - both INT. See below, I just need a 'Yes' value in the checkbox linked colums/fields.

I want to insert data with a form that has both textboxes and checkboxes. I thought best way to do this was php array...??

Form:

<form action="process.php" method="post" enctype="multipart/form-data">
  <label>Childname
  <input type="text" name="textfield[childname]" />
  </label>
  <p>
    <label>Age
    <input type="text" name="textfield[age]" />
    </label>
  </p>
  <p>
    <label>Parent Name
    <input type="text" name="textfield[parent_name]" />
    </label>
  </p>
  <p>
    <label>Contact Number
    <input type="text" name="textfield[contact_no]" />
    </label>
  </p>
  <p>Subjects<br />
    <label>
    <input type="checkbox" name="checkbox[scratch]" value="checkbox" />
    Scratch</label> 
    <label>
    <input type="checkbox" name="checkbox[app_inventor]" value="checkbox" />
    App Inventor</label>
    <label>
    <input type="checkbox" name="checkbox[html]" value="checkbox" />
    HTML</label>
  </p>
  <p>Sessions Attended<br />
    <label>
    <input type="checkbox" name="checkbox[nov12]" value="checkbox" />
    Nov 2012</label>
  </p>
  <p>
    <label>
    <input type="checkbox" name="checkbox[dec12]" value="checkbox" />
    Dec 2012</label>
  </p>
  <p>&nbsp;</p>
  <p>
    <label>
    <input type="submit" name="Submit" value="Submit" />
    </label>
  </p>
</form>

PHP script:

<?php

include("config.php");

$childrecord = array("childname","age","parent_name","contact_no","scratch","app_inventor","html");

if(isset($_POST['childrecord'])){
    $childrecord = $_POST['childrecord'];
    $i = 0;
    foreach ($childrecord as $key => $value); {
    $i++;

    $sql="INSERT INTO tblchildren (childrecord) VALUES ($_POST['childrecord'])";
mysql_query($sql);

}

?>

Please help! Thanks in advance....

share|improve this question
 
What is your question/your problem? –  feeela Jul 4 at 12:12
 
there is no such element with name "childrecord" and you should learn some php before posting question here.. –  Rajeev Ranjan Jul 4 at 12:16
 
Obviously the PHP is not working. I have only learned a small amount and didn't know this forum was 'owned' by programmers (@Rajeev). –  user2550346 Jul 4 at 13:56
 
I've tried to use isset for the checkboxes but I get undefined index on the ones that aren't checked in the form, so I thought array, but I have the array syntax all wrong I think.... –  user2550346 Jul 4 at 14:40
 
@user2550346 this site is not owned by programmers but moderated by programmers. –  Rajeev Ranjan Jul 5 at 4:25
add comment

1 Answer

up vote 0 down vote accepted

You want to store your form data in your code.

For this you have to make following changes in your code and database.

In Form

note: Input field value should be relevant.

With your existing html code your process.php will get data in this structure


        Array
        (
            [textfield] => Array
                (
                    [childname] => dang
                    [age] => 18
                    [parent_name] => doctor
                    [contact_no] => 100
                )

            [checkbox] => Array
                (
                    [scratch] => checkbox
                    [app_inventor] => checkbox
                    [html] => checkbox
                    [nov12] => checkbox
                    [dec12] => checkbox
                )

            [Submit] => Submit
        )

So you need to modify your process.php

Before that you have to modify structure of your table, follow these steps

1. Delete your existing table
2. Create new table




 DROP TABLE tblchildren ;


         CREATE TABLE tblchildren
         (
           id INT AUTO_INCREMENT PRIMARY KEY,
           childname VARCHAR(30),
           age TINYINT,
           parent_name VARCHAR(30),
          contact_no VARCHAR(20),
           scratch ENUM('yes','no'),
           app_inventor ENUM('yes','no'),
           html ENUM('yes','no'),
           sesNov12Attnd ENUM('yes','no'),
           sesDec12Attnd ENUM('yes','no')
         );

Since you are new to php so i have used basic function, I will suggest you to use switch from mysql to mysqli

process.php

        <?php

            $con=mysql_connect("hostname","username","pass");
            $db=mysql_select_db('dbname', $con);

            //check form is submitted    
            if(isset($_POST)){

            //mysql_real_escape_string() prevents from sql injection.
            $childname= mysql_real_escape_string($_POST['textfield']['childname']);
            $age=mysql_real_escape_string($_POST['textfield']['age']);
            $parentName=mysql_real_escape_string($_POST['textfield']['parent_name']);
            $contactNo=mysql_real_escape_string($_POST['textfield']['contact_no']);


            $scratch=isset($_POST['checkbox']['scratch'])?'yes':'no';
            $appInventor=isset($_POST['checkbox']['app_inventor'])?'yes':'no';
            $html=isset($_POST['checkbox']['html'])?'yes':'no';
            $nov12=isset($_POST['checkbox']['nov12'])?'yes':'no';
            $dec12=isset($_POST['checkbox']['dec12'])?'yes':'no';

            $sql="INSERT INTO tblchildren(childname, age, parent_name, contact_no, scratch,app_inventor,html, sesNov12Attnd, sesDec12Attnd )
            VALUES
            ('$childname',$age,'$parentName','$contactNo','$scratch','$appInventor','$html','$nov12','$dec12')";

            mysql_query($sql);
            }else{
                echo "Form Not SUbmitted";
             }
        ?>
share|improve this answer
 
Thanks so much - got so far and on submisison of form got this error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ',,no,no,yes,no,no)' at line 2. I'm running XAMPP with MySQL version 5.5.27... –  user2550346 Jul 4 at 16:48
 
You need to modify query as :- $sql="INSERT INTO tblchildren(childname, age, parent_name, contact_no, scratch,app_inventor,html, sesNov12Attnd, sesDec12Attnd ) VALUES ('$childname',$age,'$parentName','$contactNo','$scratch','$appInventor','$html',‌​'$nov12','$dec12')"; –  Dr_Dang Jul 4 at 16:56
1  
why would you use enum? use a bit(1) or a tinyint(1) –  mc_fish Jul 4 at 17:03
 
Please let me know if there is any other problem . –  Dr_Dang Jul 4 at 17:03
 
@mc_fish , enum has better performance. for more details check :- link –  Dr_Dang Jul 4 at 17:07
show 4 more comments

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.