PDA

View Full Version : MySQL / php help needed


Vitesse
08-08-2005, 09:18 AM
Would someone be able to just check my code here and see where i've gone wrong.


$query=" SELECT * FROM quartertime WHERE user='$user'"; //Query the DB to check if user exists
$result=mysql_query($query);
$num=mysql_numrows($result);
if ($result !=0){ //if record already exists
//amend it
} else {

// Create new record

}
//Rest of script



Basically i just want to see if one or more records exist that match a username held in $user

the if/else is so if the record exists it amends all records with 'user'=$user
otherwise it makes a new record with their details. The code for adding/amending is already done and working just cant get the If/Else right

Many thanks

Boofo
08-08-2005, 10:24 AM
Have you tried:

if ($result !=""){

Logikos
08-08-2005, 10:40 AM
Try this:

$query = mysql_query("
SELECT *
FROM quartertime
WHERE user = '" . $user . "'
");

if (mysql_num_rows($query))
{
// we found something!
}
else
{
//we didn't find anything!
}

Vitesse
08-08-2005, 10:57 AM
Just solved it (i hope)


$query=" SELECT * FROM quartertime WHERE user='$user'"; $result=mysql_query($query);
$num=mysql_numrows($result);
if ($num !=0){
//Amend
} else {
// Create new record
}


The underlying problem was that i had a ; further down in the script preventing it from creating new records <---- DOH


One other thing i need to do though, is check that $quartertime conforms to some criteria

it's a number figure to two decimal places....must be higher than 6, and in the followin format eg. 10.30, 11.99, 9.35 you get the idea, not overly sure how to check that.



Thanks for your assistance guys :)

Boofo
08-08-2005, 11:02 AM
And changing this:

if ($result !=0){

to this probably made a difference. ;)

if ($num !=0){

Vitesse
08-08-2005, 11:11 AM
well, that too LOL, i put $num and $result into the template so i could see what was going on and $result was returning a alhpanumeric tstring where $num was returning a numerical only result, anyone got any idea on checking the conformity of the $quartertime (edited in last post), i just know someone's gonna eventually realise they can add text in there and put something offensive on my site :P

Logikos
08-08-2005, 11:30 AM
Couldn't you even do:

if (!$num)
{

}