繁体   English   中英

从SQL Select语句中获取来自PHP下拉选择框的变量where子句

[英]Getting a variable out of SQL Select statement where clause that came from a PHP Drop down select box

我在这里有点困惑。 由于某种原因,我的结果恢复为空白。 我试图允许用户输入名字和姓氏,然后从下拉列表中的5个职位中进行填写。 然后,当他们提交表单时,PHP动作将从数据库中提取特定作业的信息(使用SQL select语句)并为用户显示一些结果。 我遇到的问题是我提取的变量的结果恢复为空白。 我已经尝试了六种不同的方法来使此select语句起作用,但不会让步。

这是我的表格,并且我有PHP来处理displayasselect函数并连接到服务器以及所有其他内容(由于某种原因,这使我难以在此处粘贴代码)...但是我毫无疑问验证一切都很好。 在PHP之后,这是我的表单:

<form class="form-inline" method="get" action="jobTitlePull.php">
    <div class="form-group">
        <label for="firstName">First Name:  </label>
        <input type="text" name = "fname" id="firstName"/>
    </div>
    <div class="form-group"> 
        <label for="lastName">Last Name: </label>
        <input type="text" name = "lname" id="lastName">
     </div>
        <div class="form-group"> 
        <label for="jobTitle">Job Title: </label>
        <?php
            displayAsSelect($list);
        ?>
     </div>
    <input type="submit" class="btn btn-default"></button>
</form>

表格看起来万事大吉。

我遇到的麻烦来自我的动作php,在这里我将其称为jobTitlePull.php。 这是我的代码:

    <?php
    $serverName = "xxxxxx";
    $userName = "xxxxxx";
    $passWord = "xxxxxx";
    $database = "xxxxxx"; // last 4 fields purposefully masked.
    $firstName = $_GET["fname"];
    $lastName = $_GET["lname"];
    $jobTitle = $_GET["jobName"];
    $conn = mysqli_connect($serverName, $userName, $passWord, $database);
    if (!$conn){
        die ("<p>Connection failed: ".mysqli_connect_error()."</p>");
    }
    $queryString = "SELECT jobName, description, posType, basePay FROM Titles WHERE jobName = '$jobTitle'";
    $result = $conn->query($queryString);
    if(!$result){
        die ("<p>Query failed</p>");
    }
    $record = mysqli_fetch_assoc($result); //I think my problem MAY lie here??
    $desc = $record['description']; //or maybe I'm declaring variables wrong?
    $pos = $record['posType'];
    $base = $record['basePay'];
    echo "<h4>Hello $firstName $lastName Job Title: $jobTitle Job Description: $desc Position Type: $pos Base Pay: $base</h4>";
    mysqli_close($conn);
?>

select语句中的字段都是正确的。 我知道这是因为,如果将WHERE jobName = '$jobTitle'更改为WHERE jobName = jobName ,它将允许我使用while语句并循环遍历并打印出数据库Titles部分中所有字段的所有数据。 但是我只需要下拉列表中的数据。 但是,当我将$jobName等同于$jobTitle (并且$jobTitle提取正确的信息)时,它给我回显语句部分的空白结果,其中$desc$pos和$ base的变量在那儿。

请帮忙。

谢谢

我建议您对当前查询采取一些不同的方法,并考虑一些安全的方法来处理代码。

首先,一切基本保持不变:

$serverName = "xxxxxx";
$userName = "xxxxxx";
$passWord = "xxxxxx";
$database = "xxxxxx"; // last 4 fields purposefully masked.

将您的连接更改为OOP方法,在处理Prepared Statemtns时会更容易

$conn = new mysqli($serverName, $userName, $passWord, $database);
if (!$mysqli->connect_error){
    die ("<p>Connection failed: ". $mysqli->connect_error() ."</p>");
}

在变量中添加一些过滤器

  1. 由于您正在使用$_GET来解码URL,这对于在发布时浏览器对变量进行编码非常重要。
  2. htmlentities将确保没有html标记,并阻止代码运行。

看起来像:

$firstName = urldecode( htmlentities($_GET["fname"]) );
$lastName = urldecode( htmlentities($_GET["lname"]) );
$jobTitle = urldecode( htmlentities($_GET["jobName"]) );

现在让我们看一些重要的变化:

// let's start changing your query here
// use prepared statements
$queryString = "SELECT jobName, description, posType, basePay FROM Titles WHERE jobName = ?";

然后,您必须准备查询

if( $stmt = $mysqli->prepare( $queryString ) ) {
    //bind the parameter of `jobName`
    $stmt->bind_param('s', $jobName);
    // execute the query
    $stmt->execute();

    // get all of the results
    $results = $stmt->get_result();
    // fetch the results in an associative array
    $row = $result->fetch_assoc();

    // close stmt connection
    $stmt->close();
} else {
    $error = array(
        'is_error' => true,
        'error' => 'There was a problem preparing the SQL'
    );

    print_r($error);
}

现在,您将所有记录都放在$row数组中,可以使用它来完成所需的操作。 您可以通过打印整个数组来确保数据在那里:

print_r($row);

干杯!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM