PDA

View Full Version : updating my records


dazzclub
05-08-2008, 03:15 PM
Hi,

I am trying to update a record and im running into trouble.

i double checked my query and found some errors in it and still there is a poblem. here is the query

$query = "UPDATE order_enquiries SET type='$type',title='$title', first_name='$first_name', surname='$surname', position='$position', company_name='$company_name', company_email='$company_email',company_fax='$compa ny_fax', company_telephone_number='$company_telephone_numbe r', address_1='$address_1', address_2='$address_2', address_3='$address_3', town='$town', county='$county', post_code='$post_code', free_sample='$free_sample' WHERE id=$id";
$return = mysql_query($query);
echo "File $id has been updated";


Can anyone see anything wrong?

kind regards
Dazzclub

edbr
05-09-2008, 12:45 AM
in my ignorance it looks ok. when i have problems I try substituting values instead d of $'s to see if it updates,then i maybe find where the error is. Caveman maybe but works for me

davidj
05-09-2008, 05:49 AM
are you getting errors or is it just not updating

dazzclub
05-09-2008, 08:21 AM
Hi, its just not updating.. i havent tride substituting the variables for actual values as of yet but i will try this..

What i did try was to try this way '". $_POST['variable_name'] "', the form but stll no luck.

the variables are coming from the form, and the script which contains the above query is placed in the update.php


i think im gonna strip it down and do it slowly.

davidj
05-09-2008, 05:13 PM
just echo your $query variable and submit the form as normal

your SQL will be printed to the page

dazzclub
05-09-2008, 06:52 PM
Hi there,

I tried that and what it shows is that the id number i use to distinguish each record is not passed through so it doesnt delete it.

echoing the query displays this, agan it looks like the id isn't being passed, so it fails to update the record

UPDATE order_enquiries SET `title`='Mr', `first_name`='test', `surname`='test', `position`='test', `company_name`='test', `company_email`='test@test.co.uk', `company_fax`='0845', `company_telephone_number`='0845', `address_1`='test', `address_2`='test', `address_3`='', `town`='test', `county`='test', `post_code`='test', `free_sample`='test' WHERE id='' LIMIT 1

kind regards
Dazzclub

davidj
05-09-2008, 06:54 PM
check the spelling of your ID variable

dazzclub
05-09-2008, 07:25 PM
hmm i've tried lower case and uppercase for id and they dont seem to work.

davidj
05-10-2008, 09:00 AM
post all your code including the form

ayok
05-12-2008, 12:05 AM
see if it is a mysql error. Try add
mysql_query($query) or die(mysql_error());
If there is a mysql error, it will tell you the error.. If not, then it's not the query..

dazzclub
05-13-2008, 02:14 PM
As mentioned in some posts, the reason for the update not working was the failure of the id variable being passed/retrieved by the update.php script.

so what i did was to echo the id on the action part of the form, like this,
action="update.php?ID=<?php echo $id; ?>"

this now works and my problem is solved thanks for all your help on this guys

pete
05-13-2008, 06:29 PM
As mentioned in some posts, the reason for the update not working was the failure of the id variable being passed/retrieved by the update.php script.

so what i did was to echo the id on the action part of the form, like this,
action="update.php?ID=<?php echo $id; ?>"

this now works and my problem is solved thanks for all your help on this guys

You are going about this the wrong way. The id for the record to update should be passed with a $_POST var so you would do this:


<input type="hidden" name="id" value="<?php echo $row->id;?>" />
Pass the id in the form not with a url. No actions on any record should be processed via $_GET.

Your script also needs tweaking, without seeing the page in its entirety it is hard to tell but the first rule is to validate input before executing a query. In this case the following would have saved you wasted time:


if(isset($_GET['id']) && ctype_digit($_GET['id'])){
// code here
} else {
echo 'invalid id passed';
}
You should also stop using the @ error suppressor on your queries, review http://uk3.php.net/mysql_query. The '@' is for email addresses, if you are using it in your PHP code then in my opinion there is something wrong.

davidj
05-13-2008, 11:28 PM
You should also stop using the @ error suppressor on your queries, review http://uk3.php.net/mysql_query. The '@' is for email addresses, if you are using it in your PHP code then in my opinion there is something wrong

I have always suppressed warnings this way in line with ...

Pitfalls of Transactions with PHP by O'Reilly



// the @ symbol is warning suppression so a warning will not be thrown back
// to the user, be careful not to over-rely on warning suppression, every
// warning suppression should be modified with an if else to catch the
// warning

if ($connect = @mysql_connect("localhost", "specialwebuser", "good4pasSwor9d"))

pete
05-14-2008, 08:00 AM
I have always suppressed warnings this way in line with ...

Pitfalls of Transactions with PHP by O'Reilly

@ is probably OK for mysql_connect (although try/catch/throw would be better) but it is over used inappropriately on queries, vars etc , from a previous thread:


$result = @mysql_query($query);
$rowMake = mysql_fetch_array($result);


is not ...


if($result = mysql_query($query)){
$row = mysql_fetch_array($result);
# show results
} else {
echo 'invalid query';
# or redirect ...
}


the preferred method? Using @ is also two times slower then isset($myvar) for variables, it is generally accepted that using error suppression is perhaps not the best practice.