Different 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
faile486
Forum Newbie
Posts: 15
Joined: Wed Nov 18, 2009 8:12 pm

Different Questions

Post by faile486 »

I gave up on the refresh thing - it's not really necessary. However, the next part of this project is.

Here's what's going on:
I have four files, index.php, form.php, submit.php and template.php

I'm using PHP and a mySQL database to store information on individual apartment listings (as I'm currently searching for a new place to live).
Index.php lists the name, location and a link of all listings currently in the database. It also contains a link to add new listings. Clicking a listing link takes you to detailed listing information displayed by template.php.

The link for new listings takes you to form.php which collects all the information on the apartment. Submit.php is the form processor.

So far everything is working just fine EXCEPT the image uploading part. I want to be able to:
1) Get an int value for the number of pictures uploaded and store that in the database.
2) Upload an image to my server.
3) Rename the image to $aptID_$picnum. The picture number can be obtained via the form. I'm not sure how to get the apartment ID since it's an auto-incremented mySQL field. This is where the major problem is.
4) I'd like all the images for each listing to be stored in their own directory organized by $aptID.
5) I need a way to display all the images in a given directory. I think I can figure this out once the rest of it is taken care of, however.

Thank you for any help in advance!
Last edited by faile486 on Thu Nov 19, 2009 4:24 pm, edited 1 time in total.
faile486
Forum Newbie
Posts: 15
Joined: Wed Nov 18, 2009 8:12 pm

Re: Different Questions

Post by faile486 »

bump
User avatar
iankent
Forum Contributor
Posts: 333
Joined: Mon Nov 16, 2009 4:23 pm
Location: Wales, United Kingdom

Re: Different Questions

Post by iankent »

faile486 wrote: 1) Get an int value for the number of pictures uploaded and store that in the database.
I wouldn't bother. If you create a table to keep a record of each upload, you can query that to find out how many images there are total and per apartment etc.
faile486 wrote: 2) Upload an image to my server.
have a look here for a tutorial on uploading files using PHP: http://www.tizag.com/phpT/fileupload.php
You can also find a lot more info by googling 'php file uploads'
Once the file has been uploaded, add a record of its location and apartment ID etc to a table
faile486 wrote: 3) Rename the image to $aptID_$picnum. The picture number can be obtained via the form. I'm not sure how to get the apartment ID since it's an auto-incremented mySQL field. This is where the major problem is.
Use something like mysql_affected_rows() to find out if the table insert works, and if it does, use mysql_insert_id() to get the auto-incremended field value.
faile486 wrote: 4) I'd like all the images for each listing to be stored in their own directory organized by $aptID.
you can use php's mkdir() function to create a folder for each apartment. you can use is_dir to check if the folder exists when uploading the file, and if it doesn't, use mkdir to create it.
faile486 wrote: 5) I need a way to display all the images in a given directory. I think I can figure this out once the rest of it is taken care of, however.
if you store info about them in a table in your database you can query that to find out which images apply to which apartments.

hth :)
faile486
Forum Newbie
Posts: 15
Joined: Wed Nov 18, 2009 8:12 pm

Re: Different Questions

Post by faile486 »

So, just to make sure I'm understanding this correctly, are you suggesting I make a separate table for the images?

Right now I only have one table: apartments

This contains the columns: ID, address, name, contact name, price etc...

You're saying I should also make a table 'pictures' and include the picture location(primary key?), and aptID associated with it?

Then I could use a mySQL query to display all images with an individual apartment ID.

I think I see how that would work, let me try it out!
User avatar
iankent
Forum Contributor
Posts: 333
Joined: Mon Nov 16, 2009 4:23 pm
Location: Wales, United Kingdom

Re: Different Questions

Post by iankent »

exactly :)

personally I'd use an auto-incremented integer for the primary key of the pictures table but it doesn't really make a lot of difference.

the only real requirement is that the pictures table includes the apartment ID. Then you could use a query like this:
select * from tbl_pictures where apt_id=1;
to get a list of images for that apartment

