有什么毛病我的AJAX / HTML / PHP code添加到我的SQL数据库? [英] What's wrong with my AJAX/HTML/PHP code to add to my SQL database?

查看:138
本文介绍了有什么毛病我的AJAX / HTML / PHP code添加到我的SQL数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让将信息添加到我的MySQL数据库的形式。为了做到这一点,我有四个剧本:

I'm trying to make a form that adds information to my MySQL database. To do that, I have four scripts:

<html>
<head><title>Insert Data Into MySQL: jQuery + AJAX + PHP</title></head>
<body>

<form id="myForm" action="user_info.php" method="post">
User_id: <input type="text" name="user_id" /><br />
Hash : <input type="text" name="hash" /><br />
<button id="sub">Save</button>
</form>

<span id="result"></span>

<script src="jquery-2.1.1.min.js" type="text/javascript"></script>
<script src="jcode.js" type="text/javascript"></script>
</body>
</html>

这是我的主网页,它提到其他两个文件,​​这是user_info.php和j code.js(忽略了jQuery件)。这是user_info.php

That's my main page, it mentions two other files, which are user_info.php and jcode.js (ignore the jQuery piece). This is user_info.php

<?php
        include_once('db.php');

        $user_id = $_POST['user_id'];
        $hash = $_POST['hash'];

        if(mysqli_query("INSERT INTO _test('user_id, 'hash') VALUES('$user_id', '$hash')"))
          echo "Successfully Inserted";
        else
          echo "Insertion Failed";
?>

它指向db.php的,所以这里的是:

It points to db.php, so here's that:

<?php
$con=mysqli_connect(" **taken out** ","root","password","test");
?> 

最后,这里的歼code.js:

and finally, here's jcode.js:

$("#sub").click( function() {
 $.post( $("#myForm").attr("action"), 
         $("#myForm :input").serializeArray(), 
         function(info){ $("#result").html(info); 
   });
 clearInput();
});

$("#myForm").submit( function() {
  return false; 
});

function clearInput() {
    $("#myForm :input").each( function() {
       $(this).val('');
    });
}

有一段时间,它会说,插入成功。但奇怪的是,它会输入空白数据库中的行。我不知道什么需要改变,但现在它只是说插入失败。我是相当新的这一技术,所以我可能失去了一些东西明显。

For a while, it would say that the insertion was successful. But strangely,it would input blank rows in the database. I don't know what needs to be changed but now it just says "Insertion Failed". I'm fairly new to this technology, so I'm probably missing something obvious.

推荐答案

这将如果打印对我们的回报是有用

It will be useful if you print for us the return of

mysqli_error($con); 

后再行

echo "Insertion Failed";

从现在开始,我能说的是,这不是一个AJAX / jQuery的问题(​​概念,至少) 有关mysqli_error功能的更多信息,可以发现这里

From now, what can I say is that it's not an AJAX/jQuery problem (conceptual, at least) More information about mysqli_error function can be found here.

和请,得到它在生产前消毒用户输入的建议<一href="http://stackoverflow.com/questions/60174/how-can-i-$p$pvent-sql-injection-in-php?rq=1">here.

And please, before getting it in production sanitize the user inputs as suggested here.

这篇关于有什么毛病我的AJAX / HTML / PHP code添加到我的SQL数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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