php代码有问题,可以运行,但无法执行

时间:2019-06-03 05:06:30

标签: php

搜索过程正在影响过程的种类。 如果我单独测试每个,它们都可以工作。 我很确定它与mysqli_fetch_array($ results)有关 我只是找不到我要去的地方。 任何帮助将不胜感激。

服务器:测试表:测试代码 id,FirstName,LastName

index.php


<?php
include('new.php');
?>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Test Code</title>
<style>
table,tr,th,td
{
border: 1px solid black;
}
</style>
</head>
<body>
<div class="input-sbi">
<form action="index.php" method="post">
<input type="text" name="valueToSearch"  placeholder="Value To Search"> 
<br><br>
<input type="submit" name="search"  value="Search">
</div>     
<form method="post" action="index.php" >
<div class="input-group">
<div id="content1">     
<label>FirstName</label>
<input type="text" name="FirstName" value="<?php echo $FirstName; ?>">
<label>LastName</label>
<input type="text" name="LastName" value="<?php echo $LastName; ?>"> 
</select><br><br> 
<input type="hidden" name="id" value="<?php echo $id; ?>" >
</div>
</div>  
<table> 
<thead>
<tr>
<th><a href="?orderby=id&order=<?php echo $idOrder; ?>">id</a></th>
<th><a href="?orderby=FirstName&order=<?php echo $FirstNameOrder; ? 
>">FirstName</a></th>
<th><a href="?orderby=LastName&order=<?php echo $LastNameOrder; ? 
>">LastName</a></th>
</tr>
</thead>
<?php while ($row = mysqli_fetch_array($results)): ?>
<tr>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['FirstName']; ?></td>
<td><?php echo $row['LastName']; ?></td>
<td>
<a href="index.php?edit=<?php echo $row['id'];? 
>"class="edit_btn">Edit</a>
</td>
<td>
<a href="index.php?del=<?php echo $row['id'];? 
>"class="del_btn">Delete</a>
</td>
</tr>
<?php endwhile; ?>
</table>  
</form>
</body>                
</html>


<?php
session_start();
$db = mysqli_connect('localhost', 'root', '', 'test');
$FirstName = "";
$LastName = "";
$id = 0;
if (isset($_GET['edit'])) {
$id = $_GET['edit'];
$db=null;
$db = mysqli_connect('localhost', 'root', '', 'test') or die("Not 
connected.");
$record = mysqli_query($db, "SELECT * FROM testcode WHERE id=$id");
if (@count($record) == 1 ) {
$n = mysqli_fetch_array($record);
$FirstName = $n['FirstName'];
$LastName = $n['LastName'];
}
}
// Ascending Decending Order
$db = new mysqli("localhost", "root", "", "test");
$orderBy = !empty($_GET["orderby"]) ? $_GET["orderby"] : "id";
$order = !empty($_GET["order"]) ? $_GET["order"] : "asc";
$sql = "SELECT * FROM testcode ORDER BY " . $orderBy . " " . $order;
$result = $db->query($sql);
$idOrder = "asc";
$FirstNameOrder = "asc";
$LastNameOrder = "asc";
if($orderBy == "id" && $order == "asc") {
$idOrder = "desc";
}
if($orderBy == "FirstName" && $order == "asc") {
$FirstNameOrder = "desc";
}
if($orderBy == "LastName" && $order == "asc") {
$LastNameOrder = "desc";
}
if(isset($_POST['search']))
{
$valueToSearch = $_POST['valueToSearch'];
$query = "SELECT * FROM `testcode` WHERE CONCAT(`id`, `FirstName`, 
`LastName`) LIKE '%".$valueToSearch."%'";
$results = filterTable($query);
}
else {
$query = "SELECT * FROM `testcode`";
$results = filterTable($query);
}
function filterTable($query)
{
$connect = mysqli_connect("localhost", "root", "", "test");
$filter_Result = mysqli_query($connect, $query);
return $filter_Result;
}
?>      

1 个答案:

答案 0 :(得分:1)

您需要按如下所述更改PHP代码。此外,您错过了一些标签并添加了未使用的标签,我添加了一些注释,以供您理解:

<?php
session_start();

include('new.php');

$db = mysqli_connect('localhost', 'root', '', 'test');
$FirstName = "";
$LastName = "";
$id = 0;
if (isset($_GET['edit'])) {
    $id = $_GET['edit'];

    // --- Unnecessary db connection start ---
    // $db=null;
    // $db = mysqli_connect('localhost', 'root', '', 'test') or die("Not connected.");      
    // --- Unnecessary db connection start ---

    $record = mysqli_query($db, "SELECT * FROM testcode WHERE id=$id");

    if (@count($record) == 1 ) {
        $n = mysqli_fetch_array($record);
        $FirstName = $n['FirstName'];
        $LastName = $n['LastName'];
    }
}
// Ascending Decending Order 

// --- Unnecessary db connection start ---
// $db = new mysqli("localhost", "root", "", "test");   
// --- Unnecessary db connection end ---

$orderBy = !empty($_GET["orderby"]) ? $_GET["orderby"] : "id";
$order = !empty($_GET["order"]) ? $_GET["order"] : "asc";

// --- Unnecessary query start ---
// $sql = "SELECT * FROM testcode ORDER BY " . $orderBy . " " . $order;
// $result = $db->query($sql);
// --- Unnecessary query end ---

$idOrder = "asc";
$FirstNameOrder = "asc";
$LastNameOrder = "asc";

if($orderBy == "id" && $order == "asc") {
    $idOrder = "desc";
}
if($orderBy == "FirstName" && $order == "asc") {
    $FirstNameOrder = "desc";
}
if($orderBy == "LastName" && $order == "asc") {
    $LastNameOrder = "desc";
}
if(isset($_POST['search'])) {
    $valueToSearch = $_POST['valueToSearch'];

    // Add order by clause at the end of query to apply both Search and Order By functionality
    $query = "SELECT * FROM `testcode` WHERE CONCAT(`id`, `FirstName`, `LastName`) LIKE '%".$valueToSearch."%' ORDER BY " . $orderBy . " " . $order;

    // Pass new paramter called $connect, for not initializing db connection again
    $results = filterTable($query, $db);
}
else {
    // Add order by clause at the end of query to apply only Order By functionality
    $query = "SELECT * FROM `testcode` ORDER BY " . $orderBy . " " . $order;

    // Pass new paramter called $connect, for not initializing db connection again
    $results = filterTable($query, $db);
}

// Add new paramter called $connect, for not initializing db connection again
function filterTable($query, $connect) {        
    // --- Unnecessary query start ---
    // $connect = mysqli_connect("localhost", "root", "", "test");
    // --- Unnecessary query start ---

    $filter_Result = mysqli_query($connect, $query);
    return $filter_Result;
}
?>

执行以上更改,然后尝试。希望对您有所帮助!