JohnM1983 Posted September 4, 2008 Share Posted September 4, 2008 PHP MYSQL (UPDATE) - Code not working... Nice to meet you all. 1st post, hope you don't mind its a question... I have a problem... I cant get this bit of my code to update the database if a user is already in it... if ($dbmain) { $query="SELECT pilotname FROM Pilot_Data WHERE pilotname = '$pilotname'"; $result = mysql_query($query); if ($result == $pilotname) { $updaterecord = true; } else { $updaterecord = false; } if ($updaterecord) { $query = "UPDATE Pilot_Data SET pilotdate = '$pilotdate', pilottime = '$pilottime', pilotcorp = '$pilotcorp', pilotcorpid = '$pilotcorpid', pilotalli = '$pilotalli', pilotalliid = '$pilotalliid', pilotcorprole = '$pilotcorprole', pilotregion = '$pilotregion', pilotconst = '$pilotconst', pilotsystem = '$pilotsystem', pilotnear = '$pilotnear', pilotstation = '$pilotstation' WHERE pilotname = '$pilotname'"; } else { $query = "INSERT INTO Pilot_Data (pilotname, pilotdate, pilottime, pilotnameid, pilotcorp, pilotcorpid, pilotalli, pilotalliid, pilotcorprole, pilotregion, pilotconst, pilotsystem, pilotnear, pilotstation ) Values ('$pilotname', '$pilotdate', '$pilottime', '$pilotnameid', '$pilotcorp', '$pilotcorpid', '$pilotalli', '$pilotalliid', '$pilotcorprole', '$pilotregion', '$pilotconst', '$pilotsystem', '$pilotnear', '$pilotstation')"; } mysql_query($query); mysql_close($dbmain); } Any help? Thanks. JohnM EDIT Code works fine if its a new user ($updaterecord = false;) Quote Link to comment Share on other sites More sharing options...
Mchl Posted September 4, 2008 Share Posted September 4, 2008 Do you get any errors? Also you could try INSERT ... ON DUPLICATE KEY UPDATE syntax. Quote Link to comment Share on other sites More sharing options...
JohnM1983 Posted September 4, 2008 Author Share Posted September 4, 2008 No errors as far as I know. ~JohnM Quote Link to comment Share on other sites More sharing options...
Mchl Posted September 4, 2008 Share Posted September 4, 2008 Change mysql_query($query); to if(!mysql_query($query)) echo mysql_error(); This will display any mysql errors. Quote Link to comment Share on other sites More sharing options...
tmbrown Posted September 4, 2008 Share Posted September 4, 2008 Try this <?php if ($dbmain) { $query="SELECT pilotname FROM Pilot_Data WHERE pilotname = '".$pilotname."'"; $result = mysql_query($query); $count = mysql_num_rows($result); if ($count == 1) { $updaterecord = true; } else { $updaterecord = false; } if ($updaterecord == true) { $query = "UPDATE Pilot_Data SET pilotdate = '".$pilotdate."', pilottime = '".$pilottime."', pilotcorp = '".$pilotcorp."', pilotcorpid = '".$pilotcorpid."', pilotalli = '".$pilotalli."', pilotalliid = '".$pilotalliid."', pilotcorprole = '".$pilotcorprole."', pilotregion = '".$pilotregion."', pilotconst = '".$pilotconst."', pilotsystem = '".$pilotsystem."', pilotnear = '".$pilotnear."', pilotstation = '".$pilotstation."' WHERE pilotname = '".$pilotname."'"; } else { $query = "INSERT INTO Pilot_Data (pilotname, pilotdate, pilottime, pilotnameid, pilotcorp, pilotcorpid, pilotalli, pilotalliid, pilotcorprole, pilotregion, pilotconst, pilotsystem, pilotnear, pilotstation ) Values ('".$pilotname."', '".$pilotdate."', '".$pilottime."', '".$pilotnameid."', '".$pilotcorp."', '".$pilotcorpid."', '".$pilotalli."', '".$pilotalliid."', '".$pilotcorprole."', '".$pilotregion."', '".$pilotconst."', '".$pilotsystem."', '".$pilotnear."', '".$pilotstation."')"; } mysql_query($query); mysql_close($dbmain); } ?> you seem to be missing a few steps to determine if the pilot name in the db is the same as the pilot name stored in the variable. instead parse on the basis of a count, if the count is 1 then the user exists, therefore run the update, if not the run the insert. Quote Link to comment Share on other sites More sharing options...
JohnM1983 Posted September 4, 2008 Author Share Posted September 4, 2008 Try this... you seem to be missing a few steps to determine if the pilot name in the db is the same as the pilot name stored in the variable. instead parse on the basis of a count, if the count is 1 then the user exists, therefore run the update, if not the run the insert. This worked. Thank you very much. ~JohnM Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.