您好我正在尝试创建一个搜索数据库表并显示在表上的搜索表单。共有3个部分。 index.php 2. process_studennt.php 3. Database.php
从表单中将转到process_studennt.php,然后转到Database.php,之后它将返回并在表中提供结果。
但我遇到了问题。请帮忙。
警告是:
注意:未定义的索引:valuetosearch in 第46行的H:\ install \ xampp \ htdocs \ crud \ index.php
警告:为foreach()提供的参数无效 第274行的H:\ install \ xampp \ htdocs \ crud \ lib \ Database.php
致命错误:未捕获PDOException:SQLSTATE [42000]:语法错误或 访问冲突:1064您的SQL语法有错误;检查 与您的MariaDB服务器版本对应的手册 要在' FROM tbl_student'附近使用的语法在第1行 H:\ install \ xampp \ htdocs \ crud \ lib \ Database.php:280堆栈跟踪:#0 H:\安装\ XAMPP \ htdocs中\污物\ lib中\ database.php中(280): PDOStatement-> execute()#1 H:\ install \ xampp \ htdocs \ crud \ index.php(88): 数据库 - >搜索(' tbl_student',数组)#2 {main}抛出
我在index.php页面中的表单是:
<form class="form form-control" action="lib/process_student.php" method="POST" > <fieldset class="form-group"> <input type="text" class="form-control-sm" name="valuetosearch" placeholder="Search.."> <input type="hidden" name="action" value="search"> <input type="submit" class="btn btn-primary" id="submit" name="search" Value="Search"> </fieldset>
和显示结果的表格是:
> /*
> search section
>
> */
>
>
> $searchdata = $db-> search($tablename,$selectcon);
> if (!empty($searchdata)) {
> ?>
> <tr>
> <td><?php echo $searchdata["id"];; ?></td>
> <td><?php echo $searchdata["name"]; ?></td>
> <td><?php echo $searchdata["email"]; ?></td>
> <td><?php echo $searchdata["phone"]; ?></td>
> <td>
> <a href="editstudent.php?id=<?php echo $searchdata["id"]; ?>" class="btn btn-secondary">Edit</a>
> <a href="lib/process_student.php?action=delete&id=<?php echo $searchdata["id"]; ?>" class="btn btn-danger">Delete</a>
> </td>
> </tr>
> <?php
> } else {
> # code...
> }
>
>
> ?>
process_student.php文件部分:
// search section
elseif ($_REQUEST["action"] == "search" && !empty($_REQUEST["action"])) {
$searchvalue = $_POST["valuetosearch"];
if (!empty($searchvalue)) {
$tablename = "tbl_student";
$condition = ["name" => $searchvalue];
$searchdata = $db->search($tablename,$condition);
if ($searchdata) {
$msg = "Data founded";
} else {
$msg = "Error! No data founded";
}
/*
redirecting to the index page
*/
Session::set("msg",$msg);
$home_url = "../index.php";
header("Location: ".$home_url);
} else {
# code...
}
和Database.php部分:
public function search($tablename,$data)
{
$sql = "SELECT ";
$sql .= array_key_exists("select", $data)?$data["select"]:"*";
$sql .= " FROM ".$tablename;
if (array_key_exists("where", $data)) {
$sql .= " WHERE ";
$i = 0;
foreach ($data["where"] as $key => $value) {
$add = ($i > 0)?" OR " : "";
$sql .= "$add"."$key=:$key";
$i++;
}
}
// now prepare and execute
$query = $this->pdo->prepare($sql);
// bind value
if (array_key_exists("select", $data)) {
foreach ($data["select"] as $key => $value) {
$query->bindValue(":$key","$value");
}
}
// now execute
$query->execute();
// now for the return type
if (array_key_exists("return_type", $data)) {
switch ($data["return_type"]) {
case 'count':
$value = $query->rowCount();
break;
case 'single':
$value = $query->fetch(PDO::FETCH_ASSOC);
break;
default:
$value = "";
break;
}
}
elseif ($query->rowCount() > 0) {
$value = $query->fetchAll();
}
// finaly return
return !empty($value)?$value:false;
}
有什么问题请帮帮我。 谢谢。