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

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.