Cannot retrieve id[SOLVED]...again

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
User avatar
khushbush
Forum Commoner
Posts: 99
Joined: Tue Mar 11, 2008 11:50 am

Cannot retrieve id[SOLVED]...again

Post by khushbush »

I am trying to retrieve the id for one of my tables in the database, so that I can automatically enter that id number into another table upon submission of a form. The form asks for particular values, such as the bidding price required by a user that is currently logged in at that point in time. Once the values are entered and the form is submitted, the form processes the user's id, the value entered by that user, the property id on which that user bid on and the bid id, which is set on auto increment.

My problem here is that although I seem to retrieve the id for the selected property and am able to show up the property details on the requested browser, I am unable to get that particular property's id to show up in my database when the user enters their bid further down the page on that property.

I am including code. The troublesome code lies between lines 60 and 69.

Code: Select all

 
<?
include "session.php";
mysql_connect(" ", " ", " ") or die(mysql_error());
 
mysql_select_db("property") or die(mysql_error()); 
 
 if (isset($_GET['id'])) { ?>
 <head><?include "menu.php";?></head>
 <br>
 <br>
 <br>
 <?
 
 // Put the value in a separate variable
 $propID = mysql_real_escape_string($_GET['id']); 
 
 
$query = "select propertyArea, propertyPrice, propertyType, propertyBedrooms, propertyDescription, propertyImage1 from property where propertyID = $propID ORDER BY 'propertyID'";
$result = mysql_query($query);
 
 
while ($row = mysql_fetch_row($result))
{
?>
<HTML>
<BODY>
<table ALIGN = "LEFT">
<font face="Arial" color="#0B3A62">
 
<?echo $row[0]; ?> , 
£<? echo $row[1]; ?> , 
<? echo $row[2]; ?> ,
<? echo $row[3]; ?> bedroomed,
<? echo $row[4];?>
<? echo $row[5];?>
   <br>
   <br>
   <tr><tr><td><?echo "<img src =\"showimage.php?id=$row[propertyID]\">";?></td></tr></tr>
   </br></br>
 
<?
}
 
 
if($session->logged_in){
 
?>
 
<br>
<br>
<table align = "LEFT">
<TR>
<TD>
<form action = "bid.php" method = "POST">
<font face="Arial" color="#0B3A62">
Bid value: <input type = "text" name = "bidvalue" value = ""></td></tr></table>
<table align = "LEFT" VALIGN = "bottom">
<tr><td><input type="submit" value="Submit bid"></font></form></td></tr></table></br></br>
<?
$bid = mysql_real_escape_string($_POST['bidvalue']);
 
if(!empty($bid)){
 
$result = mysql_query($query);
$user = mysql_real_escape_string($_SESSION['username']);
$propID = $_GET['id'];
$q = "INSERT INTO bid(username, bidvalue, propertyid) VALUES ('$user', '$bid', '$propID') ";
       return mysql_query($q) or die(mysql_error());
}}
else {
?> <font face="Arial" color="#0B3A62">
<?
echo "<br><br>You must login to bid on this property. Click on the button below to login." ?>
 
</font>
</TABLE>
<br><br>
<table align = "LEFT" VALIGN = "bottom">
<tr><td><form action = "main.php"><input type="submit" value="Login to bid"></form></td></tr></table>
<?
}}?>
 
</BODY>
</HTML>
 
Last edited by khushbush on Sat Apr 19, 2008 2:31 pm, edited 3 times in total.
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Re: Cannot retrieve id

Post by andym01480 »

Code: Select all

$q = "INSERT INTO bid(username, bidvalue, propertyid) VALUES ('$user', '$bid', '$propID') ";
       return mysql_query($q) or die(mysql_error());
Why the "return"?
and echo $q just after setting it to see what the query looks like.

