473,372 Members | 1,037 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

Inserting multiple rows of data in table

66
Hi all,

The system im working on currently allows the user to select a number of flowers.. click submit and whatever they clicked is passed onto the next page, i now want them to click order and each flower is inserted into the table.

username - flowerid - qty - total
user1 ------------ 2 ------- 1----- 1.99
user1 ------------ 3 ------- 5----- 2.55
user1 ------------ 6 ------- 1----- 0.50

i havent set up the session yet to display the username etc... just trying to insrt the flowers selected into multiple rows... heres the code ive got so far.!

line 7 shows where i would but the sql statement that allows me to insert the records.

[PHP]<?php
include "constants/flowertimedbcnx.php";

echo 'You have chosen the following flowerids:<br>';
foreach($_GET['flowerid'] as $flower) {
$res = mysql_query ("SELECT * FROM flowers WHERE flowerid = $flower");
//$order = mysql_query ("INSERT INTO bqt_order VALUES ....... ?
while ($row = mysql_fetch_array($res)) {
echo "$flower {$row['flowername']}";
echo " {$row['flowerdesc']} <br>";
}
}
?> [/PHP]



many thanks in advance to anyone who can help me with this!
Feb 13 '08 #1
16 2503
ronverdonk
4,258 Expert 4TB
When you are sure that the flowerid is unique, you can leave the WHILE loop out. When you want each type of flower in the order to be stored in a separate row, you'll have to do the INSERT within the foreach loop, as follows:
[php]
foreach($_GET['flowerid'] as $flower) {
$res = mysql_query ("SELECT * FROM flowers WHERE flowerid = $flower");
// check here if any rows were selected
$row = mysql_fetch_array($res))
echo "$flower {$row['flowername']} : {$row['flowerdesc']} <br>";
if (!mysql_query ("INSERT INTO bqt_order (flowerid, quantity) VALUES (".$row['flowerid'].",".$row['quantity'])
die("INSERT error: ".mysql_error());
}
[/php]
Ronald
Feb 13 '08 #2
jasone
66
Hi Ronald,

looking at the script it looks very logical.. well written!!

for some reason though im getting the 'http 500 internal server error' usualy get this when things such as ';' or '}' are missing ive checked through but cant see any obvious causes of this... all the names match up in the tables etc but it just wont work! would it be possible to put the insert on a link press? not sure if this would be easier or not..

kind regards
Jason
Feb 14 '08 #3
jasone
66
hey, ok so ive tested it in firefox... and it gives a better message, its saying the following: '
Parse error: syntax error, unexpected T_EXIT in C:\web\Apache2\htdocs\university\FinalYearProject\ testing folder\choice.php on line 18'

:-s
Feb 14 '08 #4
ronverdonk
4,258 Expert 4TB
So all you have to do is look at line 18 and see where in that line, or before that line, you missed something like a closing bracket, a semicolumn, a quote, etc.

Ronald
Feb 14 '08 #5
jasone
66
Hi ron,

tried a number of things, but really cant see what missing, here the script with the error:

Parse error: syntax error, unexpected T_EXIT in C:\web\Apache2\htdocs\university\FinalYearProject\ testing folder\choice.php on line 19

[PHP]<?php

include "constants/flowertimedbcnx.php";

foreach($_GET['flowerid'] as $flower) {
$res = mysql_query ("SELECT * FROM flowers WHERE flowerid = $flower");
// check here if any rows were selected
$row = mysql_fetch_array($res);
echo "$flower {$row['flowername']} : {$row['flowerdesc']} <br>";
if (!mysql_query ("INSERT INTO orders ('flowerid', 'flowername') VALUES (".$row['flowerid'].",".$row['flowername'])
die("INSERT error: ".mysql_error());
}
?> [/PHP]

line 10 aboive is line 19!

Jason
Feb 14 '08 #6
ronverdonk
4,258 Expert 4TB
No line 19 in sight!

Ronald
Feb 14 '08 #7
jasone
66
ok problem solved.. was missing a closing bracket...

just getting this error now, but i will try and solve this... :-)

