用jQuery和PHP插入MySQL数据库 [英] Insert into MySQL database with jQuery and PHP

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

问题描述

我在这里遇到了一些问题,我不知道这段代码有什么问题。我很确定它是客户端,因为我没有得到任何PHP错误,但我可能是错的。

I'm experiencing some kind of a problem here, I have no idea what's wrong with this code. I'm pretty sure it's client sided since I am not getting any PHP errors, but I may be wrong.

我试图获取表单,一旦提交,通过对PHP文件的AJAX请求将包含在文本字段中的信息插入到MySQL数据库中。

I'm trying to get a form, once submitted, to insert the information contained in a text field into a MySQL database via an AJAX request to a PHP file.

以下是我的位置:

/* index.php */

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="scripts/ajax.js"></script>


//...


        <div class="success" id="success"></div>
        <div class="err" id="err"></div>


 <!--Create Form-->
 <form action="" method="post" name="create" id="createForm" onsubmit="createNew(document.create.create2.value); return false;">        
        <h5>Create New File</h5>
        <p><input name="create2" type="text" maxlength="32" /></p>
 <input type="submit" name="submit" value="Create" />
 </form>


/* ajax.js */

function createNew(name)
{
 $('#loading').css('visibility','visible');

  $.ajax({
  type: "POST",
  url: "../utilities/process.php",
  data: 'name='+name,
  datatype: "html",
  success: function(msg){

   if(parseInt(msg)!=5)
   {
    $('#success').html('Successfully added ' + name + ' into database.');
    $('#loading').css('visibility','hidden');
    alert('success');//testing purposes
   }
   else
   {
    $('#err').html('Failed to add ' + name + ' into database.');
    $('#loading').css('visibility','hidden');
    alert('fail');//testing purposes
   }
  }
      })
}


/* process.php */

<?
include('db_connect.php');

if($_POST['name'])
{
 $name = $_POST['name'];
 $name = mysql_escape_string($name);
 $query = "INSERT INTO tz_navbar (name) VALUES (".$name.")";
 $result = mysql_query("$query") or die ("5");

}
?>

这是我的问题:
在我提交表单后,它报告成功但没有东西会被添加到我的数据库中。

Here's my problem: After I submit my form with something, it reports that is succeeds but nothing gets added to my database.

非常感谢大家花时间帮助我。

Thank you all in advance for taking your time to help me.

推荐答案

查看您的查询,我怀疑您需要它:

Looking at your query, I suspect you need it to be:

$query = "INSERT INTO tz_navbar (name) VALUES ('".$name."')";

如果这样不能解决问题,您需要记录$ _REQUEST的值

If that doesn't fix it, you need to log the value of $_REQUEST

error_log(print_r($_REQUEST,true));

确保您在服务器端获得正确的值。

to ensure that you are getting the right values on the server side.

这篇关于用jQuery和PHP插入MySQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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