People,
I do feel such an idiot as I have managed to get things to work now.
I had not fully apreciated that the example given as 'best practice' related to the user and password details being passed from a login page. Once I changed the POST data in the example (below) to variables containing a test user and password it worked.
$query = sprintf("SELECT * FROM users WHERE user=%s AND password=%s",
quote_smart($_POST['username']),
quote_smart($_POST['password']));
Because of your tips I was able to reason the problem out for myself which I suppose is part of the game.
NogDog - I used your suggestion for printing out the query/result which helped although strangely it echos the reply whether data is returned or not. I understand it should only echo where no data is returned. I know data is being returned because I can see it on the page where it should be.
This is what I have:
$query = sprintf("SELECT * FROM race_admin WHERE user=%s AND password=%s",
quote_smart($user),
quote_smart($password));
$result = mysql_query($query,$conn)or die("Query failed: " . mysql_error() . "<br />\n $query");
if(mysql_num_rows($result) == 0);
{
echo "No data returned by query:<br />\n$query";
}
And this is what it produces in the browser:
No data returned by query:
SELECT * FROM race_admin WHERE user='username' AND password='test'
Web Site Administration <--- rest of the page showing the form and prefilled data etc
Burfo - Sorry. Maybe I didn't explain what the page does clearly enough. This page is in effect a form which a client will use to change dates and other details for a fell race on a website. The form fields will be pre-populated from the database using the 'value' attribute which shows the client what the current data is. There will only ever be one record in the database as all the client will do is update and never be able to add. They can overwrite the data in the form fields - which will update the DB - which will in turn update the relevant part of the website. It all worked okay before I tried to make it more secure by using the 'best practice' code. That's what I meant.
ALL - Is there a better way to make queries secure? Any other advice or articles you would recommend?
Thanks again for your time. Every problem solved / examined helps me to learn and I'm going to crack this lark if it takes me the rest of the year - although I suspect you never actually stop learning!
🙂