Update Query
5 posts by 2 authors in: Forums > CMS Builder
Last Post: April 15, 2009 (RSS)
By rjbathgate - April 14, 2009 - edited: April 14, 2009
I'm trying to get a form field to update records in a table, and here's where I'm at:
(Note this code is condensed to only provide what's needed for this post, to make it clearer!)
I am trying to update the table DIRECTORY_LISTING.
The FORM:
<form method="post" action="?">
<input type="hidden" name="submitProfile" value="1">
<input type="text" name="company">
</form>
Then that's submitted and we have this:
if (@$_POST['submitProfile']) {
$currentDate = date("Y-m-d H:i:s", getAdjustedLocalTime()); # format: YYYY-MM-DD HH:MM:SS (24hour format)
$updateQuery = "UPDATE `{$GLOBALS['TABLE_PREFIX']}directory_listing` SET
company = '".mysql_real_escape_string( $_POST['company'] )."',
updatedDate = '".mysql_real_escape_string( $currentDate )."',
updatedByUserNum = '" .mysql_real_escape_string($currentloggednum). "'
WHERE num = '" .mysql_real_escape_string($currentprofilenum). "'";
mysql_query($updateQuery) or die("MySQL Error Updating User:<br/>\n". htmlspecialchars(mysql_error()) . "\n");
NOTE, the variables in there are defined as:
$currentloggednum -- num of account record - to record the USER who updated the record
$currentprofilenum are defined - num of record in directory_listing to ensure the correct record is updated.
Now, when I submit the form to update the record, it is almost working..!
The LAST UPDATED (updateDate) correctly updates - i.e. i submit the form, and then I look in the database, and the LAST UPDATE time has been updated.
However, the COMPANY field doesn't update. Whatever entry I put in the field, it doesn't update the database record.
Which is why I'm confused - the function is obviously working ok as it's updating the udpated date.
I can only think it's something wrong with this line:
company = '".mysql_real_escape_string( $_POST['company'] )."',
But it looks ok to me.
I've also got validation on there, and when submitted, $_POST['company'] is going through as NOT BLANK, so something is being sent with $_POST['company'].
Any thoughts, or ideas or pointers in the right direction would be appreciated.
Cheers
Rob
EXTRA:
If I define the $company variable as follows, it works ok, updating the database:
$company = "hello";
$updateQuery = "UPDATE `{$GLOBALS['TABLE_PREFIX']}directory_listing` SET
company = '".mysql_real_escape_string( $company)."',
So that says to me something is wrong with that line when it's trying to pull COMPANY from the form.
I have also just tried defining $company before the UPDATE function like this:
$company = Trim(stripslashes($_POST['company']));
and that DOESN'T WORK.
Humz
Re: [rjbathgate] Update Query
By Dave - April 14, 2009
What do these two debug lines output?
<xmp><?php print_r($_POST); ?></xmp>
and
<xmp><?php print_r($updateQuery); ?></xmp>
Once we can see what is being submitted by the form and the query that is being sent to MySQL it should make it easier to isolate and resolve the issue.
Also, you can write this:
updatedDate = '".mysql_real_escape_string( $currentDate )."',
as this:
updatedDate = NOW(),
Since we now update the timezone for mysql as well.
Hope that helps, let me know what you find.
interactivetools.com
Re: [Dave] Update Query
By rjbathgate - April 14, 2009
Thanks for the quick reply.
The output for the _POST print is working fine, but looking at the updateQuery output, we see:
UPDATE `cms_directory_listing` SET company = 'Development Company Test', updatedDate = '2009-04-15 04:51:15', updatedByUserNum = '' WHERE num = ''
You'll see the last two fields (updated by and where num) are both blank. So they're not getting populated...
This is the code for them:
updatedByUserNum = '" .mysql_real_escape_string($currentloggednum). "'
WHERE num = '" .mysql_real_escape_string($currentprofilenum). "'";
These two variables are correctly defined before they are called...
So have I got this string written incorrectly:
updatedByUserNum = '" .mysql_real_escape_string($currentloggednum). "'
WHERE num = '" .mysql_real_escape_string($currentprofilenum). "'";
??
Just tried:
updatedByUserNum = '"$currentloggednum"'
WHERE num = '"$currentprofilenum"'";
but that doens't work...!
Cheers
Re: [rjbathgate] Update Query
By Dave - April 15, 2009 - edited: April 15, 2009
Try this debug code on the line before: $updateQuery = ...
print "<xmp>";
print "\$currentloggednum = '$currentloggednum'\n";
print "\$currentprofilenum = '$currentprofilenum'\n";
print "</xmp>";
Let me know what that says.
And feel free to attach the whole script if you can.
interactivetools.com
Re: [Dave] Update Query
By rjbathgate - April 15, 2009
The variables were defined correctly on the pre-form-submit, but once the form was submit (back onto itself) the variables were lost, and coming back as blank.
So but putting the variables into the form as hidden fields and then redefining them from that, it's all working now.
Thanks heaps Dave - all your debug code is a great help
Rob