spliting a insert query

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

spliting a insert query

Post by nite4000 »

Hey all

I am not sure if this is possible however i think it is.

I have a form and there is a qty box for them to say how many they want and there is a drop box with a cost lets say they choose $1.. now lets say they enter 4 so thats $4 they spend

now when they press the button it does insert adn has $4 in teh record but what I want to do is take the qty of 4 and insert 4 different records into the table and on each record it would have $1

I hope someone can help me with this.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: spliting a insert query

Post by califdon »

Sounds like you just need to have a for() loop that runs n times, n being the quantity from the Form data. Within the loop, you always insert with a value of 1 (maybe you don't even need a quantity field in your table, if you're going to always do this).
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

Re: spliting a insert query

Post by nite4000 »

do you have an example of what the for loop would look like i have never been great with for loops i try to avoid them sometimes lol
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: spliting a insert query

Post by califdon »

nite4000 wrote:do you have an example of what the for loop would look like i have never been great with for loops i try to avoid them sometimes lol
http://www.tizag.com/phpT/forloop.php
You won't be able to write much PHP code if you avoid loops.
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

Re: spliting a insert query

Post by nite4000 »

yeah i am looking at the loop i see in the example what the counter variable would be that would be the box. I dont need the qty field in the table i just need to have it create the n number of records according to the box

thanks let me know if you have any examples with for look with a query

*Update*

I did find a example somewhat i inputed my info but its not workign right plus i dont know where to insert the box name of the qty box but here is what i have

Code: Select all

	 	/* start by putting all your values into an array */
	$my_values_array = array ("$postion","{$_SESSION['admin_user']}","$qty","$total");
	
	/* count the elements */
	$c = count($my_values_array);
	/* set the begining of the query string */
	
	$query = "INSERT INTO line(id,pid,username,qty,spent_amt) VALUES";
	/* loop thru the elements, adding them to the query string */
	for($i=0; $i<$c;$i++) {	
	$query = $query . "(" . $my_values_array[$i] . ")";
	}
	/* clean up the query string  - removing the lsat comma and space */
	$query = trim($query);
	$query = substr($query, 0, -1);
any help would be nice
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: spliting a insert query

Post by califdon »

As I understood your original question, you are starting with a form the user fills in that will be an order for some quantity of something, so your form fields will be something like "item", maybe "price", and "qty". And you will have the user's ID in a session variable. And instead of recording the order as a single record in the database, you want to record as many records with the quantity "1" as the quantity ordered. Right? That doesn't make any sense to me, but if that is what you want, all you need to do is read those values from the $_POST array (assuming that you use the POST method in your form) and issue the same MySQL INSERT query as many times as the "qty" value. You don't need to add anything to the query string or trim or any such operations. It could be as simple as:

Code: Select all

$ID = $_SESSION['admin_user'];
$item = $_POST['item'];
$price = $_POST['price'];
$qty = $_POST['qty'];

$sql = "INSERT INTO line(pid, qty, spent_amt) VALUES ($ID, 1, $price)";

for ($i=0; $i<$qty; $i++) {
    mysql_query($sql) or die(mysql_error());
}
Ordinarily you would obtain the price of the item from your database, not from the user's input, because otherwise, the user can determine the price, but again, if you have some reason for doing it that way, the above should do what you described.
Post Reply