我正在尝试创建一个表单,这样我就可以在数据库中搜索特定的标题,但是每当我搜索没有发生任何事情时,我都不确定如何连接到数据库。这是该应用程序的链接:https://web-seanakiyama.c9users.io/Week%2011/Index3.php。有人能帮我解决这个问题吗,谢谢!
<!DOCTYPE html>
require_once “week11.php”;
$search = "TITLE";
getGames($search);
<?php
$data = [
["TITLE" => "Counterstrike: Global Offensive", "DEVELOPER" => "Valve", "PUBLISHER" => "Valve", "PRICE" => 19.99, "PLATFORM" => "PC"],
["TITLE" => "Final Fantasy XV", "DEVELOPER" => "Square Enix", "PUBLISHER" => "Square Enix", "PRICE" => 99.99, "PLATFORM" => "PS4"],
["TITLE" => "Halo", "DEVELOPER" => "Bungie", "PUBLISHER" => "Microsoft", "PRICE" => 49.99, "PLATFORM" => "Xbox"],
["TITLE" => "Battlefield 4", "DEVELOPER" => "EA Digital Illusions", "PUBLISHER" => "EA", "PRICE" => 79.99, "PLATFORM" => "PC"]
]
?>
<html>
<head>
<link type='text/css' rel='stylesheet' href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"/>
</head>
<body>
<div>
<div class="jumbotron">
<div class="container">
<h1>Games</h1>
<div class="container">
<div id="Search"</div>
<form>
<form class="form-inline">
<form action="index.php" method="get">
<fieldset>
<label>Title</label>
<input type="text" class="form-control" id="title" name="title"/>
<input type="submit" value="Search" class="btn btn-default"/>
</fieldset>
</form>
<div class="container">
<div id="data"</div>
<table>
<table class="table">
<thead>
<th>Game Title</th>
<th>Developer</th>
<th>Publisher</th>
<th>Price</th>
<th>Platform</th>
</thead>
<?php
foreach($data as $data)
{
echo"<tr>";
echo"<td>".$data["TITLE"]."</td>";
echo"<td>".$data["DEVELOPER"]."</td>";
echo"<td>".$data["PUBLISHER"]."</td>";
echo"<td>".$data["PRICE"]."</td>";
echo"<td>".$data["PLATFORM"]."</td>";
echo"</tr>";
}
?>
</table>
</div>
</body>
</html>发布于 2016-05-29 13:13:12
我只是写了一个简单的代码,其中有一个输入命名为标题的表单,在提交此表单将查看表格,并显示匹配的搜索,如果在数据库中找到匹配的东西,希望它会给你一点关于表或数据库中搜索的想法。
因此,首先我在searchform.html文件中创建一个表单:
<!DOCTYPE html>
<head>
<--include your bootstrap here-->
</head>
<body>
<form name='search' class='form-horizontal' method='post' action='search.php'>
<div class='form-group'>
<div class='col-sm-2'>
<label for='title' class = 'control-label'>enter title to search</label>
</div>
<div class='col-sm-8'>
<input type='text' name='title' id='title'>
</div>
</div>
<input type='submit' name='search' class='btn btn-info' value='search'>`
</form>
</body>
</html>之后,我们将在phpmyadmin中创建一个名为'testsearch‘的数据库,然后创建一个名为search的表,该表将有两列'id’和' title‘,其中i将是整数,title将是varchar。
现在,我们将创建一个名为search.php的新文件,该文件将在表单提交时运行,我们将在其中添加下面编写的代码。
<?php
//get the posted values
$submit = $_POST['search'];
$title = $_POST['title'];
//if submitted then rurun the code
if ($submit) {
//create a database connection
mysql_connect('localhost', 'root', ' ');
mysql_select_db('testsearch');
$query = "SELECT * FROM search WHERE 'title' = $title ";
$result = mysql_query($query);
if(mysql_num_rows($result) > 0){
echo 'search result is';
foreach (mysql_fetch_array($result) as $row) {
echo $row['title']; //print result
}
} else {
echo 'no match found';
}
}注意:也不要忘记在数据库中插入值。要检查它,请在表搜索中插入一些值,然后在浏览器中运行表单文件,并输入与您在表中插入的标题相同的标题,它将显示相应的结果。
发布于 2016-05-29 13:30:39
require_once “week11.php”;
$search = "TITLE";
getGames($search);
<?phpPHP标签应该在顶部,因为您正在使用php标签包含文件
<?php
require_once “week11.php”;
$search = "TITLE";
getGames($search);https://stackoverflow.com/questions/37506020
复制相似问题