View unanswered posts | View active topics
It is currently Mon May 12, 2025 9:38 pm
|
Page 1 of 1
|
[ 9 posts ] |
|
Author |
Message |
tombolt
Spends far too much time on here
Joined: Fri Apr 24, 2009 8:38 am Posts: 2967 Location: Dorchester, Dorset
|
I'm trying to update some records in my database. I'm sure I've made some sort of error with my syntax in the '$sql=' line. Does anybody know where I'm going wrong? I've not quite got the hang of when to use single quotes, double quotes or brackets!
|
Mon May 10, 2010 3:32 pm |
|
 |
Nick
Spends far too much time on here
Joined: Thu Apr 23, 2009 11:36 pm Posts: 3527 Location: Portsmouth
|
In the $sql line you have a lot of " quote marks. You need one pair, to signal the start and end of the string to be stored within the $sql variable. Any others need to be escaped with a back-slash, so PHP knows that you don't mean it's the end of the line. So in your code you will need to escape as such: Also - it seems you are pulling the data straight from the user input. This can be dangerous - are you checking it first? I don't tend to do it this way - I've got a habit of pulling the data out of the post array, and processing with addslashes() etc as it goes into another. Infact, I'm not even sure the above code will work. It might add the literal string $_POST["description"] etc rather than the data held in the $_POST[] array. If you find that is the case, you'll need to pull the data out of the $_POST[] array and into a new one as mentioned. It's been a long time since I've done any PHP, so I can't remember exactly how it's likely to behave in this instance.
_________________
|
Mon May 10, 2010 3:40 pm |
|
 |
Nick
Spends far too much time on here
Joined: Thu Apr 23, 2009 11:36 pm Posts: 3527 Location: Portsmouth
|
P.S. Take a look at these functions for preparing your data for the DB query: mysql_real_escape_stringaddslashes
_________________
|
Mon May 10, 2010 3:50 pm |
|
 |
tombolt
Spends far too much time on here
Joined: Fri Apr 24, 2009 8:38 am Posts: 2967 Location: Dorchester, Dorset
|
Cheers Nick, I shall absorb and digest. I'm going straight from user input as it's an internal database and doesn't need checking.
|
Mon May 10, 2010 3:58 pm |
|
 |
Nick
Spends far too much time on here
Joined: Thu Apr 23, 2009 11:36 pm Posts: 3527 Location: Portsmouth
|
Cool. In that case, there is another option: It's horrendously ugly, but will get the job done. 
_________________
|
Mon May 10, 2010 4:03 pm |
|
 |
tombolt
Spends far too much time on here
Joined: Fri Apr 24, 2009 8:38 am Posts: 2967 Location: Dorchester, Dorset
|
I'd love to be able to have a look at what you suggested, but I decided to look at something else and got stuck again! I've got a drop down list that populates based on the contents of a column. I want the drop down to be pre-selected with the current selection. I've looked all over the web and the code seems to be correct, but it sets the option to "selected" on every single option, not just the one that matches the current selection. It seems that it thinks the value of $line[Status] is always the same as the $_POST[status] which it's definitely not…
|
Mon May 10, 2010 9:52 pm |
|
 |
Nick
Spends far too much time on here
Joined: Thu Apr 23, 2009 11:36 pm Posts: 3527 Location: Portsmouth
|
There appears to be a couple of syntax errors. First, there is a semi-colon after the if statement, which shouldn't be there. I don't know what effect it's likely to have, but it's likely to make PHP think that is the end of the if statement, IE there is no conditional statement to follow. Also, the echo statement has some errors. Try this code: What options are you running in your .ini file? I'm guessing that you have not set errors to be printed - it would make things much easier if you were to enable these. That way, if PHP runs into a problem, like the syntax errors in the code above, it can print out a helpful message saying what error it had and even what line of code caused it. If you are unable to change the php.ini configuration file, you can change the configuration at run time using the ini_set() method as detailed [url]here[/url]. That page includes some code you can include in your scripts, so that if there is an error it will give you some output hinting at the problem.
_________________
|
Mon May 10, 2010 11:22 pm |
|
 |
tombolt
Spends far too much time on here
Joined: Fri Apr 24, 2009 8:38 am Posts: 2967 Location: Dorchester, Dorset
|
Cheers Nick, you're bang on, it was the semi colon. That must have been the only thing I overlooked and I was tearing my hair out for hours! Now to go back to the data update…
|
Tue May 11, 2010 8:29 am |
|
 |
tombolt
Spends far too much time on here
Joined: Fri Apr 24, 2009 8:38 am Posts: 2967 Location: Dorchester, Dorset
|
You were also right about the quotes, that was the problem on the update part. I thought I had tried absolutely every combination of everything, but obviously not! Thanks very much for your help.
|
Tue May 11, 2010 9:00 am |
|
|
|
Page 1 of 1
|
[ 9 posts ] |
|
Who is online |
Users browsing this forum: No registered users and 6 guests |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum
|
|