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;) Link to comment https://forums.phpfreaks.com/topic/122739-solved-php-mysql-update-code-not-working/ 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. Link to comment https://forums.phpfreaks.com/topic/122739-solved-php-mysql-update-code-not-working/#findComment-633828 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 Link to comment https://forums.phpfreaks.com/topic/122739-solved-php-mysql-update-code-not-working/#findComment-633831 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. Link to comment https://forums.phpfreaks.com/topic/122739-solved-php-mysql-update-code-not-working/#findComment-633833 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. Link to comment https://forums.phpfreaks.com/topic/122739-solved-php-mysql-update-code-not-working/#findComment-633839 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 Link to comment https://forums.phpfreaks.com/topic/122739-solved-php-mysql-update-code-not-working/#findComment-633847 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.