Jump to content

insert into mysql table


Recommended Posts

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

Link to comment
Share on other sites

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();

Link to comment
Share on other sites

  • 1 month later...

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--]

 

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.