We help IT Professionals succeed at work.

make form work with mysql and php

James Murrell
on
114 Views
Last Modified: 2019-02-26
Hiya hope you guys can help me once again


yes i know php has changed ...   but how to i write a php page to accept details from this form in a mysql database -


thanks in advance

<?php 
	include('profile/validate.php');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>cs97jjm3 Fantasy F1</title>

<!--[if (gt IE 9)|!(IE)]><!-->
<script src="js/jquery.mobile.customized.min.js"></script>
<!--<![endif]-->
<script>
 $(window).load(function(){
  $().UItoTop({ easingType: 'easeOutQuart' });
  $('.gallery .gall_item').touchTouch();
 });
 $(document).ready(function(){
     jQuery('#camera_wrap').camera({
      loader: false,
      pagination: true ,
      minHeight: '500',
      thumbnails: false,
      height: '22.42708333333333%',
      caption: true,
      navigation: false,
      fx: 'mosaic'
    });
    $('.gallery .gall-item').touchTouch();
  });
</script>
<!--[if lt IE 9]>
<script src="js/html5shiv.js"></script>
<link href="css/ie.css" media="screen" rel="stylesheet" />
<![endif]-->
<style>
 margin-top: 100px;
 margin-bottom: 100px;
 margin-right: 150px;
  margin-left: 80px;
  </style>

</head>
<body style="background-color: #FFFFFF; background-image: url('../images/main_texture.png'); margin-top: 80px;
 margin-bottom: 100px;
 margin-right: 150px;
  margin-left: 180px;">
<div class="texture">
<!--==============================
              header
=================================-->
<h2 class="ta__left">
<div class="ta__left">
	Register Have you read the 
	<a href="http://f1cs97jjm3.ddns.net/F1/?page_id=680" target="_parent"><strong>rules</strong></a>?,
			</div>
			<span lang="en-gb">Please Note: one team per email address </span>
			<h2 class="ta__left">Entry Form </h2>
<form id="form1"  class="pure-form pure-form-stacked" method="post" name="form1">
<fieldset>

<label for="Email">Email&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </label>
<input id="Email" name="Email" placeholder="Email Address" required="" type="text" maxlength="50" />
<br class="clear" />
<label for="FullName"><br />Full Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </label>
<input id="FullName" name="FullName" placeholder="Your Name" required="" type="text" maxlength="25" />
<label for="TeamName"><br /><br />Team Name&nbsp; </label>
<input id="TeamName" name="TeamName" placeholder="Your F1 Team Name" required="" type="text"  maxlength="25"/>
<label for="Password"><br /><br />Password&nbsp;&nbsp;&nbsp;&nbsp; </label>
<input id="Password" name="Password" placeholder="enter Password" required="" type="password" maxlength="12"  />
<button type="button" id="eye" title = "click here to see your password" data-toggle="tooltip" >
    <img src="https://cdn0.iconfinder.com/data/icons/feather/96/eye-16.png" alt="eye" />
</button>

<br /><br class="clear" />
<label for="Driver1">Select 1st Driver </label>