INSERT error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''flowerid', 'flowername') VALUES (3,wewrwe' at line 1
Feb 14 '08 #8
ronverdonk
4,258 Expert 4TB
This is the error: [php]VALUES (".$row['flowerid'].",".$row['flowername'])[/php]
Typo from my part. VALUES substring starts with a double quote and a dot.

Ronald
Feb 14 '08 #9
jasone
66
hi, tried taking that out.. still getting this error:

INSERT error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '3,wewrwe' at line 1

for this line of code:

[PHP] if (!mysql_query ("INSERT INTO orders (flowerid, flowername) VALUES ".$row['flowerid'].",".$row['flowername']))[/PHP]
Feb 14 '08 #10
ronverdonk
4,258 Expert 4TB
I don't know what it is with me. I gave you the wrong answer before. What you must do is enclose the flowername within quotes because it is a character-type field. I assigned the 2 columns to variables so it is easier to see:
[php] $fi=$row['flowerid'];
$fn=$row['flowername'];
if (!mysql_query ("INSERT INTO orders (flowerid, flowername) VALUES($fi,'$fn'"))[/php]

Ronald
Feb 14 '08 #11
jasone
66
lol, nothing wrong with you, you are a legend in my eyes!

though for some reason now it doesnt like the curly brackt on line 21...

if i show you the entire code and the error, any chance you can see why?

error :

Parse error: syntax error, unexpected '}' in C:\web\Apache2\htdocs\university\FinalYearProject\ testing folder\choice.php on line 21

code:

[PHP]<?php

include "constants/flowertimedbcnx.php";

foreach($_GET['flowerid'] as $flower) {
$res = mysql_query ("SELECT * FROM flowers WHERE flowerid = $flower");
// check here if any rows were selected
$row = mysql_fetch_array($res);
echo "$flower {$row['flowername']} : {$row['flowerdesc']} <br>";
$fi=$row['flowerid'];
$fn=$row['flowername'];
if (!mysql_query ("INSERT INTO orders (flowerid, flowername) VALUES ('$fi','$fn')")
}
?> [/PHP]
Feb 14 '08 #12
ronverdonk
4,258 Expert 4TB
Show all code of choice.php because I can't see what lines you are talking about.

Ronald
Feb 14 '08 #13
jasone
66
[PHP]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php

include "constants/flowertimedbcnx.php";

foreach($_GET['flowerid'] as $flower) {
$res = mysql_query ("SELECT * FROM flowers WHERE flowerid = $flower");
// check here if any rows were selected
$row = mysql_fetch_array($res);
echo "$flower {$row['flowername']} : {$row['flowerdesc']} <br>";
$fi=$row['flowerid'];
$fn=$row['flowername'];
if (!mysql_query ("INSERT INTO orders (flowerid, flowername) VALUES ('$fi','$fn')");
}
?>



</body>
</html>
[/PHP]

here we go!

Jason
Feb 14 '08 #14
ronverdonk
4,258 Expert 4TB
You cut off the original 'die(..)' statement after the insert. The code should be
[php]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
include "constants/flowertimedbcnx.php";

foreach($_GET['flowerid'] as $flower) {
$res = mysql_query ("SELECT * FROM flowers WHERE flowerid = $flower");
// check here if any rows were selected
$row = mysql_fetch_array($res);
echo "$flower {$row['flowername']} : {$row['flowerdesc']} <br>";
$fi=$row['flowerid'];
$fn=$row['flowername'];
if (!mysql_query ("INSERT INTO orders (flowerid, flowername) VALUES ('$fi','$fn')"))
die("INSERT error: ".mysql_error());
}
?>
</body>
</html>[/php]
Ronald
Feb 14 '08 #15
jasone
66
I take my hat off to you once again ronald!

if you need any help in the future... you know where i am ;-)

hehe! maybe one day!
Feb 14 '08 #16
ronverdonk
4,258 Expert 4TB
I take my hat off to you once again ronald!

if you need any help in the future... you know where i am ;-)

hehe! maybe one day!
Thanks for your kind words. See you again.(see my PM)

Ronald
Feb 14 '08 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Raj Kotaru | last post by:
Hi, In sqlplus, I can insert a single row using: insert into employee (name, salary, hiredate) values ('xyz', '86378', sysdate); Is there a modification of the insert command that will...
3
by: JB | last post by:
To anyone that is able to help.... What I am trying to do is this. I have two tables (Orders, and OrderDetails), and my question is on the order details. I would like to set up a stored...
1
by: Scott Chapman | last post by:
I am working with Python (psycopg). I have HTML with embedded Python that I'm inserting into a database and it could contain any character. Single quotes, at least, must be escaped (to two...
1
by: Srinadh | last post by:
Hi all, We have files with about 20 to 30 fields per row. We are trying to update such files with about 60 rows as contiguous data in a CLOB field. It passes through. But when we try...
3
by: Joachim Klassen | last post by:
Hi all, first apologies if this question looks the same as another one I recently posted - its a different thing but for the same szenario:-). We are having performance problems when...
2
by: a | last post by:
NEW Post Here's my best guess at how to insert this dataset.... the code runs, but no new records are added to the sql table. I've read and split a delimited text file into a dataset. It...
5
by: Arsen V. | last post by:
Hello, What is the optimal way to insert multiple rows (around 1000) from a web application into a table? The user enters multiple lines into a text box (up to 10,000). The ASP.NET...
11
by: c676228 | last post by:
Hi everyone, I am just wodering in asp program, if there is anybody writing store procedure for inserting data into database since there are so many parameters need to be passed into store...
3
by: rcoco | last post by:
Hi, I want to share this problem. I have a datagrid that will help me Insert data into sql database. So I made a button On my form so that when I press the button a new row on datagrid should be...
482
by: bonneylake | last post by:
Hey Everyone, Well i am not sure if this is more of a coldfusion problem or a javscript problem. So if i asked my question in the wrong section let me know an all move it to the correct place. ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.