Page 1 of 1

implode() function ?? insert into field?

Posted: Wed Jan 30, 2008 12:18 pm
by plodos
***** PLEASE USE THE

Code: Select all

TAG *****[/color]

Code: Select all

<input type="checkbox" name="committee[]" value="ijh" /> Inter
 
<input type="checkbox" name="committee[]" value="pwa" /> Proced1
 
<input type="checkbox" name="committee[]" value="ijc" /> Inter2
.................................................................................................... ...
these are the methods and query

Code: Select all

$committee = $_POST['committee'];
$arr=array($committee);
$text = implode("),(", $arr);
$query="INSERT INTO editor(editor) VALUES ('({$text})')";
$result = mysql_query($query) or die ("query not made");
but

Code: Select all

$data1 = mysql_query("SELECT * FROM editor where editor_id={$info['user_id']} ") or die(mysql_error());
while($info1 = mysql_fetch_array($data1)){
Print "<th>Committee:</th> <td>{$info1['editor'] } </td>";}
the printout is like that " Committee: (Array) "

i just want to see the checkbox values in the editor field and HTML table:)

help me plss

Re: implode() function ?? insert into field?

Posted: Wed Jan 30, 2008 12:23 pm
by Christopher

Code: Select all

$text = implode("', '", '$arr);
$query="INSERT INTO editor(editor) VALUES ('{$text}')";
 

Re: implode() function ?? insert into field?

Posted: Wed Jan 30, 2008 12:42 pm
by plodos
arborint wrote:

Code: Select all

$text = implode("', '", '$arr);
$query="INSERT INTO editor(editor) VALUES ('{$text}')";
 
 

unfortunately, still it is not working output is " Committee: (Array) "

Re: implode() function ?? insert into field?

Posted: Wed Jan 30, 2008 1:04 pm
by Zoxive

Code: Select all

$committee = $_POST['committee'];
$arr=array($committee); // this is your problem remove it
$text = implode("),(", $arr); // Wrong Syntax for sql

Re: implode() function ?? insert into field?

Posted: Wed Jan 30, 2008 1:38 pm
by plodos
maybe i didnt see whats wrong? :S
these are my sql tables...

Code: Select all

 
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 ; 
 
my html & php form...

Code: Select all

 
<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>
 
_add.php

Code: Select all

 
<?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");
}
 
?>
 

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?imag ... gdfpw9.jpg

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

my mind is realy mixed

Re: implode() function ?? insert into field?

Posted: Wed Jan 30, 2008 2:08 pm
by Christopher
It looks like you need to do:

Code: Select all

$committee = implode(",", $_POST['committee']);
The use that variable in your SQL. You should filter, validate and escape all values though.