Jump to content


Photo

insert into mysql table


  • Please log in to reply
2 replies to this topic

#1 ekalath

ekalath
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 19 July 2005 - 09:22 AM

I couldn't find a way to insert multiple records in a database through a single page. What i want is to make a suitable form to fill the data in and with a submit button to insert all the records to the database. Now i can do two records at a time with phpMyadmin application, but only two records at a time. If i want more what can i do

#2 neylitalo

neylitalo
  • Staff Alumni
  • Advanced Member
  • 1,854 posts
  • LocationMichigan, USA

Posted 20 July 2005 - 03:58 AM

All you would really do to insert more than one record is just build more than one query, and run all of them. Example:

[!--PHP-Head--][div class=\'phptop\']PHP[/div][div class=\'phpmain\'][!--PHP-EHead--][span style=\"color:#0000BB\"]<?php

$name [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#DD0000\"]\"Bob Jones\"[/span][span style=\"color:#007700\"];
[/span][span style=\"color:#0000BB\"]$phone [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#DD0000\"]\"123-4567\"[/span][span style=\"color:#007700\"];
[/span][span style=\"color:#0000BB\"]$email [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#DD0000\"]\"bob@email.com\"[/span][span style=\"color:#007700\"];

[/span][span style=\"color:#0000BB\"]$query1 [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#DD0000\"]\"INSERT INTO people(\"[/span][span style=\"color:#0000BB\"]name[/span][span style=\"color:#DD0000\"]\", \"[/span][span style=\"color:#0000BB\"]phone[/span][span style=\"color:#DD0000\"]\", \"[/span][span style=\"color:#0000BB\"]email[/span][span style=\"color:#DD0000\"]\") VALUES(\\"[/span][span style=\"color:#0000BB\"]$name[/span][span style=\"color:#007700\"]&[/span][span style=\"color:#FF8000\"]#092;\", \\"$phone\\", \\"$email\\")\";
[/span][span style=\"color:#0000BB\"]mysql_query[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$query1[/span][span style=\"color:#007700\"]);

[/span][span style=\"color:#0000BB\"]$name [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#DD0000\"]\"John Smith\"[/span][span style=\"color:#007700\"];
[/span][span style=\"color:#0000BB\"]$phone [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#DD0000\"]\"555-7890\"[/span][span style=\"color:#007700\"];
[/span][span style=\"color:#0000BB\"]$email [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#DD0000\"]\"john@email.com\"[/span][span style=\"color:#007700\"];

[/span][span style=\"color:#0000BB\"]$query2 [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#DD0000\"]\"INSERT INTO people(\"[/span][span style=\"color:#0000BB\"]name[/span][span style=\"color:#DD0000\"]\", \"[/span][span style=\"color:#0000BB\"]phone[/span][span style=\"color:#DD0000\"]\", \"[/span][span style=\"color:#0000BB\"]email[/span][span style=\"color:#DD0000\"]\") VALUES(\\"[/span][span style=\"color:#0000BB\"]$name[/span][span style=\"color:#007700\"]&[/span][span style=\"color:#FF8000\"]#092;\", \\"$phone\\", \\"$email\\")\";
[/span][span style=\"color:#0000BB\"]mysql_query[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$query[/span][span style=\"color:#007700\"]);

[/span][span style=\"color:#0000BB\"]?>[/span]
[/span][!--PHP-Foot--][/div][!--PHP-EFoot--]

This adds those two entries to the database... just keep going, assembling queries (or however you do it), and running them with mysql_query();
http://nealylitalo.net - My personal website, and home of The Netizen's Journal.

#3 dbdummy

dbdummy
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 27 August 2005 - 06:13 PM

a simpler way might be

[!--PHP-Head--][div class=\'phptop\']PHP[/div][div class=\'phpmain\'][!--PHP-EHead--]$insertsql = \"INSERT INTO `people` (`name`, `phone`, `email`)
VALUES (\'john doe\', \'555.555.5555\', \'john@john.com\'),
(\'joe doe\', \'555.555.5551\', \'joe@john.com\'),
(\'jane doe\', \'555.555.5552\', \'jane@john.com\'),
(\'jill doe\', \'555.555.5553\', \'jill@john.com\')\";
mysql_query($insertsql, $conn);
[/span][!--PHP-Foot--][/div][!--PHP-EFoot--]





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users