edit:
and you could use this to find out how many images you have total:
select count(picture_id) as total from tbl_pictures;
and for how many an apartment has just add where apt_id=whatever at the end

hth
faile486
Forum Newbie
Posts: 15
Joined: Wed Nov 18, 2009 8:12 pm

Re: Different Questions

Post by faile486 »

Here's some code I'm trying:

$last_id = mysql_insert_id ();

if ($last_id) {
echo "The last query generated an AUTO_INCREMENT value of $last_id";
} else {
echo "The last query did not generate an AUTO_INCREMENT value";
}

Directly before this code is the insert statement for my apartments table. I know it's working, and I know it's generating new id's, because I have those displayed on a separate page. However, $last_id echos 0 and I get the error message from the if statement. I thought I had some part of the code wrong, so I scrapped what I had and pulled the above code from a tutorial.

So now I'm really confused o.O
faile486
Forum Newbie
Posts: 15
Joined: Wed Nov 18, 2009 8:12 pm

Re: Different Questions

Post by faile486 »

Ah, looks like I was calling it in the wrong location.
faile486
Forum Newbie
Posts: 15
Joined: Wed Nov 18, 2009 8:12 pm

Re: Different Questions

Post by faile486 »

Ok, almost got everything working now - just need to add the data to the database. Getting a mySQL error when I try to add it.
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: Different Questions

Post by Weiry »

faile486 wrote:Getting a mySQL error when I try to add it.
And what error might that be?
faile486
Forum Newbie
Posts: 15
Joined: Wed Nov 18, 2009 8:12 pm

Re: Different Questions

Post by faile486 »

This is the error:
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/kendramo/public_html/Apartments/img_upload.php on line 125
Error:

Here's the code:

$sql = "INSERT INTO images (aptID, location)
VALUES
(
'$last_id',
'images/$newname'
)";

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 image added. <br />";
mysql_close($con);
User avatar
iankent
Forum Contributor
Posts: 333
Joined: Mon Nov 16, 2009 4:23 pm
Location: Wales, United Kingdom

Re: Different Questions

Post by iankent »

You haven't made a successful connection to the database. I.e., $con doesn't contain a database connection from mysql_connect(). If you're calling mysql_connect(), try adding a 'or die(mysql_error())' bit to the end to see if there's any connection problem
faile486
Forum Newbie
Posts: 15
Joined: Wed Nov 18, 2009 8:12 pm

Re: Different Questions

Post by faile486 »

Thank you SO MUCH for your help!! It's all working fine now!

This will make finding a new place so much simpler.
faile486
Forum Newbie
Posts: 15
Joined: Wed Nov 18, 2009 8:12 pm

Re: Different Questions

Post by faile486 »

Another question:

I've set up a modify.php page. It displays the same form, populated with info pulled from the database. Text, textarea and select inputs work fine, but my checkboxes won't save! I don't understand what's wrong with them = /

Here's the code from the original form:
<input type="checkbox" name="counter" id="counter" value="1" /> <label for="counter">Granite Counters</label> <br />

Here's code from the modify.php form:
Values for the variables (checked, example): 1, $dw, Dishwasher
Values for the variable (unchecked, example): , $dw, Dishwasher
I tried printing out an unchecked box, and it didn't do anything...
function checked($value, $name, $long_name)
{
if ($value == 1)
{
echo "<input type='checkbox' name='" . $name . "' id='" . $name . "' value='1' checked/> <label for='" . $name . "'>" . $long_name . "</label> <br />";
}
else
{
echo "<input type='checkbox' name='" . $name . "' id='" . $name . "' value='1'/> <label for='" . $name . "'>" . $long_name . "</label> <br />";
}
}

The current process for template.php file has an if statement to read whether or not the checkbox value==1. If it does, it displays in bold, otherwise it's just a normal font.

Any ideas? I'm thinking having two checkboxes might work better, but I was hoping to cut down on space by only having one.
faile486
Forum Newbie
Posts: 15
Joined: Wed Nov 18, 2009 8:12 pm

Re: Different Questions

Post by faile486 »

Well, I found A solution using two checkboxes, but it makes the code a lot more bulky.
Post Reply