Jump to content

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
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.
<?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?

$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....
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
[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
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
[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
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..
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...
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]
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
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
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.