0

当用户选择他的状态月份时,我试图创建一个动态下拉菜单,并且它搜索 mysql 数据库的年份,该数据库提取与状态、月份和年份相关的正确信息。当我单击提交按钮时,没有任何反应,我也没有在屏幕上收到任何错误。

我将不胜感激您能给我的任何建议和/或可能包含我需要的信息的网站。如果我应该以其他方式做,请告诉我。

    <?php

 error_reporting(E_ALL);
    ini_set('display_errors', '1');

require_once('members/connectvars.php');

// Connect to the database
  $dbc = @mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

  if (!$dbc) {
    trigger_error('Could not connect to MySQL: ' . mysqli_connect_error());


        if($_POST)
        {
        $state = $_POST['state']; 
        $month = $_POST['month']; 
        $year = $_POST['year']; 


$query = mysql_query("SELECT * FROM triathlons WHERE state = '".$state."' month     ='".$month."' year = '".$year."' ") or die(mysql_error());

$num = mysql_num_rows($query);

echo "$num results found!<br>";

while($result = mysql_fetch_assoc($query))
{
$day = $result['day'];
$date = $result['date'];
$city= $result['city'];
$racename= $result['racename'];
$park= $result['park'];
$website= $result['website'];


echo "<b> day</b> : $day<br>
<b>Last Name : </b> $date<br>
<b>city : </b>$city<br>
<b>state : </b>$state <br><br>";
}
}


} 
?>

 <form  method="POST" id="filtermainpage" action="<?php echo $_SERVER['PHP_SELF']; ?>">

      <label for="state">State:</label>
        <select name="state" id="state1">



        <option selected="selected" value="">All States</option>
          <option value="AL">Alabama</option>
          <option value="AK">Alaska</option>
          <option value="AZ">Arizona</option>
          <option value="AR">Arkansas</option>
          <option value="CA">California</option>
          <option value="CO">Colorado</option>
          <option value="CT">Connecticut</option>
          <option value="DC">D.C.</option>
          <option value="DE">Delaware</option>
          <option value="FL" >Florida</option>
          <option value="GA">Georgia</option>
          <option value="HI">Hawaii</option>
          <option value="ID">Idaho</option>
          <option value="IL">Illinois</option>
          <option value="IN">Indiana</option>
          <option value="IA">Iowa</option>
          <option value="KS">Kansas</option>
          <option value="KY">Kentucky</option>
          <option value="LA">Louisiana</option>
          <option value="ME">Maine</option>
          <option value="MD">Maryland</option>
          <option value="MA">Massachusetts</option>
          <option value="MI">Michigan</option>
          <option value="MN">Minnesota</option>
          <option value="MS">Mississippi</option>
          <option value="MO">Missouri</option>
          <option value="MT">Montana</option>
          <option value="NE">Nebraska</option>
          <option value="NV">Nevada</option>
          <option value="NH">New Hampshire</option>
          <option value="NJ">New Jersey</option>
          <option value="NM">New Mexico</option>
          <option value="NY">New York</option>
          <option value="NC">North Carolina</option>
          <option value="ND">North Dakota</option>
          <option value="OH">Ohio</option>
          <option value="OK">Oklahoma</option>
          <option value="OR">Oregon</option>
          <option value="PA">Pennsylvania</option>
          <option value="RI">Rhode Island</option>
          <option value="SC">South Carolina</option>
          <option value="SD">South Dakota</option>
          <option value="TN">Tennessee</option>
          <option value="TX">Texas</option>
          <option value="UT">Utah</option>
          <option value="VT">Vermont</option>
          <option value="VA">Virginia</option>
          <option value="WA">Washington</option>
          <option value="WV">West Virginia</option>
          <option value="WI">Wisconsin</option>
          <option value="WY">Wyoming</option>

        </select>

    </p>
    <br
    <br

      <label for="month">Month:</label>
        <select name="month" id="month1">

        <option selected="selected" value="0">All Months</option>
          <option value="1">January</option>
          <option value="2">February</option>
          <option value="3">March</option>
          <option value="4">April</option>
          <option value="5">May</option>
          <option value="6">June</option>
          <option value="7">July</option>
          <option value="8">August</option>
          <option value="9">September</option>
          <option value="10">October</option>
          <option value="11">November</option>
          <option value="12">December</option>

      </select>


      <label for="year">Year:</label>
      <select name="year" id="year1">
          <option value="2013">2013</option>
          <option value="2012">2012</option>
          <option value="2011">2011</option>


      </select>
      </label>
      </p>


    <p>
      <input type="submit" name="submit" id="button" value="submit">
    </p>
  </form> 
4

3 回答 3