The structure of the whole code is a little hard to work out BTW! Could do with commenting - you''ll come back in 6months and wonder what is going on (I often come back to code I wrote a long time ago that wasn't commented and regret that!)
User avatar
khushbush
Forum Commoner
Posts: 99
Joined: Tue Mar 11, 2008 11:50 am

Re: Cannot retrieve id

Post by khushbush »

I tried echoing...it didn't make any difference.

Oh yes, please don't remind me of the commenting...this is part of a project due in just over a week...so I should actually thank you for reminding me!! :D
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Re: Cannot retrieve id

Post by andym01480 »

The echo wasn't meant to make a difference - it was meant to identify whether the variables were set!

Code: Select all

 
if(!empty($bid)){
 
$result = mysql_query($query);//what is this line for? You already have $result from that $query
$user = mysql_real_escape_string($_SESSION['username']);
$propID = $_GET['id'];
$q = "INSERT INTO bid(username, bidvalue, propertyid) VALUES ('$user', '$bid', '$propID') ";
echo $q; //post it so we can see what the query looked like!
mysql_query($q) or die(mysql_error()); //can't see why the return was there - it may have stopped the query happening! Also post an error if one happens!
} //don't forget the second } in your code- I left it out to be clear on the if statement!
User avatar
khushbush
Forum Commoner
Posts: 99
Joined: Tue Mar 11, 2008 11:50 am

Re: Cannot retrieve id[SOLVED]

Post by khushbush »

OMG!! andym01480...you are a star!!!

Thank you sosososoooooooo much!! You've fixed my bug!!! :D :D :D
Here is the code:

Code: Select all

 
<br>
<br>
<table align = "LEFT">
<TR>
<TD>
<form action = "<?php echo $PHP_SELF;?>" method = "POST">
<font face="Arial" color="#0B3A62">
Bid value: <input type = "text" name = "bidvalue" value = ""></td></tr></table>
<table align = "LEFT" VALIGN = "bottom">
<tr><td><input type="submit" value="Submit bid"></font></form></td></tr></table></br></br>
<?
$bid = mysql_real_escape_string($_POST['bidvalue']);
if(!empty($bid)){
mysql_connect(" ", " ", " ") or die(mysql_error());
 
mysql_select_db("bid") or die(mysql_error()); 
 
$user = mysql_real_escape_string($_SESSION['username']);
$propID = $_GET['id'];
$q = "INSERT INTO bid(username, bidvalue, propertyid) VALUES ('$user', '$bid', '$propID') ";
      
       mysql_query($q) or die(mysql_error());
}}
else {
?> <font face="Arial" color="#0B3A62">
<?
echo "<br><br>You must login to bid on this property. Click on the button below to login." ?>
 
</font>
</TABLE>
<br><br>
<table align = "LEFT" VALIGN = "bottom">
<tr><td><form action = "main.php"><input type="submit" value="Login to bid"></form></td></tr></table>
<?
}}?>
 
</BODY>
</HTML>
 
I think it was the return statement that was causing a bit of trouble...

Nevertheless...you have been a great help! :D
User avatar
khushbush
Forum Commoner
Posts: 99
Joined: Tue Mar 11, 2008 11:50 am

Re: Cannot retrieve id[SOLVED]

Post by khushbush »

The solved bit in the title refers to another part of the system...I need to retrieve a picture id from the database, because my pictures aren't showing up on the page.

