checkbox to mysql

Discussion in 'PHP' started by plodos, Jan 30, 2008.

  1. #1
    maybe i didnt see whats wrong? :S
    these are my sql tables...
    
    CREATE TABLE `user` (
    `user_id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
    `first_name` VARCHAR( 40 ) NOT NULL ,
    `last_name` VARCHAR( 40 ) NOT NULL ,
    `email` VARCHAR( 128 ) NOT NULL ,
    `country` VARCHAR( 128 ) NOT NULL ,
    `university_dep` VARCHAR( 250 ) NOT NULL ,
    `university` VARCHAR( 250 ) NOT NULL ,
    `phone` VARCHAR( 30 ) NOT NULL
    ) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci;
    
    CREATE TABLE `editor` (
    `editor_id` SMALLINT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
    `editor` TEXT
    ) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci;
    
    CREATE TABLE `user_editor` (
    `user_id` INT NOT NULL ,
    `editor_id` SMALLINT NOT NULL ,
    PRIMARY KEY ( `user_id` , `editor_id` )
    ) ENGINE = MYISAM ; 
    
    Code (markup):

    my html & php form...
    
    <form action="_add.php" method="post">
    	<table width="900" border="0" cellspacing="1" cellpadding="2">
    		<tr>
    			<td width="100">Name</td>
    			<td><input name="name" type="text" id="name" size="30"></td>
    		</tr>
    
    <input type="checkbox" name="committee[]" value="ijh" /> Inter <br>
    			<input type="checkbox" name="committee[]" value="pwt" /> Proceed <br>
    			<input type="checkbox" name="committee[]" value="ijc" /> Int <br>
    
    <td><input name="add" type="submit" id="add" value="Add New User"></td>
    		</tr>
    	</table>
    </form>
    
    Code (markup):


    _add.php
    
    <?php
    	if(isset($_POST['add']) )	{
    
    include("dbconfig.php");
    
    $name = $_POST['name'];
    $surname = $_POST['surname'];
    $email = $_POST['email'];
    $country = $_POST['country'];
    $university = $_POST['university'];
    $university_dep = $_POST['university_dep'];
    $phone=$_POST['phone'];
    
    
    foreach ( $_POST['committee'] AS $committee ) {
      $query="INSERT INTO editor(editor) VALUES ('$committee')";
      $result = mysql_query($query) or die ("query not made");
    }
    
     
    
    $result= mysql_query("INSERT INTO user (first_name, last_name, email, country,university_dep,university,phone) VALUES ( ' $name ' , ' $surname ' , ' $email ' , ' $country ' , ' $university ' , ' $university_dep ' , ' $phone ')  "); 
    
    }
    else
    {
    include("index.html");
    }
    
    ?>
    
    Code (markup):
    and this is the output but still working incorrectly...

    but i just want to make , user will select the checkbox and all of the checked values will be inside of the editor table -> editor field..

    http://img264.imageshack.us/my.php?image=adszdfgdfpw9.jpg

    some of the user has 4 value, some of them 2 , some of them 1 and (Array) ...

    my mind is realy mixed
     
    plodos, Jan 30, 2008 IP
  2. darkmessiah

    darkmessiah Peon

    Messages:
    500
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #2
    here's the thing. you have an array of checkboxes. when you call the add.php from the form. you want to capture the array into something like, $test = $_POST["committee"]; you can than refer to the array like any other array..

    I see what your are doing by looping through each committee object, but you might want to make a test page that displays the results before sending to your database.

    I don't understand where you are passing name, surname, etc..

    ijh
    pwt
    ijc

    looks like the only values being passed to the array.
     
    darkmessiah, Jan 31, 2008 IP
  3. LittleJonSupportSite

    LittleJonSupportSite Peon

    Messages:
    386
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Where are all these coming form???

    
    $name = $_POST['name'];
    $surname = $_POST['surname'];
    $email = $_POST['email'];
    $country = $_POST['country'];
    $university = $_POST['university'];
    $university_dep = $_POST['university_dep'];
    $phone=$_POST['phone'];
    
    
    Code (markup):
    Would seem like your not passing these to the script from possbily the previous script.
     
    LittleJonSupportSite, Jan 31, 2008 IP
  4. lslars31

    lslars31 Peon

    Messages:
    260
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    are you trying to put an array into a db? You can't! You have to serialize() the array before it goes in, and unserialize() it when you try to pull it out again.
     
    lslars31, Jan 31, 2008 IP