<select id="Driver1" class="select-sum" name="Driver1" required="" onchange="updateSelect(this,'Driver2');" >
<option data-id="30" value=" 1   Lewis Hamilton  ">Lewis Hamilton  30 Million </option>
<option data-id="25" value=" 822 Valtteri Bottas "> Valtteri Bottas 25 Million </option>
<option data-id="28" value=" 20  Sebastian Vettel ">Sebastian Vettel 28 Million </option>
<option data-id="22" value=" 844 Charles Leclerc ">Charles Leclerc 22 Million </option>
<option data-id="22" value=" 999 Max Verstappen ">Max Verstappen 22 Million </option>
<option data-id="18" value=" 842 Pierre Gasly">Pierre Gasly 18 Million </option>
<option data-id="14" value=" 817 Daniel Ricciardo ">Daniel Ricciardo 14 Million </option>
<option data-id="13" value=" 807 Nico Hulkenberg ">Nico Hulkenberg 13 Million </option>
<option data-id="12" value=" 154 Romain Grosjean ">Romain Grosjean 12 Million </option>
<option data-id="12" value=" 825 Kevin Magnussen  ">Kevin Magnussen 12 Million </option>
<option data-id="10" value=" 839 Lando Norris">Lando Norris 10 Million </option>
<option data-id="10" value=" 832 Carlos Sainz Jr  ">Carlos Sainz Jr 10 Million </option>
<option data-id="9" value=" 815 Sergio Perez Mendoza ">Sergio Perez Mendoza 9 Million </option>
<option data-id="9" value=" 840 Lance Stroll ">Lance Stroll 9 Million </option>
<option data-id="15" value=" 8   Kimi Raikkonen ">Kimi Raikkonen 15  Million </option>
<option data-id="7" value=" 845 Antonio Giovinazzi ">Antonio Giovinazzi  7 Million </option>
<option data-id="6" value=" 4   Alexander Albon ">Alexander Albon 6 Million </option>
<option data-id="6" value=" 838 Daniil Kvyat ">Daniil Kvyat 6 Million </option>
<option data-id="5" value=" 828 George Russell ">George Russell 5 Million </option>
<option data-id="5" value=" 843 Robert Kubica  ">Robert Kubica 5 Million </option>


</select> <br /><br class="clear" />
<label for="Driver2">Select 2nd Driver</label>
<select id="Driver2" class="select-sum" name="Driver2" required="" onchange="updateSelect(this,'Driver1');" >
<option data-id="30" value=" 1   Lewis Hamilton  ">Lewis Hamilton  30 Million </option>
<option data-id="25" value=" 822 Valtteri Bottas "> Valtteri Bottas 25 Million </option>
<option data-id="28" value=" 20  Sebastian Vettel ">Sebastian Vettel 28 Million </option>
<option data-id="22" value=" 844 Charles Leclerc ">Charles Leclerc 22 Million </option>
<option data-id="22" value=" 999 Max Verstappen ">Max Verstappen 22 Million </option>
<option data-id="18" value=" 842 Pierre Gasly">Pierre Gasly 18 Million </option>
<option data-id="14" value=" 817 Daniel Ricciardo ">Daniel Ricciardo 14 Million </option>
<option data-id="13" value=" 807 Nico Hulkenberg ">Nico Hulkenberg 13 Million </option>
<option data-id="12" value=" 154 Romain Grosjean ">Romain Grosjean 12 Million </option>
<option data-id="12" value=" 825 Kevin Magnussen  ">Kevin Magnussen 12 Million </option>
<option data-id="10" value=" 839 Lando Norris">Lando Norris 10 Million </option>
<option data-id="10" value=" 832 Carlos Sainz Jr  ">Carlos Sainz Jr 10 Million </option>
<option data-id="9" value=" 815 Sergio Perez Mendoza ">Sergio Perez Mendoza 9 Million </option>
<option data-id="9" value=" 840 Lance Stroll ">Lance Stroll 9 Million </option>
<option data-id="15" value=" 8   Kimi Raikkonen ">Kimi Raikkonen 15  Million </option>
<option data-id="7" value=" 845 Antonio Giovinazzi ">Antonio Giovinazzi  7 Million </option>
<option data-id="6" value=" 4   Alexander Albon ">Alexander Albon 6 Million </option>
<option data-id="6" value=" 838 Daniil Kvyat ">Daniil Kvyat 6 Million </option>
<option data-id="5" value=" 828 George Russell ">George Russell 5 Million </option>
<option data-id="5" value=" 843 Robert Kubica  ">Robert Kubica 5 Million </option>

</select> 
<br /><br class="clear" />
<label for="Team1">Your Constructor </label>
<select class="select-sum" id="Team1" name="Team1" required="">
<option data-id="30" value="131 Mercedes ">Mercedes AMG Petronas Motorsport 30 Million </option>
<option data-id="22" value="6   Ferrari  ">Scuderia Ferrari Mission Winnow 22 Million </option>
<option data-id="20" value="9   Red Bull  ">Aston Martin Red Bull Racing 20 Million </option>
<option data-id="15" value="4   Renault  ">Renault F1 Team 15 Million </option>
<option data-id="14" value="210 Haas ">Rich Energy Haas F1 Team 14 Million </option>
<option data-id="13" value="1   McLaren ">McLaren F1 Team 13 Million </option>
<option data-id="12" value="10  Racing Point  ">Racing Point F1 Team 12 Million </option>
<option data-id="11" value="15  Alfa Romeo Racing  ">Alfa Romeo Racing 11 Million </option>
<option data-id="11" value="5   Toro Rosso  ">Red Bull Toro Rosso Honda 11 Million </option>
<option data-id="10" value="3   Williams  ">Williams Martini Racing 10 Million </option>




