GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-Dec-2006, 18:55
ctrohaya ctrohaya is offline
New Member
 
Join Date: Nov 2006
Posts: 29
ctrohaya is on a distinguished road

php insert function


hi guys. i have a little bit problem. now, i'm developing a very simple system. it include inserting multiple row concurrently.
normally, when we click submit button, the data will be inserted into one row, so now i want it be when we click submit, all the data will be inserted into multiple row concurrently.

row[0]=Banana
row[1]=Carrot
row[3]=pineapple
...
by single clicking.
  #2  
Old 09-Dec-2006, 18:57
Richardknox Richardknox is offline
Junior Member
 
Join Date: Nov 2006
Location: Michigan
Posts: 83
Richardknox is on a distinguished road

Re: php insert function


More info please. I guess I am not quite seeing what your trying to do.
  #3  
Old 09-Dec-2006, 20:21
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough

Re: php insert function


Read this howto I wrote many years ago, maybe it will point you in the right direction.

http://www.desilva.biz/mysql/insert.html
  #4  
Old 09-Dec-2006, 22:05
ctrohaya ctrohaya is offline
New Member
 
Join Date: Nov 2006
Posts: 29
ctrohaya is on a distinguished road

Re: php insert function


Quote:
Originally Posted by Richardknox
More info please. I guess I am not quite seeing what your trying to do.

ok.. the situation is like this;
banana = 1st row
pineapple = 2nd row
apple = 3rd row
...
to insert such 3 items, i have to click submit button 3 times. first click, banana will be inserted into 1st row, and if i have 10 items, i need 10 clicking.
so now i want to save times by insert all the items by single clicking. it mean, all the items inserted into different row concurrently.
  #5  
Old 09-Dec-2006, 22:14
ctrohaya ctrohaya is offline
New Member
 
Join Date: Nov 2006
Posts: 29
ctrohaya is on a distinguished road

Re: php insert function


Quote:
Originally Posted by JdS
Read this howto I wrote many years ago, maybe it will point you in the right direction.

http://www.desilva.biz/mysql/insert.html

it almost same, but i use a variable.

$fruit = banana
$fruit = pineapple
$fruit = apple
...
$sql = "INSERT INTO fruits(name, quantity)
VALUES
('$fruit', '$qtt')";

is it possible if such 3 items inserted all at once ?
  #6  
Old 13-Dec-2006, 04:34
PcFan PcFan is offline
New Member
 
Join Date: Dec 2006
Posts: 1
PcFan is on a distinguished road
Thumbs up

Re: php insert function


PHP Code:

$sql = "INSERT INTO fruits(name, quantity)
VALUES
('$fruit', '$qtt')"; 


will not work.

try something like:
PHP Code:

$sql = "INSERT INTO fruits(name, quantity)
VALUES
('banana', 3),('apple', 7), ('peach', 8)"; 



or, programatically, you will have to create an array containing the fruits and qty's, then compose a query string smt. like this:

PHP Code:

$sql = "INSERT INTO fruits(name, quantity)
VALUES ";

for (t=0; t< fruits.length; t++)
{
$sql_arr[] = '("'.$fruits[t].'", "'.$qty[t].'")';
}

$sql .= implode(',' , $sql_arr); 



Of course, the fruits and qty arrays will have the corresponding values you need. Or, you can use an associative and/or multidimensional array.

Best Regards, PcFan
Last edited by JdS : 13-Dec-2006 at 04:38. Reason: Please insert your PHP code between [php] & [/php] tags
  #7  
Old 13-Dec-2006, 04:40
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough

Re: php insert function


Why thank you, PcFan

I just pulled up this thread to reply and you did it already! Again, thank you.
  #8  
Old 13-Dec-2006, 20:47
ctrohaya ctrohaya is offline
New Member
 
Join Date: Nov 2006
Posts: 29
ctrohaya is on a distinguished road

Re: php insert function


i've tried the script given by PcFan, but it appear an error pointing at for loop, so i change the loop to be like this;
PHP Code:

$query = "SELECT count(*) FROM class AS c LEFT JOIN markah_pelajar AS m ON c.nokp = m.nokp AND m.subjek = '$f_subjek' WHERE c.kelas = '$f_kelas' AND c.sesi ='$sesi' AND m.nokp IS NULL";    
    $result = mysql_query($query,$conn);
    $num=mysql_numrows($result);
    echo $num;
//for (p=0; p<=$num; p++){
$p = 0;
while ($p <= $num){
  $query1 ="INSERT INTO markah_pelajar(nokp,sesi,subjek,jenis_pep,markah)
   values('$IC[$p]','$sesi[$p]','$f_subjek[$p]','$f_test[$p]','$f_mark[$p]')";
   $p++;
} 



also not work
and only the last row get into the dbase.
  #9  
Old 13-Dec-2006, 21:43
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough

Re: php insert function


I'll try to correct some of the typos in PcFan's example code...

PHP Code:

$sql = "INSERT INTO fruits(name, quantity)
VALUES ";

$fruits_length = sizeof( $fruits );

for( $i=0; $i<$fruits_length; $i++)
{
    $sql_arr[] = '("'.$fruits[$i].'", "'.$qty[$i].'")';
}

$sql .= implode( ',', $sql_arr ); 


 
 

Recent GIDBlogNARMY by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Message Class TransformedBG CPP / C++ Forum 5 29-Nov-2006 21:28
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 13:12
[Tutorial] Function Pointers aaroncohn CPP / C++ Forum 4 17-Feb-2006 11:33
Revising Script style ?????? pepee MySQL / PHP Forum 4 14-Apr-2004 04:59

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 03:14.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.