update table using array

General discussions related to php

Moderators: macek, egami, gesf

User avatar
nadi
New php-forum User
New php-forum User
Posts: 9
Joined: Sun Mar 04, 2012 11:19 pm

update table using array

Postby nadi » Mon Oct 01, 2012 8:05 am

haiii....can any body example to me how to update base on table.

currently i have table doc1, doc2, doc3, doc4, doc5...i need to update by using array

here example mysql

UPDATE material set doc1 = $doc1 where id = $id

$result = mysql_query("UPDATE material SET doc1='$doc1' WHERE mat_id='$mat_id'")
or die(mysql_error());

how can i put array for doc?..i really confius how to do it n to make sure this table would not capture the same value each other n update the doc if not same i mean
1) if doc1=doc1 it won't update else doc1!=doc1 it will update the new value
2) if doc1==null or doc1=="" it update
3) this will keep loop to make sure it update a new id n not duplicate the id on each table

plz help.. :( :( :( :( :help: :help: :help: :help:

**sorry for my bad eng

User avatar
egami
php-forum GURU
php-forum GURU
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: update table using array

Postby egami » Mon Oct 01, 2012 9:13 am

So, why do you have so many tables? Why not one table, with multiple fields?

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: update table using array

Postby seandisanti » Mon Oct 01, 2012 2:19 pm

Do you mean you want $doc1 to reference an element of an array, or you want to pass in an array of updates? Also, either your explanation of your tables, or your sql update statement are wrong. your sql statement should read like:

Code: Select all

UPDATE table SET field=value WHERE condition=true


so what you're saying with your example is that the table being updated is material, and you're setting the field doc1 equal to the value stored in $doc1 where the id equals $id.

User avatar
nadi
New php-forum User
New php-forum User
Posts: 9
Joined: Sun Mar 04, 2012 11:19 pm

Re: update table using array

Postby nadi » Mon Oct 01, 2012 5:45 pm

sorry to make some confusing actually it's field not table.

here some in my mind and please correct me if I make a mistake.

$doc = array ("doc1", "doc2", "doc3","doc4","doc5");

UPDATE table SET $doc1[]=value WHERE condition=0001


I still wondering how to make it work! for make sure each field will not duplicate the value.

pls advise!

:oops: :oops:

User avatar
egami
php-forum GURU
php-forum GURU
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: update table using array

Postby egami » Tue Oct 02, 2012 7:33 am

if you're array looks like this..

Code: Select all


$array 
= array(
  
'field1' => 'value1',
  
'field2' => 'value2',
  
'field3' => 'value3');
 


Then you can make life super easy like this.. (And this is only true if the field name of your array is the same as your database table)

$query = "INSERT INTO table (".implode(',',array_keys($array)).") VALUES ('".implode("','",$array)."')";
$result = mysql_query($query);


And done.


Return to “PHP General”

Who is online

Users browsing this forum: No registered users and 2 guests

cron