PHP Dropdown and database

2019-09-10 02:15发布

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();
?>

2条回答
Root(大扎)
2楼-- · 2019-09-10 02:59

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();
?>
查看更多
可以哭但决不认输i
3楼-- · 2019-09-10 03:03

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

查看更多
登录 后发表回答