I have been studying PHP/MySQL on and off again for the past few years and I decided the only way I was going to learn it was to create my own web application. We're going on a road trip in a few days and I got the idea to create an application to track our fuel consumption, lodging, meals, etc. I've been doing pretty good until the past couple of days.

The first page essentially prints everything from the database in a nice looking table. Beside each entry there is an "Edit" link that will take you to a page where you can edit the entry. When you go to the edit page the information currently in the database is populated into the form. When you click on submit it goes to another page where the update query is (suppose to be) performed. However, despite the friendly message that I created to inform me the database has been updated, the database is not updated with any information.

Before I show you some code I would like to point out that I know it is possible to do all of this on one page and probably for the better. Eventually, I would like to get to that point. But right now I am in a hurry to get the basics of this application working. To save some space I will only share the edit page and the submit update query page. Any help would be GREATLY appreciated! OH! I also want to point out that on the last page I took the ISSET statement out. If the statement is in there or not, the script performs the same. So go ahead and tell me the obvious mistake I'm overlooking! :$

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>iStop > Edit Fuel Stop Record</title>
<meta http-equiv="content-type"
	content="text/html; charset=iso-8859-1" />

<link rel="stylesheet" href="style.css" type="text/css" media="screen" />

</head>
<body>

<h1>iStop > Edit Fuel Stop Record</h1>
<br /><hr><br />

<?php

//Connect to the database server
$dbcnx = @mysql_connect('localhost', 'username', 'pass');
if (!$dbcnx) {
	exit('<p>Unable to connect to the '. ' database server at this time.</p>');
	}
	
//Select the istop database
if (!@mysql_select_db('istop')) {
	exit('<p>Unable to locate the iStop database at this time.</p>');
	}

$id = $_GET['id'];
$fueldata = @mysql_query("SELECT id, town, date, time, station, ppg, gp, pmethod, total, mpg, distance, wifi FROM fuel WHERE id='$id'");
	if (!$fueldata) {
		exit('<p>Error fetching fuel data details: ' . mysql_error() . '</p>');
		}
		
$fueldata = mysql_fetch_array($fueldata);

$id = $fueldata['id'];
$town = $fueldata['town'];
$date = $fueldata['date'];
$time = $fueldata['time'];
$station = $fueldata['station'];
$ppg = $fueldata['ppg'];
$gp = $fueldata['gp'];
$pmethod = $fueldata['pmethod'];
$total = $fueldata['total'];
$mpg = $fueldata['mpg'];
$distance = $fueldata['distance'];
$wifi = $fueldata['wifi'];
		
?>
		
<form action="fuel_record_updated.php?=<?php echo $id; ?>" method="post">
		Date: <INPUT type="text" maxlength="10" size="11" name="date" value="<?php echo $date; ?>" onblur="if(this.value == '') { this.value='<?php echo $date; ?>'}" onfocus="if (this.value == '<?php echo $date; ?>') {this.value=''}" /><BR /><BR />
		Time: <INPUT type="text" maxlength="8" size="11" name="time" value="<?php echo $time; ?>" onblur="if(this.value == '') { this.value='<?php echo $time; ?>'}" onfocus="if (this.value == '<?php echo $time; ?>') {this.value=''}" /><BR /><BR />
		Town & State: <INPUT type="text" size="20" name="<?php echo $town; ?>" onblur="if(this.value == '') { this.value='<?php echo $town; ?>'}" onfocus="if (this.value == '<?php echo $town; ?>') {this.value=''}" /><BR /><BR />
		Gas Station: <INPUT type="text" size="20" name="<?php echo $station; ?>" onblur="if(this.value == '') { this.value='<?php echo $station; ?>'}" onfocus="if (this.value == '<?php echo $station; ?>') {this.value=''}" /><BR /><BR />
		Price Per Gallon: <INPUT type="text" size="11" name="ppg" value="<?php echo $ppg; ?>" onblur="if(this.value == '') { this.value='<?php echo $ppg; ?>'}" onfocus="if (this.value == '<?php echo $ppg; ?>') {this.value=''}" /><BR /><BR />
		Gallons Purchased: <INPUT type="text" maxlength="3" size="11" name="gp" value="<?php echo $gp; ?>" onblur="if(this.value == '') { this.value='<?php echo $gp; ?>'}" onfocus="if (this.value == '<?php echo $gp; ?>') {this.value=''}" /><BR /><BR />

