php - 有效地从数据库下拉列表中获取值

标签 php mysql pdo

目前我的代码正在运行,但这并不是真正有效,因为存在大量代码重复。不知道这样做的方法。这是一些代码:

<form method='POST'>
        <fieldset>
            <div id="dropDownList">
                <select value="sport" name="sport">
                    <option value="invalid">Please select a sport</option>
                    <option value="show">Show All</option>
                    <?php
                        foreach ($dropDown as $row) {
                            echo'<option value='.$row["sportName"].'>'.$row["sportName"].'</option>';
                        }  
                    ?>
                </select>
            </div>
            <div>
                <button type="submit">Submit</button>
          </div>
        </fieldset>

     <table>
            <tr>
                <th>athleteID</th>
                <th>eventID</th>
                <th>sportID</th>
                <th>lastName</th>
                <th>firstName</th>
                <th>eventName</th>
                <th>sportName</th>
                <th>gender</th>
                <th>image</th>
                <th>medal</th>
            </tr>
            <?php
                if($sportName == 'show') {
                    foreach ($selectString1 as $row) {
                        echo'<tr>';
                        echo'<td>'.$row['athleteID'].'</td>';
                        echo'<td>'.$row['eventID'].'</td>';
                        echo'<td>'.$row['sportID'].'</td>';
                        echo'<td>'.$row['lastName'].'</td>';
                        echo'<td>'.$row['firstName'].'</td>';
                        echo'<td>'.$row['eventName'].'</td>';
                        echo'<td>'.$row['sportName'].'</td>';
                        echo'<td>'.$row['gender'].'</td>';
                        echo'<td><img src="photos/'.$row['image'].'"</td>';
                        echo'<td>'.$row['medal'].'</td>';
                        echo'</tr>';
                    } 
                }

                if($sportName == 'Athletics') {
                    foreach ($selectString3 as $row) {
                        echo'<tr>';
                        echo'<td>'.$row['athleteID'].'</td>';
                        echo'<td>'.$row['eventID'].'</td>';
                        echo'<td>'.$row['sportID'].'</td>';
                        echo'<td>'.$row['lastName'].'</td>';
                        echo'<td>'.$row['firstName'].'</td>';
                        echo'<td>'.$row['eventName'].'</td>';
                        echo'<td>'.$row['sportName'].'</td>';
                        echo'<td>'.$row['gender'].'</td>';
                        echo'<td><img src="photos/'.$row['image'].'"</td>';
                        echo'<td>'.$row['medal'].'</td>';
                        echo'</tr>';
                    } 
                }

                if($sportName == 'CanoeSprint') {
                    foreach ($selectString4 as $row) {
                        echo'<tr>';
                        echo'<td>'.$row['athleteID'].'</td>';
                        echo'<td>'.$row['eventID'].'</td>';
                        echo'<td>'.$row['sportID'].'</td>';
                        echo'<td>'.$row['lastName'].'</td>';
                        echo'<td>'.$row['firstName'].'</td>';
                        echo'<td>'.$row['eventName'].'</td>';
                        echo'<td>'.$row['sportName'].'</td>';
                        echo'<td>'.$row['gender'].'</td>';
                        echo'<td><img src="photos/'.$row['image'].'"</td>';
                        echo'<td>'.$row['medal'].'</td>';
                        echo'</tr>';
                    }
                }
            ?>      
        </table>
    </form>

