asharoo85 asharoo85 - 7 months ago 18
PHP Question

Issue storing option value in MySQL using PHP

I am trying to use the POST method in order to submit the contents of a form to a pre-created MySQL table. There are different input types for each part of the form including datetime, number and option values and i can't figure out the problem with my code. Any help would be greatly appreciated. HMTL and PHP is below...TIA.

PHP CODE:

<?php

$servername = "localhost";
$username = "root";
$password = "cornwall";

$con=mysqli_connect('localhost','root','cornwall','ibill');
// This code creates a connection to the MySQL database in PHPMyAdmin named 'ibill':

if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// The connection is then checked, if it fails, an echo is sent back to the page stating a connection error.

if($_POST['formSubmit'] == "Submit")
{
$typeofactivity = $_POST['typeofactivity'];
$employer = $_POST['employer'];
$datetime = $_POST['datetime'];
$amount = $_POST['amount'];
$errorMessage = "";

// - - - snip - - -
}

if(empty($typeofactivity)) {
$errorMessage .= "<li>You forgot to enter an activity!</li>";
}
if(empty($employer)) {
$errorMessage .= "<li>You forgot to enter an employer!</li>";
}
if(empty($datetime)) {
$errorMessage .= "<li>You forgot to select the time and date!</li>";
}
if(empty($amount)) {
$errorMessage .= "<li>You forgot to select the amount of the session!</li>";
}

$record_session = "INSERT INTO session_details (typeofactivity, employer, datetime, amount) VALUES ('$typeofactivity', '$employer', '$datetime', '$amount')"
mysql_query($sql);
}



/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);

?>


HTML:

<!--********************************RECORD SESSION PAGE************************************-->
<!--***************************************************************************************-->

<!--********************************HEADER**********************************************-->
<div data-role="page" id="sessionrecord">
<div data-role="header" data-id="foo1" data-position="fixed">
<div class='cssmenu'>
<ul>
<li class='active'><a href='#home'>Home</a></li>
<li><a href='#sessionrecord'>Record a Session</a></li>
<li><a href='#viewsessions'>View Sessions</a></li>
<li><a href='#email'>E-mail an Invoice</a></li>
</ul>
</div>
</div><!-- /header -->
<!--********************************HEADER**********************************************-->

<!--********************************MAIN**********************************************-->
<div data-role="main" class="ui-content">

<img class="mainlogo" src="/projects/ibill_v3/img/ibill logo.png" alt="iBill Logo" width="250" height="190">

<section class="maincontent">
<h1>Record a session using the form below</h1>
<form method="post" action="record_session.php" id="sessionRecord">
<fieldset>
<select name="typeofactivity" id="typeofactivity" data-native-menu="false">
<option>Type of Session</option>
<option value="surf">Surf</option>
<option value="coast">Coasteer</option>
<option value="bodyboard">Bodyboard</option>
<option value="climbing">Cornish Challenge</option>
</select>
</fieldset>
<fieldset>
<select name="employer" id="employer" data-native-menu="false">
<option>Employer</option>
<option value="nac">Newquay Activity Centre</option>
<option value="coastline">Coastline Coasteer</option>
</select>
</fieldset>
<label for="datetime">Date and Time of Session</label>
<input type="datetime-local" data-clear-btn="false" name="datetime" id="datetime" value="">
<label for="amount">Amount (GBP)</label>
<input type="number" data-clear-btn="true" name="amount" id="amount" value="">
<div id="submitbutton">
<input type="submit" name="formSubmit" value="Submit">
</div>
</form>

</section>
</div>
<!--********************************MAIN**********************************************-->

<!--********************************FOOTER**********************************************-->
<div data-role="footer">
<footer class="footer">
<p>awilliams&copy;</p>
</footer>
</div>
</div>
<!--********************************FOOTER**********************************************-->

<!--********************************END OF RECORD SESSION PAGE************************************-->
<!--***************************************************************************************-->

Answer

please post your error, but I noticed you left a ; off the end of this line:

$record_session = "INSERT INTO session_details (typeofactivity, employer, datetime, amount) VALUES ('$typeofactivity', '$employer', '$datetime', '$amount')"

also you had that extra } after the snip section.

also you're not posting formSubmit data as a submit button.

so try this just to get started, then you can work on input validation/sanitization:

<?php

$servername = "localhost";
$username = "root";
$password = "cornwall";

$con=mysqli_connect('localhost','root','cornwall','ibill');
// This code creates a connection to the MySQL database in PHPMyAdmin named 'ibill':

if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// The connection is then checked, if it fails, an echo is sent back to the page stating a connection error.

if(!empty($_POST)) 
{
    $typeofactivity = $_POST['typeofactivity'];
    $employer = $_POST['employer'];
    $datetime = $_POST['datetime'];
    $amount = $_POST['amount'];
    $errorMessage = "";

    if(empty($typeofactivity)) {
      $errorMessage .= "<li>You forgot to enter an activity!</li>";
    }
    if(empty($employer)) {
      $errorMessage .= "<li>You forgot to enter an employer!</li>";
    }
    if(empty($datetime)) {
      $errorMessage .= "<li>You forgot to select the time and date!</li>";
    }
    if(empty($amount)) {
      $errorMessage .= "<li>You forgot to select the amount of the session!</li>";
    }

    $record_session = "INSERT INTO session_details (typeofactivity, employer, datetime, amount) VALUES ('$typeofactivity', '$employer', '$datetime', '$amount')";
    mysql_query($sql);
}

/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);

?>