Sooooooooo...to summarise my problem...(which has already been summarised in the 'BLOB image won't display' thread)...I can't get a picture from one file to show up in another. This time, the pictures aren't showing up as characters, but as a broken image icon. I thought this might be due to the image header, but in fact, it is not. It is now because I cannot retrieve the required picture id corresponding to that picture's property details.

I am posting code for two files.

This is code from searchprocess.php where the image is embedded:

Code: Select all

 
//retrieve property ID from the search*/
 if (isset($_GET['id'])) { ?>
 <head><?include "menu.php";?></head>
 
 
 
 <?
 
// Put the value in a separate variable and retrieve relevant details using that value
$propID = mysql_real_escape_string($_GET['id']); 
$query = "select propertyArea, propertyPrice, propertyType, propertyBedrooms, propertyDescription from property where propertyID = $propID ORDER BY 'propertyID'";
$result = mysql_query($query);
 
while ($row = mysql_fetch_array($result))
{
?>
 
<HTML>
<BODY>
<table ALIGN = "LEFT">
 
 
<?//display results
?>  <br>
   <br>
   <tr><tr><td><?echo "<img src =\"showimage.php?id=$row[propertyID]\">";?></td></tr></tr>
   </br></br>
   
<font face="Arial" color="#0B3A62">   
Property area: <? echo $row[0]; ?> , 
Starting bid: £<? echo $row[1]; ?> , 
<? echo $row[2]; ?> ,
<? echo $row[3]; ?> bedroomed,
<? echo $row[4];?>
<? echo $row[5];?>
 
 
<?
}
 
And this is code from showimage.php, where the image is being retrieved from the database:

Code: Select all

 
 // Put the value in a separate variable
$propID = mysql_real_escape_string($_GET['id']); 
$query = "select propertyImage1 from property where propertyID = '$propID'";
$result = mysql_query($query) or die(mysql_error());
 
 
while ($row = mysql_fetch_array($result)){
  $imageData1 = $row['propertyImage1'];
 
}
 
header('Content-Type: image/jpeg');
echo $imageData1;
 
I am trying to get $row[propertyID] to retrieve the image in an <img src> tag and it doesn't seem to work. However, when I manually enter a number for, say, property 2, the image shows up. Therefore there is a problem with retrieving the id number, as opposed to getting the image to show.
For further clarification, I must add that the images are saved as BLOBs(I've tried the file pathname route...it's a headache!) alongside the property details in MySQL, therefore property id is the only id I am looking for here. I have not saved the images in a separate table.

I hope somebody can find where I've gone wrong. The solution to this seems to simple...and yet I can't seem to find it. :?

Thanks.
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Re: Cannot retrieve id[SOLVED]

Post by andym01480 »

Code: Select all

  <tr><tr><td><?echo "<img src =\"showimage.php?id={$row[propertyID]}\">";?></td></tr></tr>
You need some curly brackets to make the $row happily show - the curly brackets delineate the variable name when echoing array variables.
User avatar
khushbush
Forum Commoner
Posts: 99
Joined: Tue Mar 11, 2008 11:50 am

Re: Cannot retrieve id[SOLVED]

Post by khushbush »

Unfortunately, that doesn't work...still got a broken image icon. I think it's something to do with $_GET.
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Re: Cannot retrieve id

Post by andym01480 »

Please View source and post what the <img src=... line is please
User avatar
khushbush
Forum Commoner
Posts: 99
Joined: Tue Mar 11, 2008 11:50 am

Re: Cannot retrieve id

Post by khushbush »

<tr><tr><td><img src ="showimage.php?id="></td></tr></tr>
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Re: Cannot retrieve id

Post by andym01480 »

$row[propertyID] is empty because it is not part of the query!

Code: Select all

<tr><tr><td><?echo "<img src =\"showimage.php?id=$propID\">";?></td></tr></tr>
will be more fruitful!

Also you are exposing yourself to SQL injection, by not checking $_GET['id'] is an integer - a naughty user could do also sorts of nasty things to your database very easily
User avatar
khushbush
Forum Commoner
Posts: 99
Joined: Tue Mar 11, 2008 11:50 am

Re: Cannot retrieve id

Post by khushbush »

YAY!! Thank you!! That's the second time you've fixed things up for me :D :D :D

It's actually quite embarrassing that I missed out on something so obvious... :oops:
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Re: Cannot retrieve id

Post by andym01480 »

Missing obvious errors....
Been there, done that, bought the t-shirt and made some glaring ones on the forum too :)
User avatar
khushbush
Forum Commoner
Posts: 99
Joined: Tue Mar 11, 2008 11:50 am

Re: Cannot retrieve id

Post by khushbush »

I'm marking this thread as solved...again.
Post Reply