php - 新手 : Building an employee directory with scripts for add/edit/search/delete. 错误

标签 php mysql forms

我正在构建一个包含 3 个简单表单的员工目录。第一次添加记录,第二次搜索记录,第三次搜索然后删除记录。我想在同一页面上显示所有记录,然后在完成搜索时,只显示符合搜索关键字的那些记录。

我已经正确构建了数据库和表。第一种形式成功地将记录添加到数据库。在我使搜索和删除表单正常工作之前,我试图让记录显示。他们没有显示。有时我可以让我的 html 表格显示,但没有任何记录出现。但是,我知道这些记录存在,因为我可以在 MyAdmin 中看到它们。

我现在收到这个错误,但是当我尝试新事物时我的错误正在改变:警告:mysql_fetch_array() 期望参数 1 是资源,在 C:\xampp\htdocs\Employees.php 中给定为 null在第 84 行

我希望得到一些帮助来完成以下工作: 1. 帮我理解为什么会出现这个错误。 2. 帮助我了解如何显示我的记录(我以前成功地做过,但任务更简单)。

我知道这段代码还没有完成。我正在逐个构建它,并尝试在添加下一个之前让每个单独的部分都起作用。谢谢!

<html>
<body>

<?php error_reporting (E_ALL ^ E_NOTICE);
$keyword = $_GET['keyword']; ?>

<?php

$con = mysql_connect("localhost", "employees", "employeepw");
if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }

mysql_select_db("employees", $con);

mysql_query("INSERT INTO employeeinfo (firstname, lastname, phone, email, department, position)
VALUES ('$_POST[firstname]', '$_POST[lastname]', '$_POST[phone]', '$_POST[email]', '$_POST[department]', '$_POST[position]')");

mysql_query($sql,$con);

        function buildQuery() {

        $keyword = $_GET['keyword'];

        $sql = "SELECT * from employeeinfo WHERE
                (
                firstname LIKE '%$keyword%'
                OR
                lastname LIKE '%$keyword%'
                OR
                phone LIKE '%$keyword%'
                OR
                email LIKE '%$keyword%'
                OR
                department LIKE '%$keyword%'
                OR
                position LIKE '%$keyword%'
                )";

        return $sql;

        } ?>

        <form action="Employees.php" method="post">
        <fieldset>
        <legend>Submit Employee Info</legend>
        Firstname: <input type="text" name="firstname" />
        Lastname: <input type="text" name="lastname" />
        Phone: <input type="text" name="phone" />
        Email: <input type="text" name="email" />
        Department: <input type="text" name="department" />
        Position: <input type="text" name="position" />
        <input type=submit name=submit value=Submit />
        </fieldset>
        </form>

        <form action="Employees.php" method=get>
        <fieldset>
        <legend>Search Employee Info</legend>
        <label for="keyword">Enter Keyword</label>
        <input id="keyword" name="keyword" value="<?php echo "$keyword"; ?>" />
        <input type=submit name=submit value=Search />
        </fieldset>
        </form>

        <form action="Employees.php" method=get>
        <fieldset>
        <legend>Delete Employee Info</legend>
        <label for="keyword">Enter Keyword</label>
        <input id="keyword" name="keyword" value="<?php echo "$keyword"; ?>" />
        <input type=submit name=submit value=Delete />
        </fieldset>
        </form>

<?
function getRecords()
{
$sql = buildQuery();

$resource = mysql_query($sql);
}

while($row = mysql_fetch_array($resource)) { // The error is for this row
$results[] = $row;
}
return $results;

$records = getRecords(); {

foreach ($records as $record) {

}?>

        <table>
        <tbody>
        <table border='1'>
        <tr>
        <td><?= $row['firstname']; ?></td>
        <td><?= $row['lastname']; ?></td>
        <td><?= $row['phone']; ?></td>
        <td><?= $row['email']; ?></td>
        <td><?= $row['department']; ?></td>
        <td><?= $row['position']; ?></td>
        <td><a href="Employees.php">Return to Search</a></td>
        </tr>

        <? } ?>

</tbody>
</table>
</body>
</html>

最佳答案

您不会返回任何行。尝试改变这个

function getRecords()
{
$sql = buildQuery();

$resource = mysql_query($sql);
}

function getRecords()
{
$sql = buildQuery();

echo $sql;
exit();

$resource = mysql_query($sql);
}

这将输出您针对数据库查询的 SQL。如果不是很明显,请复制并针对您的数据库运行此查询。查看是否有任何行返回。如果不是,那就是你的问题!


你也可以使用“echo mysql_error();”获取 mysql 抛出的最后一个错误的文本。

示例:

// Perform Query
$result = mysql_query($query);

// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $query;
    die($message);
}

关于php - 新手 : Building an employee directory with scripts for add/edit/search/delete. 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7907228/

相关文章:

PHP PDO If 语句检查删除语句中的字段是否为空

php - 电子邮件转发器无法连接 mySQL 数据库

php - 表单数据向mySQL数据库提交空数据

php - SQL - 根据查询中的结果添加标志 - 最佳实践?

javascript - 即使验证失败,表单也会提交

javascript - 更改输入字段颜色(如果已填充)

javascript - 我在检索由 GZIP 格式的 API 调用接收的数据时遇到问题

php - 如何通过 PHP 将 sql 字段中的扩展数字加载到选择选项元素中?

PHP变量生命周期/作用域

mysql - 如何解释 JMeter 结果和功能