473,513 Members | 3,895 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

insert multiple records into one table (one form)

Hi all,

Please excuse my limited technical knowledge of PHP, so far i have only
created PHP sites using Dreamweaver, i find know that i need features
outside of it's capabilities.

I am trying to create a 'related articles' selection page. The
administrator, after creating an article, can select older articles to
include on the final displayed page.

As the number of older articles increases, so will the number of
possible relataed articles, so i am making a dynamic checkbox as the
selection. This is all covered in Dreamweavers features, but, what i
then need to be able to do is create a new record in the
'related_articles' table for each article selected when the form is
submitted.

I found this post below, which is more or less exactly what i want to
do, i just wasn't sure that this was php code, looks like asp. Would
someone be kind enough to show me how to change it to php code, or even
write the code for me? ANY help at all would be much appreciated.

http://groups.google.ca/group/macrom...41afa2366e40e2

Thanks,

Gavin

Apr 14 '06 #1
7 14605
NC
GJ*******@gmail.com wrote:

I am trying to create a 'related articles' selection page. The
administrator, after creating an article, can select older articles to
include on the final displayed page.
Is this the best way to do it? You could generate a list of related
articles dynamically, based on article's publication date and topic
(which, of course, means that you need to keep track of topics in your
database).
As the number of older articles increases, so will the number of
possible relataed articles, so i am making a dynamic checkbox as the
selection. This is all covered in Dreamweavers features, but, what i
then need to be able to do is create a new record in the
'related_articles' table for each article selected when the form is
submitted.
So what seems to be the problem? You receieve several ID numbers or
URLs via POST, and run one or more INSERT queries with this data.
I found this post below, which is more or less exactly what i want to
do, i just wasn't sure that this was php code, looks like asp. Would
someone be kind enough to show me how to change it to php code,
or even write the code for me?


Well, so far you haven't even mentioned what database you are using...

Cheers,
NC

Apr 14 '06 #2
Hi,

Maybe i should re-phrase my question. I'm using php with a MySQL
database, and i would like to know how to insert a new record into a
table for each checked item on one form.

The reason i want to create a relation in this way, is because i want
to be able to choose which article appears as a related link regardless
of it's category/date/etc.
So what seems to be the problem? You receieve several ID numbers or
URLs via POST, and run one or more INSERT queries with this data.


No problem with this, it's just this is what i'm not sure of how to do.

Thanks, Gavin

Apr 14 '06 #3
NC
GJ*******@gmail.com wrote:

Maybe i should re-phrase my question. I'm using php with a MySQL
database, and i would like to know how to insert a new record into a
table for each checked item on one form.


OK, let's say you have a form:

<form method="POST" action="insert.php">
<input type="checkbox" name="id[]" value="32">Article #32<br>
<input type="checkbox" name="id[]" value="38">Article #38<br>
<input type="checkbox" name="id[]" value="45">Article #45<br>
<input type="checkbox" name="id[]" value="59">Article #59<br>
<input type="Submit">
</form>

Now, let's assume the user checked articles #32 and #59. These values
will be available to insert.php as fields in $_POST['id'], which in
this case will be an array. So in insert.php you can write:

$query = 'INSERT INTO related_articles (id) VALUES (' .
implode('), (', $_POST['id']) . ')';
$result = mysql_query($query)
or die('Could not execute INSERT query');

That's it, really...

Cheers,
NC

Apr 15 '06 #4
Thanks for the reply NC, the trouble is i don't want the field to be an
array, i want each item that is selected to be inserted into a new row.
So if #32 and #59 were selected, #32 would be inserted into a row, then
a new row would be created to insert #59 into... and so on if there
were more articles selected.

Apr 15 '06 #5
NC wrote:
GJ*******@gmail.com wrote:
Maybe i should re-phrase my question. I'm using php with a MySQL
database, and i would like to know how to insert a new record into a
table for each checked item on one form.

OK, let's say you have a form:

<form method="POST" action="insert.php">
<input type="checkbox" name="id[]" value="32">Article #32<br>
<input type="checkbox" name="id[]" value="38">Article #38<br>
<input type="checkbox" name="id[]" value="45">Article #45<br>
<input type="checkbox" name="id[]" value="59">Article #59<br>
<input type="Submit">
</form>

Now, let's assume the user checked articles #32 and #59. These values
will be available to insert.php as fields in $_POST['id'], which in
this case will be an array. So in insert.php you can write:

$query = 'INSERT INTO related_articles (id) VALUES (' .
implode('), (', $_POST['id']) . ')';
$result = mysql_query($query)
or die('Could not execute INSERT query');

That's it, really...

Cheers,
NC


It also violates first normal form for relational databases.

Rather, something like (for simplicity, no error checking shown):

