简体   繁体   English

使用ajax从javascript传递数据并将其保存到数据库

[英]passing data from javascript using ajax and saving it to a database

Hi guys I currently have a website that lets users search a website, I would like to save these search terms and the amount of times each has been searched in a database. 嗨,大家好,我目前有一个网站,允许用户搜索网站,我想将这些搜索词以及每次搜索的次数保存在数据库中。 To this end I have created a table with 2 columns search which is varchar(30) and value which is int(30) search is set to unique so that terms for instance design don't populate two rows however when i run the program nothing is being saved to the database i was wondering if any of you could help. 为此,我创建了一个具有两列搜索的表,该表是varchar(30),而将值int(30)搜索设置为唯一,因此实例设计的术语不会填充两行,但是当我运行程序时却什么也没有正在保存到数据库中,我想知道是否有人可以帮助您。

javascript 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

<?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;
}   
?>

any help would be appreciated thanks in advance. 任何帮助将不胜感激,谢谢。

edit 编辑

@PHPglue @PHPglue

im sure you were asking for the page the getjson returns results to if so here is the html page 我确定您要的是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>

EDIT 2 编辑2

Hi everyone I have discovered that the ajax is working and the problem is cause by the ON DUPLICATE KEY UPDATE could any one perhaps check the syntax and tell me if you see any problems. 大家好,我发现ajax在工作,问题是由ON DUPLICATE KEY UPDATE引起的,任何人都可以检查语法并告诉我是否遇到任何问题。 As I said above I would like this value to update to 2 when the same term is searched and so on. 就像我上面说过的,我希望当搜索相同的术语时将该值更新为2,依此类推。

Try amending the sql line to: 尝试将sql行修改为:

$query=$conn->prepare("INSERT INTO favourites 
  VALUES ('$uid','$value') 
  ON DUPLICATE KEY UPDATE value = value + 1");

Hope this helps :) 希望这可以帮助 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM