Jump to content

[SOLVED] PHP MYSQL (UPDATE) - Code not working...


JohnM1983

Recommended Posts

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;)

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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.