PHP Dropdown and database

眉间皱痕 提交于 2019-12-12 04:10:21

问题


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

回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/36361215/php-dropdown-and-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!