通过URL传递值并使用该值选择SQL行

时间:2017-01-03 20:40:15

标签: php sql mysqli

我有这段代码:

 <a href="printable.php?job_numb=<?=$job_numb;?>"
target="new" style="color: #666 !important">
<p><?php echo $job_numb;?></p></a>

$job_numb变量传递给URL页面。该页面选择$job_numb并执行此操作:

 <?php
    if( $_SERVER['REQUEST_METHOD']=='GET' && isset( $_GET['job_numb'] ) ){
    $job_numb = filter_input( INPUT_GET, 'job_numb', FILTER_SANITIZE_STRING );
    }
 $servername = "localhost";
 $username = "xxxxx";
 $password = "xxxxx";
 $dbname = "jobs_users";

 $conn = mysqli_connect($servername, $username, $password, $dbname);

 if (!$conn) {
   die("Connection failed: " . mysqli_connect_error());
 }
 $sql = "SELECT * FROM jobs_canjobs WHERE job_numb = $job_numb";
     $job_name = $_GET['job_name'];
     $comments = $_GET['comments'];
     $due_date = $_GET['due_date'];
     $attachment1 = $_GET['attachment1'];
     $requestor = $_GET['requestor'];
     $req_email = $_GET['req_email'];
     $Property = $_GET['Property'];
     $assignee = $_GET['assignee'];
     $assign_email = $_GET['assign_email'];
     $AE = $_GET['AE'];
 $results = mysqli_query($conn, $sql);
 ?>

然后应该检查$ job_numb = job_numb然后加载这些变量。但是,它没有显示任何内容。

我拥有通过<a href的所有变量,但随后特殊字符会破坏安全性并提供406 error。我相信这可能是一个更安全的选择,但我没有做对。

我错过了一步吗?谢谢。

2 个答案:

答案 0 :(得分:2)

我猜你正试图做这样的事情:

$sql = "SELECT * FROM jobs_canjobs WHERE job_numb = $job_numb";
$results = mysqli_query($conn, $sql);
if ($row = mysqli_fetch_array($results)){
     $job_name = $row['job_name'];
     $comments = $row['comments'];
     $due_date = $row['due_date'];
     $attachment1 = $row['attachment1'];
     $requestor = $row['requestor'];
     $req_email = $row['req_email'];
     $Property = $row['Property'];
     $assignee = $row['assignee'];
     $assign_email = $row['assign_email'];
     $AE = $row['AE'];
}else{
     echo 'no records found';
}

答案 1 :(得分:1)

来自http://php.net/manual/en/mysqli.query.php

“返回值¶

失败时返回FALSE。对于成功的SELECT,SHOW,DESCRIBE或EXPLAIN查询,mysqli_query()将返回一个mysqli_result对象。对于其他成功的查询,mysqli_query()将返回TRUE。“

所以你需要在结果上使用mysql_fetch_array()或类似的东西来获取数据