下面是一些 PHP pdo 代码,我在其中创建了几个 SQL 语句:

 try {
     $selectString3 = $pdo->prepare ('
SELECT a.athleteID
     , a.eventID
     , a.sportID
     , a.lastName
     , a.firstName
     , a.gender
     , e.eventName
     , s.sportName
     , a.gender
     , a.image
     , a.medal
  FROM athlete a 
  JOIN event e
    ON e.eventID = a.eventID 
  JOIN sport s
     ON s.sportID = a.sportID 
 WHERE s.sportID = 1
');
     $selectString3->execute();
} catch (PDOException $e) {
    $error = 'Select statement error';
    include 'error.html.php';
    exit();
}

try {
    $selectString4 = $pdo->prepare ('SELECT athlete.athleteID, 
    athlete.eventID,athlete.sportID, athlete.lastName, athlete.firstName, 
    athlete.gender, event.eventName, sport.sportName, athlete.gender, 
    athlete.image, athlete.medal
    FROM athlete JOIN event ON event.eventID = athlete.eventID JOIN sport ON 
    sport.sportID = athlete.sportID WHERE sport.sportID = 2');
    $selectString4->execute();
} catch (PDOException $e) {
    $error = 'Select statement error';
    include 'error.html.php';
    exit();
}

最佳答案

因为,与不同 sportId 相关的代码没有区别。您可以创建一个查询来获取所有体育项目并根据它进行显示。

更新代码

<table>
  <tr>
    <th>athleteID</th>
    <th>eventID</th>
    <th>sportID</th>
    <th>lastName</th>
    <th>firstName</th>
    <th>eventName</th>
    <th>sportName</th>
    <th>gender</th>
    <th>image</th>
    <th>medal</th>
  </tr>
  <?php
  if(!empty($selectString)) {
    foreach ($selectString as $row) {
      echo'<tr>';
        echo'<td>'.$row['athleteID'].'</td>';
        echo'<td>'.$row['eventID'].'</td>';
        echo'<td>'.$row['sportID'].'</td>';
        echo'<td>'.$row['lastName'].'</td>';
        echo'<td>'.$row['firstName'].'</td>';
        echo'<td>'.$row['eventName'].'</td>';
        echo'<td>'.$row['sportName'].'</td>';
        echo'<td>'.$row['gender'].'</td>';
        echo'<td><img src="photos/'.$row['image'].'"</td>';
        echo'<td>'.$row['medal'].'</td>';
      echo'</tr>';
    } 
  } else {
   echo "<tr><td colspan='7'>No records found!</td></tr>"; 
  }?>      
</table>

查询

<?php
try {
  $selectString = $pdo->prepare ('SELECT athlete.athleteID, 
  athlete.eventID, athlete.sportID, athlete.lastName, athlete.firstName, 
  athlete.gender, event.eventName, sport.sportName, athlete.gender, 
  athlete.image, athlete.medal
  FROM athlete JOIN event ON event.eventID = athlete.eventID JOIN sport 
  ON sport.sportID = athlete.sportID');
  $selectString->execute();
} catch (PDOException $e) {
  $error = 'Select statement error';
  include 'error.html.php';
  exit();
}

编辑-1

<?php

$sportId = $_POST['sport_dropdown']; //Capture your sports dropdown list value.

try {
  $selectString = $pdo->prepare ('SELECT athlete.athleteID, 
  athlete.eventID, athlete.sportID, athlete.lastName, athlete.firstName, 
  athlete.gender, event.eventName, sport.sportName, athlete.gender, 
  athlete.image, athlete.medal
  FROM athlete JOIN event ON event.eventID = athlete.eventID JOIN sport 
  ON sport.sportID = athlete.sportID WHERE sport.sportID = :sportId');
  $selectString->execute(array(':sportId' => $sportId));
} catch (PDOException $e) {
  $error = 'Select statement error';
  include 'error.html.php';
  exit();
}

编辑2

将值中的运动下拉值更改为 sportid。然后根据它来获取。

<select value="sport" name="sport">
  <option value="invalid">Please select a sport</option>
  <option value="show">Show All</option>
  <?php
  foreach ($dropDown as $row) {
    echo'<option value='.$row["sportID"].'>'.$row["sportName"].'</option>';
  }?>
</select>

查询

<?php

$sportId = $_POST['sport'];

$where = "";
$search_sport_id = false;
if(is_numeric($sportId)){
  $search_sport_id = true;
  $where = " WHERE sport.sportID = :sportId";
}

try {
  $selectString = $pdo->prepare ('SELECT athlete.athleteID, 
  athlete.eventID, athlete.sportID, athlete.lastName, athlete.firstName, 
  athlete.gender, event.eventName, sport.sportName, athlete.gender, 
  athlete.image, athlete.medal
  FROM athlete JOIN event ON event.eventID = athlete.eventID JOIN sport 
  ON sport.sportID = athlete.sportID '.$where);
  if($search_sport_id){
    $selectString->execute(array(':sportId' => $sportId));
  } else {
    $selectString->execute();
  }
} catch (PDOException $e) {
  $error = 'Select statement error';
  include 'error.html.php';
  exit();
}

?>

关于php - 有效地从数据库下拉列表中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45115952/

相关文章:

php - 测试具有模型作为依赖项的 laravel 存储库

php解析错误总是在最后一行

php - 如何在 LIMIT 子句中应用 bindValue 方法?

php sql将来自不同数据库的多个表连接在一起

PHP PDO从多维数组批量插入无法插入MySQL

php - 在失败的 gelf 连接上使用 Symfony 2/Monolog 防止内部服务器错误

php - 如何在 PHP 中获取每月的周数?

php - 从数据库表中填充选择下拉列表

java - 如何使用Javafx将TextField设置在顶部?

PHP分页错误