Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am creating a php page to connect to a database table to change the \'pricing\

ID: 3621574 • Letter: I

Question

I am creating a php page to connect to a database table to change the 'pricing' in the table. However, the code seems to execute, but the table is not changed. Also, the php part of the code works before the submit button is pressed. Any ideas.

here is the site where the page is currently hosted. http://babbage.cs.missouri.edu/~bsrpn6/pricechange.php

Here is the code (without giving you database names and usernames and passwords, assume they are established and correct):

$AdultPrice = $_POST['AdultPrice'];
$ChildPrice = $_POST['ChildPrice'];
$TourID = $_Post['TourID'];

// Open link with access to data
$mysqli = new mysqli($db_server, $userName, $passwd, $dbName);

if ($mysqli->connect_error)
{
die('Connect Error (' . $mysqli->connect_errno . ') '. $mysqli->connect_error);
}

else
{
mysql_query("UPDATE Tour SET AdultPrice = '$AdultPrice', ChildPrice = '$ChildPrice'
WHERE TourID = '$TourID'");
echo "Price changes applied to TourID: $TourID.";

}

mysql_close($con);
?>

<body>
<p>To update the pricing of a tour, select which tour and tour time you want to change from the drop down list and input a new price in the text box provided. </p>
<form id="form1" name="form1" method="post" action="pricechange.php">
<p>
<label for="TourID">Tour to Change: </label>
<select name="TourID" id="TourID">
<option value="1000000001">To The Top Tour - Dawn</option>
<option value="1000000002">To The Top Tour - MidDay</option>
<option value="1000000003">To The Top Tour - Dusk</option>
<option value="1000000004">Structural - MidDay</option>
</select>
</p>
<p>
<label for="AdultPrice">New Adult Price:</label>
<input type="text" name="AdultPrice" id="AdultPrice" />
(format '###.##')</p>
<p>
<label for="ChildPrice">New Child Price: </label>
<input type="text" name="ChildPrice" id="ChildPrice">
(format '###.##')</p>
<p>
<input type="submit" name="Submit" id="Submit" value="Submit" />
</p>
</form>
<p>&nbsp;</p>
</body>

Explanation / Answer

Thats easy, close all php and mysql database connection code in a if condition, which should check if the button is pressed or if the form is submitted then do db connection and run update query, thats all.

In your given scenario, your button name is Submit so your all of above php code will go inside following if condition...

if(isset($_POST['Submit'])){
/************************ Your code starts here ***********************/
$AdultPrice = $_POST['AdultPrice'];
$ChildPrice = $_POST['ChildPrice'];
$TourID = $_Post['TourID'];

// Open link with access to data
$mysqli = new mysqli($db_server, $userName, $passwd, $dbName);

if ($mysqli->connect_error)
{
die('Connect Error (' . $mysqli->connect_errno . ') '. $mysqli->connect_error);
}

else
{
mysql_query("UPDATE Tour SET AdultPrice = '$AdultPrice', ChildPrice = '$ChildPrice'
WHERE TourID = '$TourID'");
echo "Price changes applied to TourID: $TourID.";
}

mysql_close($con);
/************************ Your code ends here ***********************/
}
?>

If you still find any problem regarding this topic, let me know.

Thank you!