<?php

		if ($pmethod=='Cash') {
				echo 'Payment Method:&nbsp;&nbsp;<INPUT type="radio" name="pmethod" value="Cash" checked /> Cash&nbsp;&nbsp;<INPUT type="radio" name="pmethod" value="Debit" /> Debit&nbsp;&nbsp;<INPUT type="radio" name="pmethod" value="Check" />Check&nbsp;&nbsp;<INPUT type="radio" name="pmethod" value="Credit" /> Credit<BR><BR />';
			}	
			
		if ($pmethod=='Debit') {
				echo 'Payment Method:&nbsp;&nbsp;<INPUT type="radio" name="pmethod" value="Cash" /> Cash&nbsp;&nbsp;<INPUT type="radio" name="pmethod" value="Debit" checked /> Debit&nbsp;&nbsp;<INPUT type="radio" name="pmethod" value="Check" />Check&nbsp;&nbsp;<INPUT type="radio" name="pmethod" value="Credit" /> Credit<BR><BR />';
			}
			
		if ($pmethod=='Check') {
				echo 'Payment Method:&nbsp;&nbsp;<INPUT type="radio" name="pmethod" value="Cash" /> Cash&nbsp;&nbsp;<INPUT type="radio" name="pmethod" value="Debit" /> Debit&nbsp;&nbsp;<INPUT type="radio" name="pmethod" value="Check" checked />Check&nbsp;&nbsp;<INPUT type="radio" name="pmethod" value="Credit" /> Credit<BR><BR />';
			}
			
		if ($pmethod=='Credit') {
				echo 'Payment Method:&nbsp;&nbsp;<INPUT type="radio" name="pmethod" value="Cash" /> Cash&nbsp;&nbsp;<INPUT type="radio" name="pmethod" value="Debit" /> Debit&nbsp;&nbsp;<INPUT type="radio" name="pmethod" value="Check" />Check&nbsp;&nbsp;<INPUT type="radio" name="pmethod" value="Credit" checked /> Credit<BR><BR />';
			}
			
		else {
				echo 'Payment Method:&nbsp;&nbsp;<INPUT type="radio" name="pmethod" value="Cash" /> Cash&nbsp;&nbsp;<INPUT type="radio" name="pmethod" value="Debit" /> Debit&nbsp;&nbsp;<INPUT type="radio" name="pmethod" value="Check" />Check&nbsp;&nbsp;<INPUT type="radio" name="pmethod" value="Credit" /> Credit<BR><BR />';
			}	
		
?>
	
		Total Price: <INPUT type="text" size="11" name="total" value="<?php echo $total; ?>" onblur="if(this.value == '') { this.value='<?php echo $total; ?>'}" onfocus="if (this.value == '<?php echo $total; ?>') {this.value=''}" /><BR /><BR />
		Distance Traveled: <INPUT type="text" maxlength="7" size="11" name="distance" value="<?php echo $distance; ?>" /><BR /><BR />
		
