0

I'm new to php. I have a dropdown menu in my form and the dropdown options are coming from a database and I'm trying to insert the selected options in the dropdown menu to a separate table in my database. The query seems to be getting executed but the team name values are not being inserted into the database. This is the code for the form. Any help is much appreciated!

<form class="form-register" method="POST" enctype="multipart/form-data">
Match Type
  <select class="form-control" name="MatchType" value="Match Type">
<option value="Select one">Select One</option>
<option value="T20">Twenty20 Match</option>
<option value="OneDay">One-Day Match</option>
<option value="Test">Test Match</option> </select>
Home Team
<?php  
mysql_select_db('cricket_system');
$sql = "SELECT TeamName FROM teams";
  echo "<select class='form-control' name='Team1' value='Team1'>";
    while ($row = mysql_fetch_array($result)) {
      echo "<option value='". $row['TeamName']."'>". $row['TeamName']."</option>";
    }
 echo "</select> "; 
 ?> 
  Away Team
  <?php  
mysql_select_db('cricket_system');
    $sql1 = "SELECT TeamName FROM teams";
    $result1 = mysql_query($sql1);
   echo "<select class='form-control' name='Team2' value='Team2'>";
    while ($row = mysql_fetch_array($result1)) {
      echo "<option value='". $row['TeamName']."'>". $row['TeamName']."</option>";
    }
 echo "</select> "; 
 ?>   
   Date (yyyy/mm/dd)
<input type="text" id="Date" name="Date" class="form-control" placeholder="Date (yyyy/mm/dd)" required>
<br><button class="signupbutton" type="submit" name="submit" >Add Match</button> <br> <br>
 </form>
<?php
include('includes/database.php');
mysql_select_db('cricket_system');
if(isset($_POST['submit'])){
 $Team1 = $_POST['Team1'];
 $Team2 = $_POST ['Team2'];
 $MatchType = $_POST['MatchType'];
  $insert = "INSERT INTO matches (Team1, Team2, Date, MatchType) values 
  ('$Team1', '$Team2', '$Date', '$MatchType')";
  $add = mysql_query($insert);
  if ($add) {
      echo "<script>alert('Match has been successfully added.')</script>";
  }
  else {
      echo mysql_error();
  }
}
mysql_close();
?>
M. Aleem
  • 9
  • 2
  • Do you have error reporting turned on for your PHP code ? – Maximus2012 Apr 01 '16 at 16:26
  • `$sql` is not being executed, so that explains why your Home Teams wont appear. – Ash Apr 01 '16 at 16:26
  • 1
    [Your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Apr 01 '16 at 16:27
  • 1
    Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Apr 01 '16 at 16:27
  • @JayBlanchard Are you a bot or a human? Just becasue you post those same two comments on every question with `mysql_*` ;) – Ash Apr 01 '16 at 16:29
  • I am most certainly not a bot! ¯\\_(ツ)_/¯ I post those comments (sometimes others) because they are important @ash – Jay Blanchard Apr 01 '16 at 16:30
  • has anyone any clue?? – M. Aleem Apr 01 '16 at 16:35
  • @jay it was a joke :p – Ash Apr 01 '16 at 16:37
  • @jay oops I totally misread your response haha :D – Ash Apr 01 '16 at 16:39

2 Answers2

2

You are missing the $Date variable that i added in the code

<form class="form-register" method="POST" enctype="multipart/form-data">
Match Type
  <select class="form-control" name="MatchType" value="Match Type">
<option value="Select one">Select One</option>
<option value="T20">Twenty20 Match</option>
<option value="OneDay">One-Day Match</option>
<option value="Test">Test Match</option> </select>
Home Team
<?php  
mysql_select_db('cricket_system');
$sql = "SELECT TeamName FROM teams";
  echo "<select class='form-control' name='Team1' value='Team1'>";
    while ($row = mysql_fetch_array($result)) {
      echo "<option value='". $row['TeamName']."'>". $row['TeamName']."</option>";
    }
 echo "</select> "; 
 ?> 
  Away Team
  <?php  
mysql_select_db('cricket_system');
    $sql1 = "SELECT TeamName FROM teams";
    $result1 = mysql_query($sql1);
   echo "<select class='form-control' name='Team2' value='Team2'>";
    while ($row = mysql_fetch_array($result1)) {
      echo "<option value='". $row['TeamName']."'>". $row['TeamName']."</option>";
    }
 echo "</select> "; 
 ?>   
   Date (yyyy/mm/dd)
<input type="text" id="Date" name="Date" class="form-control" placeholder="Date (yyyy/mm/dd)" required>
<br><button class="signupbutton" type="submit" name="submit" >Add Match</button> <br> <br>
 </form>
<?php
include('includes/database.php');
mysql_select_db('cricket_system');
if(isset($_POST['submit'])){
 $Team1 = $_POST['Team1'];
 $Team2 = $_POST ['Team2'];
$Date= $_POST ['Date'];
 $MatchType = $_POST['MatchType'];
  $insert = "INSERT INTO matches (Team1, Team2, Date, MatchType) values 
  ('$Team1', '$Team2', '$Date', '$MatchType')";
  $add = mysql_query($insert);
  if ($add) {
      echo "<script>alert('Match has been successfully added.')</script>";
  }
  else {
      echo mysql_error();
  }
}
mysql_close();
?>
Umair Khan
  • 283
  • 3
  • 13
0

The SQL for the first dropdown never gets populated as the $result variable is never set therefore the $_POST["Team1"] is null or empty/unset. The first dropdown would have never worked. Try add the following:

$result = mysql_query($sql);

What I would suggest is to add the code and then submit it again once all the values are in both Team dropdowns.

As a simple check do the following when assigning the team names

$Team1 = isset($_POST['Team1']) ? $_POST['Team1'] : '';

If this inserts an empty value for Team1 in the database table then it proofs the above dropdown was the problem

Conrad Lotz
  • 8,200
  • 3
  • 23
  • 27