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();
?>
9
  • Do you have error reporting turned on for your PHP code ? Commented Apr 1, 2016 at 16:26
  • $sql is not being executed, so that explains why your Home Teams wont appear. Commented Apr 1, 2016 at 16:26
  • 1
    Your script is at risk for SQL Injection Attacks. Commented Apr 1, 2016 at 16:27
  • 1
    Please stop using mysql_* functions. These extensions have been removed in PHP 7. Learn about prepared statements for PDO and MySQLi and consider using PDO, it's really pretty easy. Commented Apr 1, 2016 at 16:27
  • @JayBlanchard Are you a bot or a human? Just becasue you post those same two comments on every question with mysql_* ;) Commented Apr 1, 2016 at 16:29

2 Answers 2

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();
?>
Sign up to request clarification or add additional context in comments.

6 Comments

it would be better you just highlight the few lines of code you modified instead of the whole thing :)
I have that in my original code just accidently missed it out here. It still doesn't work.
Why isn't the $sql being executed?
@UmairKhan no error, the query supposedly is getting executed as the alert appears and other fields are being added to the database just the two teams are being left blank in the database.
you should remove value from the select tag
|
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

2 Comments

If you look at my code, I already have $result = mysql_query($sql);
Still don't see it. there is one $result1 = mysql_query($sql1); for the Away Team but not the Home Team

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.