<?php
		
		if ($wifi=='Free Wireless') {
				echo 'Wireless Internet Availability: <INPUT type="radio" name="wifi" value="Free Wireless" checked /> Free Wireless&nbsp;&nbsp;<INPUT type="radio" name="wifi" value="Paid Wireless" /> Paid Wireless&nbsp;&nbsp;<INPUT type="radio" name="wifi" value="Not Available" /> Not Available&nbsp;&nbsp;<INPUT type="radio" name="wifi" value="Unknown" /> Unknown<BR /><BR />';
			}
		if ($wifi=='Paid Wireless') {
				echo 'Wireless Internet Availability: <INPUT type="radio" name="wifi" value="Free Wireless" /> Free Wireless&nbsp;&nbsp;<INPUT type="radio" name="wifi" value="Paid Wireless" checked /> Paid Wireless&nbsp;&nbsp;<INPUT type="radio" name="wifi" value="Not Available" /> Not Available&nbsp;&nbsp;<INPUT type="radio" name="wifi" value="Unknown" /> Unknown<BR /><BR />';
			}
		if ($wifi=='Not Available') {
				echo 'Wireless Internet Availability: <INPUT type="radio" name="wifi" value="Free Wireless" /> Free Wireless&nbsp;&nbsp;<INPUT type="radio" name="wifi" value="Paid Wireless" /> Paid Wireless&nbsp;&nbsp;<INPUT type="radio" name="wifi" value="Not Available" checked /> Not Available&nbsp;&nbsp;<INPUT type="radio" name="wifi" value="Unknown" /> Unknown<BR /><BR />';
			}
		if ($wifi=='Unknown') {
				echo 'Wireless Internet Availability: <INPUT type="radio" name="wifi" value="Free Wireless" /> Free Wireless&nbsp;&nbsp;<INPUT type="radio" name="wifi" value="Paid Wireless" /> Paid Wireless&nbsp;&nbsp;<INPUT type="radio" name="wifi" value="Not Available" /> Not Available&nbsp;&nbsp;<INPUT type="radio" name="wifi" value="Unknown" checked /> Unknown<BR /><BR />';
			}
		else {
				echo 'Wireless Internet Availability: <INPUT type="radio" name="wifi" value="Free Wireless" /> Free Wireless&nbsp;&nbsp;<INPUT type="radio" name="wifi" value="Paid Wireless" /> Paid Wireless&nbsp;&nbsp;<INPUT type="radio" name="wifi" value="Not Available" /> Not Available&nbsp;&nbsp;<INPUT type="radio" name="wifi" value="Unknown" /> Unknown<BR /><BR />';
			}
			
?>
		<input type="submit" value="Submit" />
</form>

</body>
</html>

And the submit update query page

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>iStop > Fuel Stop Record Updated</title>
<meta http-equiv="content-type"
	content="text/html; charset=iso-8859-1" />

<link rel="stylesheet" href="style.css" type="text/css" media="screen" />

</head>
<body>

<h1>iStop > Fuel Stop Record Updated</h1>
<br /><hr><br />

<?php

//Connect to the database server
$dbcnx = @mysql_connect('localhost', 'user', 'password');
if (!$dbcnx) {
	exit('<p>Unable to connect to the '. ' database server at this time.</p>');
	}
	
//Select the istop database
if (!@mysql_select_db('istop')) {
	exit('<p>Unable to locate the iStop database at this time.</p>');
	}
	
		$id = $_POST['id'];
		$town = $_POST['town'];
		$date = $_POST['date'];
		$time = $_POST['time'];
		$station = $_POST['station'];
		$ppg = $_POST['ppg'];
		$gp = $_POST['gp'];
		$pmethod = $_POST['pmethod'];
		$total = $_POST['total'];
		$distance = $_POST['distance'];
		$wifi = $_POST['wifi'];

		$sql = "UPDATE fuel SET
		town='$town',
		date='$date',
		time='$time',
		station='$station',
		ppg='$ppg',
		gp='$gp',
		pmethod='$pmethod',
		total='$total',
		distance='$distance',
		wifi='$wifi'
		WHERE id='$id'";
	
if (mysql_query($sql)) {
		echo '<p>Fuel record was updated successfully updated.</p>';
	} else {
		echo '<p>Error updating fuel record: ' . mysql_error() . '</p>';
	}

?>

<p><a href="stoplog.php">Return to Stop Log Front Page</a></p>

</body>
</html>

Recommended Answers

All 4 Replies

In your HTML page, change this line

<form action="fuel_record_updated.php?=<?php echo $id; ?>" method="post">

with these lines

<form action="fuel_record_updated.php" method="post">
<input type= "hidden" name ="id" value= "<?php echo $id; ?>" >
Member Avatar for rajarajan2017
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

Always check for errors.

Thank you both for your responses. Now when the script runs I get the following MySQL error message: "Query was empty." I've been searching various forums with that error message and it seems most of the time the error is caused by undeclared variables or typos. While I'm not perfect and there very well could be a typo in my code, I don't see one. All of the variables look correct to me. Any further help would be greatly appreciated.

I was able to get this to work, finally. On the edit page I took the echo statements out of the form. Instead I'm now outputting the previously entered information above the form and keeping the form empty for new values. I also reverted back to my if query on the page that updates the record in the database.

Thanks for all the help! It is greatly appreciated!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.