stuck on multiple image update

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

User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: stuck on multiple image update

Post by Celauran »

Code: Select all

ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);
It's good that you're doing this, but you don't really want it in every page. Once you go into production and want to turn off display_errors, you'll need to change it in every page. It's a pain. Ideally, set these at the ini level. If that's not possible, set it in a file that gets called on every request so it only needs to be set once.

As for the edit page, it should mostly be the same as your add page, though with the form values pre-populated from what's in the database. Query the record to be edited and use those values in your form. You won't be editing the images themselves, so you'll want to display the existing images, offer an option to delete them, and maybe include additional file upload fields to attach more or new images.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: stuck on multiple image update

Post by Celauran »

Yes, of course it's OK to ask. That's why this site exists. Ask away!
ianhaney wrote:yeah would be good to display the existing images and then what have a delete link on them and then have the file upload input fields to upload new ones, would that work?
There are a few ways you could do it. One is to have delete checkboxes next to each image and have those process on form submit. Another is to have a remove button that fires an AJAX request and deletes the image immediately. Which is the better approach maybe depends on how many images there are. I'd hate to have to remove 20 images one at a time.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: stuck on multiple image update

Post by Celauran »

That's a question of personal opinion, I think. You could always offer both.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: stuck on multiple image update

Post by Celauran »

Sample

Code: Select all

UPDATE tablename
SET field = 'value',
other_field = 'other_value'
WHERE id = 123
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: stuck on multiple image update

Post by Celauran »

Your UPDATE query has no WHERE clause. You haven't told it which record to update, so it updated them all.

Erm.. it also doesn't contain any variables, so all the fields are going to be updated with the literal strings.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: stuck on multiple image update

Post by Celauran »

What I mean is, you're setting up these local variables

Code: Select all

//This gets all the other information from the form
$submittedby = $_POST['submittedby'];
$listingtitle = $_POST['listingtitle'];
$make = $_POST['make'];
$model = $_POST['model'];
$exteriorcolour = $_POST['exteriorcolour'];
$enginesize = $_POST['enginesize'];
$fueltype = $_POST['fueltype'];
$yearregistered = $_POST['yearregistered'];
$transmission = $_POST['transmission'];
$mileage = $_POST['mileage'];
$nodoors = $_POST['nodoors'];
$bodystyle = $_POST['bodystyle'];
$price = $_POST['price'];
but then don't actually reference them in your query

Code: Select all

$sql = "UPDATE privatelistings SET submittedby = 'submittedby', listingtitle = 'listingtitle', make = 'make', model = 'model', exteriorcolour = 'exteriorcolour', enginesize = 'enginesize', fueltype = 'fueltype', yearregistered = 'yearregistered', transmission = 'transmission', mileage = 'mileage', nodoors = 'nodoors', bodystyle = 'bodystyle', price = 'price', photo = 'photo', photo1 = 'photo'";
Instead of submittedby = 'submittedby', you'd want submittedby = '{$submittedby}' and so forth. You really want to be escaping those values, and should use prepared statements and PDO over the deprecated mysql_ functions, but one thing at a time.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: stuck on multiple image update

Post by Celauran »

Kill the redirect and capture the results of the query. Check mysql_errors to see where the problem is originating.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: stuck on multiple image update

Post by Celauran »

I don't have file names, so I'm not sure what that's referring to. Also, $_GET['id'] without context doesn't tell me much.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: stuck on multiple image update

Post by Celauran »

Where is $id defined?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: stuck on multiple image update

Post by Celauran »

Why $_GET? Your form is clearly being submitted as a POST. What's the page URI?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: stuck on multiple image update

Post by Celauran »

You need to either add the ID as a query parameter to your form's action (ie. action="process-edit-private-listings-info.php?id=15") or add it as a (hidden) field into the form itself. I'd opt for the latter.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: stuck on multiple image update

Post by Celauran »

Replacing $_GET['id'] with $_POST['id'] should do it, then.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: stuck on multiple image update

Post by Celauran »

Where are you getting the ID on the page containing the form? Are you sure that should be $_POST? Unless you've arrived there by submitting a form, it probably shouldn't be.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: stuck on multiple image update

Post by Celauran »

Right, and you're using the first one to populate the form value, which you're then retrieving on the form processing page. My question, though, is

Code: Select all

$id = isset($_POST['id']); // Where is this value coming from? Should it be $_POST? Probably not.
$query = "SELECT * FROM privatelistings WHERE id = '$id';";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
mysql_close();

Code: Select all

//This gets all the other information from the form
$id = isset($_POST['id']); // This one needs to be $_POST because that's your form's method.
$submittedby = $_POST['submittedby'];
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: stuck on multiple image update

Post by Celauran »

in the url ive got id=41
Which means you'll need to check $_GET, not $_POST. You still need to check $_POST when processing the form because you set the form's method to post.

Also, what if $id ends up being empty? You should handle that situation before running your query.
Post Reply