0
    if (!$dbc) {
        trigger_error('Could not connect to MySQL: ' . mysqli_connect_error());

            if($_POST) {
                $state = $_POST['state']; 
                $month = $_POST['month']; 
                $year = $_POST['year']; 
    [...]

第一个 if 应该在检查是否发送之前关闭。否则,当您成功连接到数据库时,什么都不会发生。所以与其:

    if (!$dbc) {
        trigger_error('Could not connect to MySQL: ' . mysqli_connect_error());
    }

    else if ($_POST) {
         $state = $_POST['state']; 
         $month = $_POST['month']; 
         $year = $_POST['year']; 
    [...]
于 2013-03-30T20:27:11.547 回答
0

我建议你开始使用 PDO 类!

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

require_once('members/connectvars.php');

// Connect to the database
require_once('databaseConnection.php'),

if(!empty($_POST)) {
    $state = $_POST['state']; 
    $month = $_POST['month']; 
    $year = $_POST['year']; 

    $req = $db->prepare('
                 SELECT * 
                 FROM triathlons 
                 WHERE state = :st 
                 AND month = :mnth 
                 AND year = :yr');

    $req->bindParam(':st', $state, PDO::PARAM_STR);
    $req->bindParam(':mnth', $month, PDO::PARAM_INT);
    $req->bindParam(':yr', $year, PDO::PARAM_INT);
    $req->execute();

    $num = $req->rowCount();
    echo $num . ' results found !<br />';

    while($result = $req->fetch()) {
        $day = $result['day'];
        $date = $result['date'];
        $city = $result['city'];
        $racename = $result['racename'];
        $park = $result['park'];
        $website = $result['website'];

        echo '<br />Day : ' . $day,
             '<br />Last Name : ' . $date,
             '<br />City : ' . $city,
             '<br />Website : ' . $website . '<br />';

    }
}
?>

<form method="POST" id="filtermainpage" action="#">
    <label for="state">State : </label>

    <select name="state" id="state1">
        <option selected="selected" value="">All States</option>
        <option value="AL">Alabama</option>
        <option value="AK">Alaska</option>
        <option value="AZ">Arizona</option>
        <option value="AR">Arkansas</option>
        <option value="CA">California</option>
        <option value="CO">Colorado</option>
        <option value="CT">Connecticut</option>
        <option value="DC">D.C.</option>
        <option value="DE">Delaware</option>
        <option value="FL" >Florida</option>
        <option value="GA">Georgia</option>
        <option value="HI">Hawaii</option>
        <option value="ID">Idaho</option>
        <option value="IL">Illinois</option>
        <option value="IN">Indiana</option>
        <option value="IA">Iowa</option>
        <option value="KS">Kansas</option>
        <option value="KY">Kentucky</option>
        <option value="LA">Louisiana</option>
        <option value="ME">Maine</option>
        <option value="MD">Maryland</option>
        <option value="MA">Massachusetts</option>
        <option value="MI">Michigan</option>
        <option value="MN">Minnesota</option>
        <option value="MS">Mississippi</option>
        <option value="MO">Missouri</option>
        <option value="MT">Montana</option>
        <option value="NE">Nebraska</option>
        <option value="NV">Nevada</option>
        <option value="NH">New Hampshire</option>
        <option value="NJ">New Jersey</option>
        <option value="NM">New Mexico</option>
        <option value="NY">New York</option>
        <option value="NC">North Carolina</option>
        <option value="ND">North Dakota</option>
        <option value="OH">Ohio</option>
        <option value="OK">Oklahoma</option>
        <option value="OR">Oregon</option>
        <option value="PA">Pennsylvania</option>
        <option value="RI">Rhode Island</option>
        <option value="SC">South Carolina</option>
        <option value="SD">South Dakota</option>
        <option value="TN">Tennessee</option>
        <option value="TX">Texas</option>
        <option value="UT">Utah</option>
        <option value="VT">Vermont</option>
        <option value="VA">Virginia</option>
        <option value="WA">Washington</option>
        <option value="WV">West Virginia</option>
        <option value="WI">Wisconsin</option>
        <option value="WY">Wyoming</option>
    </select>

    <br />
    <br />

    <label for="month">Month:</label>
    <select name="month" id="month1">
        <option selected="selected" value="0">All Months</option>
        <option value="1">January</option>
        <option value="2">February</option>
        <option value="3">March</option>
        <option value="4">April</option>
        <option value="5">May</option>
        <option value="6">June</option>
        <option value="7">July</option>
        <option value="8">August</option>
        <option value="9">September</option>
        <option value="10">October</option>
        <option value="11">November</option>
        <option value="12">December</option>
    </select>


    <label for="year">Year:</label>
    <select name="year" id="year1">
        <option value="2013">2013</option>
        <option value="2012">2012</option>
        <option value="2011">2011</option>
    </select>

    <p>
        <input type="submit" name="submit" id="button" value="submit">
    </p>
</form>

在 databaseConnection.php 文件中:

try {
    $pdo_options = array();
    //Optional
    $pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
    $pdo_options[PDO::MYSQL_ATTR_INIT_COMMAND] = "SET NAMES utf8";

    $db = new PDO('mysql:host='.DB_HOST.';
                         dbname=' . DB_NAME, 
                         DB_USER, 
                         DB_PASSWORD, 
                         $pdo_options);
}
catch (Exception $e) {
    die('Error : ' . $e->getMessage());
}

您可以顺便将数据库标识符放在 databaseConnection.php 文件中

于 2013-03-30T20:31:23.580 回答
0

安全性:请不要只在您的 sql 中使用 $_POST['foo'],验证它们是否是您期望的值,因为它会使您的系统受到许多攻击。

当您提交到同一页面时,您的表单中的操作现在是可选的。

您的 if (!$dbc) trigger_error 之后还缺少一个 },因此请在 if 条件之后添加 } 或删除 {,因为 if 内的指令只有一行,大括号是可选的。

于 2013-03-30T20:31:28.517 回答