• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Really basic PHP/MySQL question

teriba

Golden Member
Can anybody help me with the code to have single text box change a smallint value in a database.

http://ss.teriba.com/gas.php

This page reads the value fine.

http://ss.teriba.com/changegas.php

This page doesn't work worth a crap. I'm just reading some tutorials on the web, but I can't seem to get it to work.

It's connecting as localhost/root.
Database superstop
Table gas
Value price

Only one value, and I want to be able to change it daily for a truckstop's website.

Thanks for your help. 🙂
 
Well... first thing I noticed right from the start is this:
Warning: Undefined variable: submit in c:\web\ss\changegas.php on line 12

Also, you're more likely to get more helpful advice if the source was posted (hint hint 😉)

Edit - the PHP source as it's on the server, not the source of the HTML generated by the server after it parses the PHP file.

JW
 
I fixed that now, try it again.

Here's where the code stands now in changegas.php:

<html>
<body>
<form action="<? echo $PHP_SELF ?>" method="post">
<?
mysql_pconnect("localhost","root");
mysql_select_db("superstop");
?>
New Gas Price:<input type="Text" name="price" value="">

<input type="Submit" name="submit" value="Enter">
</form>
<?
if($submit)
{
$sql = "UPDATE gas SET price='$price' WHERE id=0";
$result = mysql_query($sql);
echo "Thank you! Information updated.";
}
?>
</body>
</html>
 
And here is gas.php which is working correctly:

<html>
<body>
<?php
$db = mysql_connect("localhost", "root");
mysql_select_db("superstop",$db);
$result = mysql_query("SELECT * FROM gas",$db);
printf("Gas Price: %s
\n", mysql_result($result,0,"price"));
?>
</body>
</html>
 
Okay... I think I may have been able to write a couple scripts to do what you want. I might make a suggestion that you define the table "gas" to have 2 columns. One being the type of gas (i.e. diesel, unleaded, etc.) and the other being the price for that type. For the type, I suggest defining it as a char of length 10, not null, and the primary key for the table. For the price, you can use the smallint of length 3 (or 4) depending on how you represent the price.

Then the code for the price display page would go something like this:
<html>
<body>

<?php

mysql_connect("localhost","root");
mysql_select_db("superstop");

$query = "SELECT * FROM gas"; // Query to get all the data from the table
$result = mysql_query($query); // Run the query

while($row = mysql_fetch_assoc($result)) // Do the following while rows exist in the result
{
echo "$row['type']: $row['price']
"; // Print the type of gas, followed by the price
}

mysql_free_result($result);

?>

</body>
</html>


And the code for the page which updates the price would go something like this:
<html>
<body>

<?php

mysql_connect("localhost","root");
mysql_select_db("superstop");

$query = "SELECT * FROM gas";
$result = mysql_query($query);

echo "Select type of gas to update price:";
?>
<form action = "<?php echo $PHP_SELF ?>" method = "post">
<select name="type" size="1">
<?php
while($row = mysql_fetch_assoc($result))
{
echo "<option value = $row['type']>$row['type']</option>"; // Add a list option for each type of gas
}
?>

</select>


New Gas Price: <input type = "text" name="price" value = ""> <!-- Get the new price -->
<input type = "submit" name="submit" value="Enter"> <!-- Submit button -->
</form>

<?php
if($submit)
{
$query = "UPDATE gas SET price='$price' WHERE type='$type'"; // Update the price for the selected type of gas
$result = mysql_query($query);
echo "Thank you! Information updated.";
}
?>
</body>
</html>


I believe that this will work. However, my webserver is currently down and I'm unable to test the scripts. But it should at least be a starting point.

Re-reading your original message and you said that the table is defined to have only one column for values, but I think you'll have more success using at least 2 columns. I think (though I may be wrong, as I'm learnng MySQL myself) in order to be able to update the table, you need to use one of the existing values as a reference point to search. Creating the column for the type of gas will let you do this. Making it the primary key will help in searching for the value to be updated. Then on the update price page, you'd have the drop-down list with the types of gas, and then you could enter the price from there. It will then use the selected value from the drop-down list as the field that it will search for, and then will update the price column for the selected value.

JW

Edit: hit the reply button too soon 😱
 
Back
Top