</select> <br />


<br /><label for="MyRaceId">Double Points Race&nbsp;&nbsp;&nbsp;&nbsp; </label>
<select id="MyRaceId" name="MyRaceId" required="">
<option value="989 Australian Grand Prix">Australian Grand Prix</option>
<option value="990 Bahrain Grand Prix">Bahrain Grand Prix</option>
<option value="991 Chinese Grand Prix">Chinese Grand Prix</option>
<option value="992 Azerbaijan Grand Prix">Azerbaijan Grand Prix</option>
<option value="993 Spanish Grand Prix">Spanish Grand Prix</option>
<option value="994 Monaco Grand Prix">Monaco Grand Prix</option>
<option value="995 Canadian Grand Prix">Canadian Grand Prix</option>
<option value="996 French Grand Prix">French Grand Prix</option>
<option value="997 Austrian Grand Prix">Austrian Grand Prix</option>
<option value="998 British Grand Prix">British Grand Prix</option>
<option value="999 German Grand Prix">German Grand Prix</option>
<option value="1000Hungarian Grand Prix">Hungarian Grand Prix</option>
<option value="1001Belgian Grand Prix">Belgian Grand Prix</option>
<option value="1002Italian Grand Prix">Italian Grand Prix</option>
<option value="1003Singapore Grand Prix">Singapore Grand Prix</option>
<option value="1004Russian Grand Prix">Russian Grand Prix</option>
<option value="1005Japanese Grand Prix">Japanese Grand Prix</option>
<option value="1007Mexican Grand Prix">Mexican Grand Prix</option>
<option value="1006United States Grand Prix">United States Grand Prix</option>
<option value="1008Brazilian Grand Prix">Brazilian Grand Prix</option>
<option value="1009Abu Dhabi Grand Prix">Abu Dhabi Grand Prix</option>

</select> 

<br class="clear" />
<input id="Amount" name="Amount" placeholder="Your F1 Team Name" required="" type="hidden" size="3" hidden /><br>
<h1>Tie-Break </h1>
<label for="LewisEndofyearPosition">Lewis Hamilton's championship position at the end of year&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </label>
<select id="LewisEndofyearPosition" name="LewisEndofyearPosition">
<!--	<label for="LewisEndofyearPosition"><br>Postion for Lewis at end of year </label><select name="LewisEndofyearPosition" id="LewisEndofyearPosition" required>
-->
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
</select> <br /><br class="clear" />
<label for="Total">The constructor who wins the most pole positions in qualifying&nbsp;<br></label><select id="Total" name="Total" required="">
<option  value="131 Mercedes ">Mercedes AMG Petronas Motorsport  </option>
<option  value="6   Ferrari  ">Scuderia Ferrari Mission Winnow </option>
<option value="9   Red Bull  ">Aston Martin Red Bull Racing  </option>
<option  value="4   Renault  ">Renault F1 Team  </option>
<option  value="210 Haas ">Rich Energy Haas F1 Team  </option>
<option  value="1   McLaren ">McLaren F1 Team  </option>
<option  value="10  Racing Point  ">Racing Point F1 Team  </option>
<option  value="15  Alfa Romeo Racing  ">Alfa Romeo Racing  </option>
<option  value="5   Toro Rosso  ">Red Bull Toro Rosso Honda  </option>
<option  value="3   Williams  ">Williams Martini Racing  </option>
</select> <br class="clear" /><br />
<!--<input type="text" name="MyRaceId" id="MyRaceId" required/>-->

 
  <!-- Latest compiled and minified JavaScript -->
<h3>Your Total is Spend in Millions is  <div id="result"> </div> </h3> 

