新手:用添加/编辑/搜索/删除脚本创建员工目录.错误


Newbie: Building an employee directory with scripts for add/edit/search/delete. Errors

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

我已经正确地构建了DB和Table。第一个表单成功地向DB添加了记录。在我使搜索和删除表单正确工作之前,我试图让记录显示。他们没有展示。有时我可以显示我的html表,但没有显示任何记录。但是,我知道这些记录存在,因为我可以在MyAdmin中看到它们。

我现在得到这个错误,但我的错误正在改变的时刻,我尝试新的东西:警告:mysql_fetch_array()期望参数1是资源,null在C:'xampp'htdocs' employeesphp在第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);
}

您的mysql查询没有通过,或者它返回0行
试着把这个

if(!$resource){
echo mysql_error();   
echo ' <br>Query: .$sql;
}

$resource = mysql_query($sql)后的语句

,看看它输出什么。此外,请确保错误报告已打开。

我昨天在你的帖子上编辑了我的答案,你可能想看看;它可能会给你一些不同方法的想法。

链接在这里:在C:'xampp'htdocs'Employees.php 101行

中调用未定义函数getRecords()

也许…也许你的代码有问题…

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

我猜你上面的代码应该是这样的

function getRecords()
{
 $sql = buildQuery();
$resource = mysql_query($sql);
$results = array();
if($resource != null)
{
    while($row = mysql_fetch_array($resource)) 
    {
        $results[] = $row;
    }
}
return $results;
}

希望有帮助

请不要这样做:

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

这将产生一个巨大的漏洞,在您的网站安全称为sql注入。你应该经常检查从用户那里获得的任何数据。

简单示例:

$firstname = mysql_real_escape_string($_POST['firstname']);
$lastname = mysql_real_escape_string($_POST['lastname']);
$电话= mysql_real_escape_string ($ _POST['电话']);
$邮件= mysql_real_escape_string ($ _POST['邮件']);
$department = mysql_real_escape_string($_POST['department']);
$position = mysql_real_escape_string($_POST['position']);

mysql_query("INSERT INTO employeeinfo(名,姓,电话,电子邮件,部门,职位)
值(‘{$ firstname}’,‘{$ lastname}’,‘{$电话}’,‘{$邮件}’,‘{$部门}’,‘{$位置}’)");

更多信息请访问:http://php.net/manual/en/mysqli.real-escape-string.php

我认为你应该找一本适合PHP新手的好书,至少时不时地读一下。它会帮助你理解你在写什么,以及如何把它写得更好。

我为此目的搜索的第一本书- http://www.amazon.com/Learning-MySQL-JavaScript-Step-Step/dp/0596157134/