foreach ($_POST['id']) as $id) {
mysql_query("INSERT INTO related_table (article_id, related_id) " .
"VALUES ($origId, $id)");

$origId is the original article id, $_POST['id'] is the id(s) of the related
article(s).

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Apr 16 '06 #6
NC
GJ*******@gmail.com wrote:

Thanks for the reply NC, the trouble is i don't want the field to be an
array,
Then tell us a little more about your form, so we can give you advice
appropriate to your situation... Also, why don't you want the field to
be an array?
i want each item that is selected to be inserted into a new row.


This is exactly what this piece of code does. It creates one record
for each selected item. Obviously, the data in the example are
incomplete (the ID of the article to which selected articles are
related is missing, which is probably what prompted Jerry Stuckle's
comment about it violating first normal form for relational databases),
but it was only meant to show you the sequence of steps... Well, let's
try it again, this time with more data...

Let's say you have a form:

<form method="POST" action="insert.php">
<input type="checkbox" name="id[]" value="32">Article #32<br>
<input type="checkbox" name="id[]" value="38">Article #38<br>
<input type="checkbox" name="id[]" value="45">Article #45<br>
<input type="checkbox" name="id[]" value="59">Article #59<br>
<input type="hidden" name="referer" value="123">
<!-- This is the ID of the "referring" article -->
<input type="Submit">
</form>

Then, let's assume the user checked articles #32 and #59. These
values will be available to insert.php as fields in $_POST['id'], which

in this case will be an array. So in insert.php you can write:

$ref = $_POST['referer'];
$query = 'INSERT INTO related_articles (id, referer) VALUES (' .
implode(", $ref), (", $_POST['id']) . ", $ref)";
$result = mysql_query($query)
or die('Could not execute INSERT query');

Here's the query that is going to be executed:

INSERT INTO related_articles (id, referer)
VALUES (32, 123), (59, 123);

This query will create two records in related_articles, one linking
article #32 to article #123, the other linking article #59 to article
#123.

The advantage over Jerry Stuckle's solution is that all necessary
records are created in one query, although I must admit that this
advantage is probably trivial.

Cheers,
NC

Apr 16 '06 #7
Thanks NC

This is exactly what i wanted, sorry if it took a few posts to get
here. Much appreciated.

Gavin

Apr 16 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
5502
by: Sans Spam | last post by:
Greetings! I have a table that contains all of the function permissions within a given application. These functions are different sections of a site and each has its own permissions (READ, WRITE, UPDATE, DELETE) which are controlled by a web frontend and the table records are manipulated to control the permissions. Example: The Press...
1
8728
by: PT | last post by:
I got a problem. And thats..... First of all, I got these three tables. ------------------- ------------------ ---------------------- tblPerson tblPersonSoftware tblSoftware ------------------- ------------------ ---------------------- PID PName PID* SID* SID SWName --- ...
2
2633
by: Joe | last post by:
Hey, I'm going to give some background on my situation in case anyone can point out a way around my problem altogether... for the problem itself, please skip to the bottom of the post. thanks. I've been having some problems with database performance... Several threads are constantly attempting INSERTs of new records into a large table -...
2
13369
by: george | last post by:
This is like the bug from hell. It is kind of hard to explain, so please bear with me. Background Info: SQL Server 7.0, on an NT box, Active Server pages with Javascript, using ADO objects. I'm inserting simple records into a table. But one insert command is placing 2 or 3 records into the table. The 'extra' records, have the same...
4
2186
by: Mike Hnatt | last post by:
My goal is to get data from an XML file into a couple of tables in an Access database. The XML file is a little complex so I need control over what I do (I can't just read it into a dataset). The way I have it now is ennumerating through my XML file and for each record, running an INSERT INTO SQL statement to put the values in the...
12
2976
by: shank | last post by:
I'm trying to use online samples for submitting multiple records from ASP into a stored procedure. Failing! Through the below form, a user could be submitting many records at a time. I'm not getting any records inserted. For troubleshooting, I cut the form down to 1 textbox and when submitted it populated 5 rows of the same data. So I know I'm...
3
6316
by: Bob Alston | last post by:
I have a routine to copy data to new versions of my app via insert into sql statements. Unfortunately, due to evolution of my app, sometimes the new version has more restrictive editing than an older version that I am updating. Thus I get this message. It tells me only how many records have errors, not which errors or which records. ...
7
15616
by: =?Utf-8?B?TG9zdEluTUQ=?= | last post by:
Hi All :) I'm converting VB6 using True DBGrid Pro 8.0 to VB2005 using DataGridView. True DBGrid has a MultipleLines property that controls whether individual records span multiple lines. Is there an equivalent property for the DataGridView? I have searched, but have not found one. I would like the user to be able to see all the columns...
0
4434
chumlyumly
by: chumlyumly | last post by:
Hello scripters - OS: Mac OSX Language: PHP w/ MySQL database I've created an insert page where a user inputs his info, which then goes to four different tables in a MySQL database. The tables are all linked with the field 'member_id', which is an auto-increment field in the parent table ('members'). I've been able to input multiple...
0
7397
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7563
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7125
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7543
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
4757
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3252
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3239
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
813
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
470
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.