<button type="submit" value="Submit Team" class="btn" id="submit-button">Submit</button>

	
	


 <p></p>
</fieldset>
			</form>
		</div>
	</div>
	
	
	
	<!--==============================
              footer
=================================-->
    </div>

</section>
</div>
<footer id="footer">


<div class="copyright"> 
      </div>
    </div>
  </div>
  <div class="clear"></div>
</footer>
<a id="toTop" class="fa fa-chevron-up" href="#"></a>
<script src="js/isotop_ini.js"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script>
$(function() {
  setScore();
  $('.select-sum').change(function() {
	setScore();
  });
});
function setScore()
{
    var total = 0;
    $('.select-sum').each(function() {
      var sel = $(this).find('option:selected').data('id');
      total += sel;
    });

    // Set a boolean based on score < 45
    // this is used to set the disabled state
    // without using an if statement
    var disabled = total > 48;

    // because not all submit buttons are <input>'s
    // use the disabled status above to set disabled state
    $('#submit-button').prop({disabled: disabled});
	

    $('#result').html(total);
    var my_txt = $('#result').text(); 
	$("input[name='Amount']").val(my_txt);
}
</script>

<script>
function updateSelect(changedSelect, selectId) {
   var otherSelect = document.getElementById(selectId);
   for (var i = 0; i < otherSelect.options.length; ++i) {
       otherSelect.options[i].disabled = false;
   }
   if (changedSelect.selectedIndex == 0) {
      return;
   }
   otherSelect.options[changedSelect.selectedIndex].disabled = true;
}</script>

<script>
function show() {
    var p = document.getElementById('Password');
    p.setAttribute('type', 'text');
}

function hide() {
    var p = document.getElementById('Password');
    p.setAttribute('type', 'password');
}

var pwShown = 0;

document.getElementById("eye").addEventListener("click", function () {
    if (pwShown == 0) {
        pwShown = 1;
        show();
    } else {
        pwShown = 0;
        hide();
    }
}, false);
</script>
</body>
</html>
	

Open in new window

Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
James MurrellQA Product Specialist
CERTIFIED EXPERT

Author

Commented:
thanks sorry for delay. trying this tonight
James MurrellQA Product Specialist
CERTIFIED EXPERT

Author

Commented:
ok thanks i have tried here is update code


<?php 

// connect to your DB
require_once 'db.php';

// safely check you have some values from your form

$Email =isset($_POST['Email']);  
$FullName =isset($_POST['FullName']);  
$Driver1 =isset($_POST['Driver1']);  
$Driver2 =isset($_POST['Driver2']);  
$Team1 =isset($_POST['Team1']);  
$LewisEndofyearPosition =isset($_POST['LewisEndofyearPosition']);  
$Amount =isset($_POST['Amount']);  
$Total =isset($_POST['Total']);  
$TeamName =isset($_POST['TeamName']);  
$Password =isset($_POST['Password']); 
$pw = md5($Password);
$MyRaceId =isset($_POST['MyRaceId']); 


// get your SQL query ready. We're using a parameterised query here for security!
$sql = "INSERT INTO f1users ( `Email`, `FullName`, `Driver1`, `Driver2`, `Team1`, `LewisEndofyearPosition`, `Amount`, `Total`, `TeamName`, `Password`, `MyRaceId`) VALUES ('$Email', '$FullName', '$Driver1', '$Driver2', '$Team1', '$LewisEndofyearPosition', '$Amount', '$Total', '$TeamName', '$pw', '$MyRaceId')";




try {

    // Prepare the query
    $insert = $db->prepare($sql);

    // Run the query
    $insert->execute([
        `Email`=> $Email,
        `FullName` => $FullName,
        `Driver1` => $Driver1, 
        `Driver2` => $Driver2,
        `Team1` => $Team1, 
        `LewisEndofyearPosition` => $LewisEndofyearPosition, 
        `Amount` => $Amount, 
        `Total` => $Total, 
        `TeamName`=> $TeamName, 
        `Password` => $pw, 
        `MyRaceId` => $MyRaceId

    ]);

    echo "<p>Record was inserted into the DB</p>";

} catch ( Exception $e ) {

    // Something went wrong so let's bomb out with a message!
    die ( $e->getMessage() );

}
?>

