大家好,我目前有一个网站,允许用户搜索网站,我想保存这些搜索条件和每个搜索在数据库中的次数。为此,我创建了一个包含2列搜索的表,即varchar(30),将int(30)搜索的值设置为unique,以便实例设计的术语不会填充两行,但是当我运行时程序没有任何东西被保存到数据库我想知道你是否有人可以提供帮助。
的javascript
function performSearch() {
var searchURL = "http://api.lmiforall.org.uk/api/v1/soc/search?q=";
var searchTerms = $('#searchterm').val();
var searchReady = searchTerms.toLowerCase();
$.ajax({
type: "POST",
url: 'findfavourites.php',
data: {searchReady : searchReady},
success: function(data)
{
alert("success!");
}
});
$('#soctable tbody').empty();
$.getJSON(searchURL + searchReady, function(results) {
results.forEach(function (result) {
var row = $("<tr></tr>");
var codeCell = $("<td></td>");
var titleCell = $("<td></td>");
var descriptionCell = $("<td></td>");
var qualificationsCell = $("<td></td>");
var tasksCell = $("<td></td>");
codeCell.html(result.soc);
titleCell.html(result.title);
descriptionCell.html(result.description);
qualificationsCell.html(result.qualifications);
tasksCell.html(result.tasks);
row.append(codeCell);
row.append(titleCell);
row.append(descriptionCell);
row.append(qualificationsCell);
row.append(tasksCell);
$('#soctable tbody').append(row);
});
});
}
$(function() {
// when the page is loaded
$('#dosearch').on('click', performSearch);
});
PHP
<?php
include 'includes/dbConnection.php';
if(isset($_POST['searchReady']))
{
$uid = $_POST['searchReady'];
$value='1';
$query=$conn->prepare("INSERT INTO favourites VALUES ('$uid','$value') ON DUPLICATE KEY UPDATE value = VALUES(value + 1");
$query->execute();
$conn = null;
}
?>
提前感谢任何帮助。
修改的
@PHPglue
我确定你要求getjson返回结果的页面如果是这样,这里是html页面
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, intial-scale=1">
<title>Homepage</title>
<link href="styleSheet.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="jquery-3.1.1.js"></script>
<script type="text/javascript" src="soc.js"></script>
</head>
<body>
<div id="page">
<nav>
<ul>
<li><a href="index.html">Home Page</a></li>
<li><a href="careers.html">Careers</a>
<ul>
<li><a href="#">Careers and Apprenticeships</a></li>
</ul>
</li>
</ul>
</nav>
<header>
<h1><u>Careers and Apprenticeships</u></h1>
</header>
<div class="container">
<div class="row">
<div class="col-sm-12">
<input class="form control" type="text" id="searchterm" placeholder="Put your search term here...">
<button class="btn btn-primary" id="dosearch">Search!</button>
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-sm-12">
<table class="table table-striped" id="soctable">
<thead>
<tr>
<th>SOC Code</th>
<th>Job Title</th>
<th>Description</th>
<th>Qualifications</th>
<th>Tasks</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
</div>
</div>
<footer>
</footer>
</body>
</html>
编辑2
大家好我发现ajax正在工作,问题是由ON DUPLICATE KEY UPDATE造成的,任何人都可以检查语法并告诉我你是否看到任何问题。正如我上面所说,我希望在搜索相同的术语时将此值更新为2,等等。
答案 0 :(得分:0)
尝试将sql行修改为:
$query=$conn->prepare("INSERT INTO favourites
VALUES ('$uid','$value')
ON DUPLICATE KEY UPDATE value = value + 1");
希望这会有所帮助:)