Page 1 of 2
Re: stuck on multiple image update
Posted: Wed Oct 08, 2014 8:26 am
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.
Re: stuck on multiple image update
Posted: Wed Oct 08, 2014 8:41 am
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.
Re: stuck on multiple image update
Posted: Wed Oct 08, 2014 9:07 am
by Celauran
That's a question of personal opinion, I think. You could always offer both.
Re: stuck on multiple image update
Posted: Wed Oct 08, 2014 9:21 am
by Celauran
Sample
Code: Select all
UPDATE tablename
SET field = 'value',
other_field = 'other_value'
WHERE id = 123
Re: stuck on multiple image update
Posted: Wed Oct 08, 2014 12:04 pm
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.
Re: stuck on multiple image update
Posted: Wed Oct 08, 2014 12:39 pm
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.
Re: stuck on multiple image update
Posted: Wed Oct 08, 2014 1:07 pm
by Celauran
Kill the redirect and capture the results of the query. Check mysql_errors to see where the problem is originating.
Re: stuck on multiple image update
Posted: Wed Oct 08, 2014 1:40 pm
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.
Re: stuck on multiple image update
Posted: Wed Oct 08, 2014 2:06 pm
by Celauran
Where is $id defined?
Re: stuck on multiple image update
Posted: Wed Oct 08, 2014 2:14 pm
by Celauran
Why $_GET? Your form is clearly being submitted as a POST. What's the page URI?
Re: stuck on multiple image update
Posted: Wed Oct 08, 2014 2:33 pm
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.
Re: stuck on multiple image update
Posted: Wed Oct 08, 2014 2:42 pm
by Celauran
Replacing $_GET['id'] with $_POST['id'] should do it, then.
Re: stuck on multiple image update
Posted: Wed Oct 08, 2014 3:04 pm
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.
Re: stuck on multiple image update
Posted: Wed Oct 08, 2014 3:15 pm
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'];
Re: stuck on multiple image update
Posted: Wed Oct 08, 2014 3:22 pm
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.