Open in new window


here is db code : important bits removed :-)

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);
 
$username = '?????????';
$password = '?????????????';
$dsn      = 'mysql:host=localhost;dbname=F1database;charset=utf8mb4'; 
$options  = [
    PDO::ATTR_EMULATE_PREPARES   => false,
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,
];

try {
    $db = new PDO($dsn, $username, $password, $options);
} catch( PDOException $e ) {
    die( $e->getMessage() );
}
?>

Open in new window



i am getting message Record was inserted into the DB   but nothing in database ?????

feel free to see form at http://f1cs97jjm3.ddns.net/register.html

quick page of results at http://f1cs97jjm3.ddns.net/ava.php
CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
James MurrellQA Product Specialist
CERTIFIED EXPERT

Author

Commented:
Thanks Chris and i really mean that: i had a major headache over this: plus a real headache for 4 days :-(

i have / i think i have done as you said  but i now get SQLSTATE[HY093]: Invalid parameter number: parameter was not defined

<?php 

// connect to your DB
require_once 'db.php';

// safely check you have some values from your form


$Email =isset($_POST['Email']) ? $_POST['Email'] : "";  
$FullName =isset($_POST['FullName']) ? $_POST['FullName'] : "";  
$Driver1 =isset($_POST['Driver1']) ? $_POST['Driver1'] : "";  
$Driver2 =isset($_POST['Driver2']) ? $_POST['Driver2'] : "";  
$Team1 =isset($_POST['Team1']) ? $_POST['Team1'] : "";  
$LewisEndofyearPosition =isset($_POST['LewisEndofyearPosition']) ? $_POST['LewisEndofyearPosition'] : "";  
$Amount =isset($_POST['Amount']) ? $_POST['Amount'] : "";  
$Total =isset($_POST['Total']) ? $_POST['Total'] : "";  
$TeamName =isset($_POST['TeamName']) ? $_POST['TeamName'] : "";  
$Password =isset($_POST['Password']) ? $_POST['Password'] : ""; 
$pw = md5($Password);
$MyRaceId =isset($_POST['MyRaceId']) ? $_POST['MyRaceId'] : ""; 


// get your SQL query ready. We're using a parameterised query here for security!
$sql = "INSERT INTO f1users ( `Email`, `FullName`, `Driver1`, `Driver2`, `Team1`, `LewisEndofyearPosition`, `Amount`, `Total`, `TeamName`, `Password`, `MyRaceId`) VALUES (:Email, :FullName, :Driver1, :Driver2, :Team1, :LewisEndofyearPosition, :Amount, :Total, :TeamName, :Password, :MyRaceId)";


try {

    // Prepare the query
    $insert = $db->prepare($sql);

    // Run the query
    $insert->execute([
        `Email`=> $Email,
        `FullName` => $FullName,
        `Driver1` => $Driver1, 
        `Driver2` => $Driver2,
        `Team1` => $Team1, 
        `LewisEndofyearPosition` => $LewisEndofyearPosition, 
        `Amount` => $Amount, 
        `Total` => $Total, 
        `TeamName`=> $TeamName, 
        `Password` => $Password, 
        `MyRaceId` => $MyRaceId

    ]);

    echo "<p>Record was inserted into the DB</p>";

} catch ( Exception $e ) {

    // Something went wrong so let's bomb out with a message!
    die ( $e->getMessage() );

}
?>

Open in new window



sorry . hope you can help me
CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019

Commented:
Ok. You're still using backticks around you array keys.  You need to be using quotes. You have this:

`Email`=> $Email

You need this

'Email'=> $Email
James MurrellQA Product Specialist
CERTIFIED EXPERT

Author

Commented:
Doh Thanks it has written record to sql but with no data....??? only the submission and date added fields which are setup to auto ??? any ideas ?

sql table after insert
CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
James MurrellQA Product Specialist
CERTIFIED EXPERT

Author

Commented:
Great Expert: helped me understand simple errors i was making.  Very understanding and helpful wish i could give more points
CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019

Commented:
Glad I could help James :)