2017-08-09 120 views
0

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

我的问题是'connection.php'正常工作。我可以在PHP中对INSERT INTO语句进行硬编码,并将该查询发送给我的数据库以插入它。所以连接没有问题。我还通过两个echo语句测试了JQuery实际上是否将值发送到'save_score.php',并且echo语句发回了我的测试值'Jeff'和'10'。但是,mySQL INSERT语句没有工作的价值和查询发送到我的数据库。

所以在本质上,我已经投入“$店”应该做的伎俩的声明,但它不是,我不知道为什么。

“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':

<?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的”:

<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> 
+0

您是否在使用变量推断出SQL仍然正常工作后回显'$ store'? – Matt

+0

我刚刚做了,它让我回到正确的查询:https://gyazo.com/a22aeb1dbe5e9cdfa5b16a88ecc9a6c2 –

+1

SQL是错误的,这就是为什么它没有更新,你没有错误处理看到它。你需要用单引号引用你的字符串值(S)。一旦你已经开始研究mysqli或PDO,并准备好发言,因为你是开放的攻击。 – Matt

回答

0

下面是如何应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'])); 
0

试着改变你的插入查询到该

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

为了要插入的价值,他们应该为字符串查询传递。最终,你传递一个字符串来执行。

EDITED 正如马特所说的,你应该总是试着去阻止SQL注入。下面说明如何做到这一点:

$query = $mysqli->prepare("INSERT INTO `galleryscores` (`player_name`, `player_score`) VALUES (?, ?)"); 
$query->bind_param("si", $name, $score);// 's' represents the corresponding variable type is String 
$query->excecute(); 

bind_param documentation

+0

足以解决问题,但始终尝试指出用户使用Mysqli/PDO进行准备语句。打开SQL注入! – Matt

+0

@Matt你是什么意思? –

+0

@Matt当然可以。 – Aman

0

做虽然我们是在它。如何使用MySQLi完成:D - 具有错误处理功能

$mysqli = new mysqli($host, $user, $pass, $db); 

if($mysqli->connect_error) 
    die('Connect Error (' . $mysqli->connect_errno . ') '. $mysqli->connect_error); 

if ($stmt = $mysqli->prepare("INSERT INTO `galleryscores` (`player_name`, `player_score`) VALUES (?, ?)")) 
{ 
    // Bind the values in order to the ?'s or log error 
    // Assuming Name is a string and score is always an int 
    if(!$stmt->bind_param("si", $name, $score)) 
    { 
     die('bind_param() failed: ' . htmlspecialchars($stmt->error)); 
    } 

    // Execute the completed sql command or log error 
    if (!$stmt->execute()) 
    { 
     die('execute() failed: ' . htmlspecialchars($stmt->error)); 
    } 

    $stmt->close(); // Close the database connection 
} 
else 
{ 
    die('prepare() failed: ' . htmlspecialchars($mysqli->error)); 
} 
+0

你是最棒的!我会回来看看这个改进我的代码! –