Jump to content

insert into table using array


nel

Recommended Posts

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:
[code]
<?php
$data = array('field1' => 'data1', 'field2'=>'data2', 'field3'=>'data3');
?>[/code]
then you can use a temporary array and the alternate format for the insert function:
[code]<?php
$tmp = array();
foreach($data as $fld=>$val)
  $tmp[] = $fld . " = '" . mysql_real_escape_string($val) . "'";
$query = "insert into yourtable set " . implode(', ',$tmp);
echo $query;
?>[/code]
The second method is similar, except that the field names are in a separate array:
[code]<?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;
?>[/code]

Ken
Link to comment
Share on other sites

thanks....

[B]i'm 50% understand..[/B]

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...
Link to comment
Share on other sites

ok this may also help.

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

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.
Link to comment
Share on other sites

<?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?
Link to comment
Share on other sites

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...
~.~
Link to comment
Share on other sites


$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....
Link to comment
Share on other sites

I think it was olved but still have it

[code]
<?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);
}
?>
[/code]

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
Link to comment
Share on other sites

[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
Link to comment
Share on other sites

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

[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);
}
}
?>
[/code]

In this case we are repeating the code by counting the number of tables. The names of tables are in $table as array
Link to comment
Share on other sites

[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.']($fields['.$i.']) values($data['.$i.']);';

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

eval($exec);
mysql_query($sql);
}
}
?>
[/code]

In case this dosent work uncomment the commented debugging line
Link to comment
Share on other sites

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..
Link to comment
Share on other sites

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...
Link to comment
Share on other sites

The for loops
change em

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

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

eval($exec);
mysql_query($sql);
}
[/code]
Link to comment
Share on other sites

thanks foe the code..
but this code make m ucclear for example :

$flds.='$fields['.$i'.]';
when i make echo"$flds"; the output contain all of the field and not have comma(,) between field.

$dat.='$data['.$i.']';
when i make echo"$dat"; the output contain all of the data and not have comma(,) between data.

this all cause the data cannot be inserted.

please give me another code...thanks
Link to comment
Share on other sites

Uh sorry, Another way
[code]
for($u=0; $u<=$num2; $u++)
{
$flds='';
$dat='';
for($i=0; $i<=$num; $i++)
{
$flds.='$fields['.$i'.],';
$dat.='$data['.$i.'],';
}
$flds=substr($flds, 0, -1);
$dat=substr($dat, 0, -1);
$exec = '$sql = \'INSERT INTO $table['.$u.']($flds) values($dat]);';

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

eval($exec);
mysql_query($sql);
}
[/code]

This should do it
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.