使用JQuery将值插入MySQL [英] Using JQuery To Insert Values Into mySQL

查看:45
本文介绍了使用JQuery将值插入MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JQuery的新手,正在从事一个项目.这个项目不应该做任何事情,它更多是概念的证明,所以我可以学习.到目前为止,我有3个PHP文件,其中一个叫做"connection.php",它连接到我的本地mySQL DB.另一个名为"save_score.php"的文件会将值保存到我的数据库中.最后,我有一个"test.php"文件,该文件创建了一些值,并使用JQuery将它们发送到"save_score.php",以将其插入到数据库中.

I am new to JQuery and am working on a project. This project isn't supposed to do anything, it's more of a proof of concept so I can learn. SO far I have 3 PHP files, one called 'connection.php' which connects to my local mySQL DB. Another, called 'save_score.php' which would save values to my DB. Last, I have my 'test.php' file which creates a few values and using JQuery sends them to 'save_score.php' to insert them into my DB.

我的问题是'connection.php'正常工作.我可以用PHP硬编码INSERT INTO语句,然后将该查询发送到我的数据库中以将其插入.因此,连接没有任何问题.我还通过两个echo语句测试了JQuery是否实际上将值发送到了'save_score.php',并且echo语句发回了我的测试值:'Jeff'和"10".但是,mySQL INSERT语句没有努力将值发送并查询到我的数据库.

The problem I have is that 'connection.php' works fine. I can hard code an INSERT INTO statement in PHP and send that query to my DB to insert it. So there is nothing wrong with the connection. I also tested, via two echo statements, whether the JQuery was actually sending the values to 'save_score.php', and the echo statements sent back my test values: 'Jeff' and "10'. However, the mySQL INSERT statement does not work to send the values and query to my DB.

因此,从本质上讲,我放入"$ store"中的语句应该可以解决问题,但事实并非如此,我也不知道为什么.

So in essence, the statement I have put into '$store' should do the trick, but it's not and I do not know why.

'save_score.php':

'save_score.php':

<?php
  include 'connection.php';

  $name      = $_POST['name'];
  $score = $_POST['score'];

  echo $name;
  echo $score;

  $store = "INSERT INTO `galleryscores` (`player_name`, `player_score`) VALUES ($name, $score)";

  $mysqli->query($store);

  echo "done";
?>

"connection.php":

'connection.php':

<?php
  $servername = "localhost";
  $username = "root";
  $password = "";
  $dbname = "cs301";

  // Create connection
  $mysqli = new mysqli($servername, $username, $password, $dbname);
  // Check connection
  if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
}

'test.php':

'test.php':

<html>

<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>

  <script type="text/javascript">
    var you = 's';
    var you_score = 10;

    $.post("save_score.php", {
        name: you,
        score: you_score
      })
      .done(function(data) {
        alert("Data Loaded: " + data);
      });
  </script>
</head>

<body>
</body>

</html>

推荐答案

以下是使用 PDO

$dbh = new PDO('mysql:dbname=cs301;host=localhost', $username, $password);

$stmt = $dbh->prepare('INSERT INTO `galleryscores` (`player_name`, `player_score`) VALUES (:player_name, :player_score)');

$stmt->execute( array('player_name' => $_POST['name'], 'player_score' => $_POST['score']) );

这篇关于使用JQuery将值插入MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