Hello I cannot figure out what is wrong with my code. Have not much experience with php yet. Can someone please tell me what am I doing wrong??
Here is my code:
<?php
include 'mysql_connect.php';
if (!isset($_POST['submit'])) {
$fuelQuery2 = sprintf("UPDATE fuel_price SET `Price` = '%s' WHERE FuelType = '%s' LIMIT 1",
mysql_real_escape_string($_POST['inputPrice']),
mysql_real_escape_string($_POST['fueltype']));
$Result = mysql_query($fuelQuery2);
if($Result){
echo 'Price has been updated!';
} else{
echo 'Failed to update price!';
}
} else{
echo 'No form submitted';
}
?>
<h1>Update Oil Price</h1>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Oil Price:<input name="inputPrice" type="text" value=""/>
Product
<select name="fueltype">
<option value="Oil">Kero</option>
<option value="Diesel">Diesel</option>
<option value="RedDiesel">Red Diesel</option>
</select>
<input type="submit" name="submit" value="Modify" />
</form>
It’s really simple, change
to
Your original code told PHP to execute the query when the form wasn’t submitted (notice that I removed the !) instead of when it was. The notices you were getting were telling you that the
$_POSTvariables you grabbed for your query didn’t exist (because the code ran before the form was submitted).Also, do look into PDO. The
mysql_family of functions is no longer the preferred method for interacting with the database layer.