Jump to content


Photo

insert into table using array


  • Please log in to reply
24 replies to this topic

#1 nel

nel
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 13 August 2006 - 12:13 PM

hi..
i want to know how to insert data using array in mysql where the data stored in field given? this field_name i taken from database....
thanks a lot..... ;)

#2 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 13 August 2006 - 12:24 PM

Sorry, I didn't understand any of that. Could you maybe give us some more information?

#3 Chetan

Chetan
  • Members
  • PipPipPip
  • Advanced Member
  • 162 posts
  • LocationIndia

Posted 13 August 2006 - 01:47 PM

U want to select data from 1 table and insert it into the other?
first select the data
and use foreach to keep executing the query using mysql_query
I am a PHP Guru, ask me questions if you want to

#4 nel

nel
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 13 August 2006 - 01:59 PM

it's not true...i don't want to insert into other table but use this table to determine which table and field i want to insert


i hope u will understand..

#5 Chetan

Chetan
  • Members
  • PipPipPip
  • Advanced Member
  • 162 posts
  • LocationIndia

Posted 13 August 2006 - 02:00 PM

Ok understood, quick thing
us a where clause
eg: I cant understand wat u say so eg
retreive id from table 1
update where id=wat got from table 1
I am a PHP Guru, ask me questions if you want to

#6 nel

nel
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 13 August 2006 - 02:06 PM

i'm not understand yet...
so can u give me example coding in insert this data?


#7 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 13 August 2006 - 02:24 PM

There are two ways of doing what you want to do, assuming I understand your question.

First way:

If your array consist of the field names as indices and the data as data, like:
<?php
$data = array('field1' => 'data1', 'field2'=>'data2', 'field3'=>'data3');
?>
then you can use a temporary array and the alternate format for the insert function:
<?php
$tmp = array();
foreach($data as $fld=>$val)
   $tmp[] = $fld . " = '" . mysql_real_escape_string($val) . "'";
$query = "insert into yourtable set " . implode(', ',$tmp);
echo $query;
?>
The second method is similar, except that the field names are in a separate array:
<?php
$fields = array('field1','field2','field3');
$data = array($data1,$data2,$data3);
$tmp = array();
for ($i=0;$i<count($data);$i++)
   $tmp[] = $fields[$i] . " = '" . mysql_real_escape_string($data[$i]) . "'";
$query = "insert into yourtable set " . implode(', ',$tmp);
echo $query;
?>

Ken

#8 nel

nel
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 13 August 2006 - 02:33 PM

thanks....

i'm 50% understand..

now, the problem is i take the data by request from previous page by using array(for) and  how i can determine which field i can insert in selected table.....can u give me others sample code...

thanks...

#9 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 13 August 2006 - 03:05 PM

ok this may also help.

<?php
$array= array('field1' => 'data1', 'field2' => 'data2'); 
$data= serialize($array); 
$sql= "INSERT INTO sometable (data_column) VALUES ('{$data}')"; 
?>

Also, I didn't realy understand what you meant by Your question. Can you post your code so that I may better understand what you mean or explain it better.

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#10 nel

nel
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 13 August 2006 - 03:52 PM

<?php
$array= array('field1' => 'data1', 'field2' => 'data2');
$data= serialize($array);
$sql= "INSERT INTO sometable (data_column) VALUES ('{$data}')";
?>

i not undersatand yet how to determine "(data_column)" in this query;
and how can i determine when have NULL value?

#11 Chetan

Chetan
  • Members
  • PipPipPip
  • Advanced Member
  • 162 posts
  • LocationIndia

Posted 14 August 2006 - 09:05 AM

You are saying there is information in one table about X and there is another table with info for X so you want to do something to those specific X column.

Well thats all I understand so, srry I cant help if I dun understand...
~.~
I am a PHP Guru, ask me questions if you want to

#12 nel

nel
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 14 August 2006 - 10:09 AM


$fields = array('field1','field2','field3');

$data = array($data1,$data2,$data3);


so how i can call this $field and $data because i use array (for) and field(field1, field2,...) i get from database (table 1) while $data1,$data2,.. , i get from post function.


* i don't know how to use array in one query for example field1, field2  !
thanks....

#13 Chetan

Chetan
  • Members
  • PipPipPip
  • Advanced Member
  • 162 posts
  • LocationIndia

Posted 14 August 2006 - 10:58 AM

I think it was olved but still have it

<?php
// MySQL
$conn=mysql_connect('localhost', 'root', '');
$db   =mysql_select_database('database');
$table='table';

// Declare feilds and data
$fields = array('field1','field2','field3');
$data = array($data1,$data2,$data3);

// The script
$num = count($field);

