Inserting MySQL records questions

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
Red Blaze
Forum Commoner
Posts: 40
Joined: Mon Mar 27, 2006 3:45 pm

Inserting MySQL records questions

Post by Red Blaze »

Ok, I'm bad at explaining, so I hope I don't confuse anyone.

I have 3 pages.
One is the gallery of photos
Second page acts as if it's not there, but it's the middle guy. It's a php page that inserts the selected photos into the database.
Third page is like the "Cart" page that shows the selected images for the specified gallery/album.

Now, when a user chooses some photos in the first page via check boxes, they get sent to the 2nd page and the 2nd page right away sends them to the cart page. Here's an example of the cart page:

Image

There's a drop down on the top left side. That button allows you to add more other available sizes. That's where I'm stuck. I don't know how to set it up. Here's a view of the items table in MySQL:

Image

As you can see, the selected files are there. Now, as the user selects an additional size, the php page cart.php will run a MySQL Insert Query. As you see in the image of the database structure, id is blank. That's because that's the original one. When a size is added, I want to insert a new row. This row will have the id col. corresponding with the proper itemid but only within the same albumid. Also, col. itemname will be empty, since I want it to be reffering to the id.

Here's the piece of code used to insert the row ONLY when selected in the drop down.

Code: Select all

//The following block adds an additional size
if($_POST['addsize'])
{
$addedsize = $_POST['addsize'];
mysql_query("INSERT INTO items (id,albumid,cartid,size,paper,quantity,userid,isphoto) VALUES ('$itemid','$albumid','$cartid','$addedsize','$paper','$userid','yes')");
}
Here's the TXT file of the php script. I think it would make the post very long, so I posted it as a txt file. If you need more explaining, I'll do my best to make it alittle more clearer. My apologies if I'm not too clear. Thank you for your time in assisting me.
Last edited by Red Blaze on Fri Mar 31, 2006 11:04 am, edited 1 time in total.
Red Blaze
Forum Commoner
Posts: 40
Joined: Mon Mar 27, 2006 3:45 pm

Post by Red Blaze »

Right, I got it working the way I wanted to, but now I have a similiar problem again. I've posted about it yesterday in this thread, but I ran into the insert query problem after I posted so I didn't reply. I have updated the cart.txt file in the first post.

EDIT:
This is from the other thread that I'm talking about:
Hello again! :D
I'm having a certain issue that I'm not sure how to get around. This is the piece of code I'm working with:

TXT File of the code
I put the entire code of the PHP file due to length.

I'm using dreamweaver due to my lack of PHP Knowledge without it. As you see, I already have a repeat region. Do you see where it says "<!--Start Repeat-->"? That's another repeat row I plan to do, but it won't allow me to. How can I get around it to display the rest of the records?
Red Blaze
Forum Commoner
Posts: 40
Joined: Mon Mar 27, 2006 3:45 pm

Post by Red Blaze »

I'm getting a MySQL error, and I'm not sure why.
Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in /home/prophot/public_html/order/cart.php on line 150
This is the block of code in that area:

Code: Select all

<?php
			  $itemname = $row_callitems['itemname'];
$addedsize = "SELECT * items WHERE userid = $userid AND itemname = $itemname AND original = '0'";
$result = mysql_query($addedsize);
$num = mysql_num_rows($result);
mysql_close();
$i=0;
while ($i < $num) {
$size=mysql_result($result,$i,"size");
$paper=mysql_result($result,$i,"paper");
$quantity=mysql_result($result,$i,"quantity");
			  ?>
Line 150 is "$num = mysql_num_rows($result);" so I assume something is wrong with line 149? Which is the $addedsize variable. I updated the txt file that has the entire code. Help would be greatly appreciated, thank you.

~RB
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

if $itemname is not a number, the SQL will have a syntax error. Add a mysql_error() output to see what MySQL says is the error.
Red Blaze
Forum Commoner
Posts: 40
Joined: Mon Mar 27, 2006 3:45 pm

Post by Red Blaze »

I tried using mysql_error(), but there was no output. I tried quite a few ways to use mysql_error() from PHP.net, but none worked.

Isn't there a way to fix that? I never had a problem like that. This loop is inside a Dreamweaver generated Repeat Region. Could that also affect it?

EDIT: Yes, itemname is actually a filename.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The following is a start.

Code: Select all

$addedsize = 'SELECT * `items` WHERE `userid` = \'' . mysql_real_escape_string($userid) . ' AND `itemname` = \'' . mysql_real_escape_string($itemname) . '\' AND original = \'0\'';
$result = mysql_query($addedsize) or die(mysql_error());
Red Blaze
Forum Commoner
Posts: 40
Joined: Mon Mar 27, 2006 3:45 pm

Post by Red Blaze »

I modified it to this code:

Code: Select all

$addedsize = 'SELECT * items WHERE userid = "'. $userid. '" AND itemname = "'. $itemname .'" AND original = "'. 0 .'"';
$result = mysql_query($addedsize, $prophot) or die(mysql_error());
And it gave me this 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 'items WHERE userid = "25" AND itemname = "7.jpg" AND original = "0"' at line 1
Does the period in the filename have something to do with it? I also tried your suggestion feyd, but it gave me the same 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 'items` WHERE `userid` = '25 AND `itemname` = '7.jpg' AND original = '0'' at line 1
Red Blaze
Forum Commoner
Posts: 40
Joined: Mon Mar 27, 2006 3:45 pm

Post by Red Blaze »

I feel like a duche. I forgot to put "SELECT * FROM items".
Post Reply