for($i=0; $i<$num; $i++)
{
$exec = '$sql = \'INSERT INTO '. $table.'($field['.$i.']) values($data['.$i.']);';

// For debugging, you may uncomment this code
// echo($exec);

eval($exec);
mysql_query($sql);
}
?>

This script is actually commplex but still Ill tell you what you have to do
in
$conn set your host, user and pass
$db set your database
$table set the table

for data and feilds you can use a script but make sure you you set $feilds to the feilds array and $data to the data

The for() is not that difficult but I prefer you dont touch it. You can put it anywhere in your script but make sure $table is set and $feilds and $data arrys are set.

I hope this is what you want
I am a PHP Guru, ask me questions if you want to

#14 nel

nel
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 14 August 2006 - 12:14 PM

[move] now i have 75% understanding...[/move]
this is for my knowledge. so i want to know :
1. why i must declare field and data in array()
2. what is the eval() function?
3. what is the code when i have more than one table, so how i can determine what is the field for this table. it because i want to insert the data in field that i had determined.

thanks a lot..... :D

#15 Chetan

Chetan
  • Members
  • PipPipPip
  • Advanced Member
  • 162 posts
  • LocationIndia

Posted 14 August 2006 - 12:52 PM

1. Uh because, if its not an array you would need to think of the names again and again which is not quite impossible.

2. (think about php.net) eval() function takes some php code and executes it. for egample you have some Php code in mysql db you can retreive it and execute it using eval()

3. So you want to put this stuff in not only 1 table but 2, ok.
Use this code

<?php
// MySQL
$conn=mysql_connect('localhost', 'root', '');
$db   =mysql_select_database('database');
$table=array('table1', 'table2', 'table3');

// Declare feilds and data
$fields = array('field1','field2','field3');
$data = array($data1,$data2,$data3);

// The script
$num2= count($table);
$num = count($field);

for($u=0; $u<$num2; $u++)
{
for($i=0; $i<$num; $i++)
{
$exec = '$sql = \'INSERT INTO $table['.$u.']($field['.$i.']) values($data['.$i.']);';

// For debugging, you may uncomment this code
// echo($exec);

eval($exec);
mysql_query($sql);
}
}
?>

In this case we are repeating the code by counting the number of tables. The names of tables are in $table as array
I am a PHP Guru, ask me questions if you want to

#16 Chetan

Chetan
  • Members
  • PipPipPip
  • Advanced Member
  • 162 posts
  • LocationIndia

Posted 14 August 2006 - 01:03 PM

Lemme check
I am a PHP Guru, ask me questions if you want to

#17 Chetan

Chetan
  • Members
  • PipPipPip
  • Advanced Member
  • 162 posts
  • LocationIndia

Posted 14 August 2006 - 01:05 PM

<?php
// MySQL
$conn=mysql_connect('localhost', 'root', '');
$db   =mysql_select_database('database');
$table=array('table1', 'table2', 'table3');

// Declare feilds and data
$fields = array('field1','field2','field3');
$data = array($data1,$data2,$data3);

// The script
$num2= count($table);
$num = count($field);

for($u=0; $u<$num2; $u++)
{
for($i=0; $i<$num; $i++)
{
$exec = '$sql = \'INSERT INTO $table['.$u.']($fields['.$i.']) values($data['.$i.']);';

// For debugging, you may uncomment this code
// echo($exec);

eval($exec);
mysql_query($sql);
}
}
?>

In case this dosent work uncomment the commented debugging line
I am a PHP Guru, ask me questions if you want to

#18 nel

nel
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 14 August 2006 - 03:44 PM

actually i want to insert data for example :

"insert in $table_name1 f$ield_name1, $field_name2, $field_name3 values $data1, $data2, $data3";

all this variable i get from select statement for example table & field
and i get from $_Request for example data.

all this variable, i also use array(for statement).

2. $fields = array($field1,$field2,$field3);
$data = array($data1,$data2,$data3);

how i can use array(for statement) in this array ?

can u give me sample code..

#19 Chetan

Chetan
  • Members
  • PipPipPip
  • Advanced Member
  • 162 posts
  • LocationIndia

Posted 14 August 2006 - 03:56 PM

Well i dont understand any of that.
What i understand is that you want to use for inside an array, put the code in a variable and eval() it
I am a PHP Guru, ask me questions if you want to

#20 nel

nel
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 14 August 2006 - 04:08 PM

this is sample inserting that i want to make :

"insert into table_name ($field[i], $field[i+1], $field[i+2]) values $data[i], $data[i+1];"

so can u give me sample code to in this example.

2. in the array(*,*,*)
* use for, for example : array(data[i], $data[i+1])

3. can you give me sample code, what is yout understand

thanks...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users