插入并通过Ajax的使用PHP检索MySQL数据 [英] Inserting and retrieving data in MySQL using PHP through Ajax

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

问题描述

我有一个非常简单的形式,包含一个文本框和一个提交按钮。当用户输入一些东西到表单,然后点击提交,我想使用PHP和Ajax(使用jQuery)插入表单结果到MySQL数据库。这个结果应在其中每个插入后更新的表的形式被显示在同一页上。

任何人都可以请帮助?

在code我用的是不工作:

ajax.html

 < HTML>
<身体GT;
< SCRIPT LANGUAGE =JavaScript的类型=文/ JavaScript的>
<! - 
//浏览器支持code
功能ajaxFunction(){
 VAR ajaxRequest; //让Ajax成为可能的变量!

 尝试{
   //歌剧8.0+,火狐,Safari浏览器
   ajaxRequest =新XMLHtt prequest();
 }赶上(五){
   // Internet Explorer浏览器
   尝试{
      ajaxRequest =新的ActiveXObject(MSXML2.XMLHTTP);
   }赶上(五){
      尝试{
         ajaxRequest =新的ActiveXObject(Microsoft.XMLHTTP);
      }赶上(五){
         // 出了些问题
         警报(您的浏览器打破了!);
         返回false;
      }
   }
 }
 //创建将接收数据的功能
 //从服务器发送和将更新
 //格在同一页面节。
 ajaxRequest.onreadystatechange =功能(){
   如果(ajaxRequest.readyState == 4){
      变种ajaxDisplay =的document.getElementById('ajaxDiv');
      ajaxDisplay.value = ajaxRequest.responseText;
   }
 }
 //现在得到用户的价值,并把它传递给
 //服务器脚本。
 。VAR名称=的document.getElementById('名')值;
VAR年龄=的document.getElementById('年龄')值。
 。VAR WPM =的document.getElementById('WPM')值;
 。VAR性=的document.getElementById('性')值;
 VAR查询字符串=&放大器;名称=+姓名+&放大器;年龄=+年龄;
 查询字符串+ =&放大器; WPM =+ WPM +&放大器;性别=+性;
 ajaxRequest.open(GET,Ajax的使用example.php+
                              查询字符串,真实);
 ajaxRequest.send(空);
}
//  - >
< / SCRIPT>
<表格名称=myForm的'>
名称:<输入类型='文本'ID ='名称'/>< BR />
最大年龄:其中;输入类型='文本'ID ='年龄'/> < BR />
最大WPM:LT;输入类型='文本'ID ='WPM'/>
< BR />
性别:<选择一个id ='性'>
<期权价值=M> M< /选项>
<期权价值=F> F< /选项>
< /选择>
<输入类型=按钮的onclick ='ajaxFunction()'
                              值='查询MySQL的/>
< /形式GT;
< D​​IV ID ='ajaxDiv'>您的结果将显示此处< / DIV>
< /身体GT;
< / HTML>
 

AJAX-使用example.php

 < PHP
$ DBHOST =localhost的;
$ DBUSER =演示;
$ DBPASS =演示;
$ DBNAME =TEST_DB;
    //连接到MySQL服务器
的mysql_connect($ DBHOST,$ DBUSER,$ DBPASS);
    //选择数据库
mysql_select_db($ TEST_DB)或死亡(mysql_error());
    //从查询字符串数据
$年龄= $ _GET ['年龄'];
$性爱= $ _GET ['性'];
$ WPM = $ _GET ['WPM'];
    //逃逸用户输入,以帮助prevent SQL注入
$年龄= mysql_real_escape_string($岁);
$性爱= mysql_real_escape_string($性);
$ WPM = mysql_real_escape_string($ WPM);
    //生成查询
$查询=INSERT INTO窗口2(姓名,年龄,性别,WPM)VALUES('$名字','$时代,$性','$ WPM');;


mysql_select_db('TEST_DB');

$ RETVAL =的mysql_query($的SQL,$康恩);
如果(!$ RETVAL)
{
  死亡('不能进入数据:'m​​ysql_error());
}

    //生成结果字符串
$ display_string =<表>;
$ display_string =&其中; TR>中;
$ display_string =<第i个姓名和LT; /第i个;
$ display_string =<第i个年龄< /第i个;
$ display_string =<第i个性别和LT; /第i个;
$ display_string =&其中;第i个的WPM&所述; /第i个;
$ display_string =。&所述; / TR>中;

//插入新行的表中返回的每个人
$ RESULT1 =请求mysql_query(SELECT * FROM窗口2,其中name ='$名字');
而($行= mysql_fetch_array($ RESULT1))
{
    $ display_string =&其中; TR>中;
    $ display_string =< TD> $行[名]< / TD>中;
    $ display_string =< TD> $行[年龄] LT; / TD>中;
    $ display_string =< TD> $行[性]< / TD>中;
    $ display_string =< TD> $行[WPM] LT; / TD>中;
    $ display_string =。&所述; / TR>中;

}

$ display_string =< /表>;
回声$ display_string;
?>
 

解决方案

  $(button_id)。点击(函数(){
    $阿贾克斯({
        网址:你应该张贴数据,
        键入:POST,
        数据:你应该张贴的字符串,
        成功:函数(结果){
            //在一些DOM元素显示你的结果
        }
    });
});
 

当您收到的PHP脚本创建查询的数据到数据库中,并得到你的结果。

希望这将有助于

I have a very simple form, containing a textbox and a submit button. When the user enters something into the form, then clicks submit, I would like to use PHP and Ajax (with jQuery) to insert the result of the form into a MySQL database. this result should be displayed on the same page in the form of a table which is updated after every insert.

Can anyone please help?

The code I have used that isn’t working:

ajax.html:

<html>
<body>
<script language="javascript" type="text/javascript">
<!-- 
//Browser Support Code
function ajaxFunction(){
 var ajaxRequest;  // The variable that makes Ajax possible!

 try{
   // Opera 8.0+, Firefox, Safari
   ajaxRequest = new XMLHttpRequest();
 }catch (e){
   // Internet Explorer Browsers
   try{
      ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
   }catch (e) {
      try{
         ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
      }catch (e){
         // Something went wrong
         alert("Your browser broke!");
         return false;
      }
   }
 }
 // Create a function that will receive data 
 // sent from the server and will update
 // div section in the same page.
 ajaxRequest.onreadystatechange = function(){
   if(ajaxRequest.readyState == 4){
      var ajaxDisplay = document.getElementById('ajaxDiv');
      ajaxDisplay.value = ajaxRequest.responseText;
   }
 }
 // Now get the value from user and pass it to
 // server script.
 var name = document.getElementById('name').value;
var age = document.getElementById('age').value;
 var wpm = document.getElementById('wpm').value;
 var sex = document.getElementById('sex').value;
 var queryString = "&name=" +name+ "&age=" + age ;
 queryString +=  "&wpm=" + wpm + "&sex=" + sex;
 ajaxRequest.open("GET", "ajax-example.php" + 
                              queryString, true);
 ajaxRequest.send(null); 
}
//-->
</script>
<form name='myForm'>
Name: <input type='text' id='name' /><br/>
Max Age: <input type='text' id='age' /> <br />
Max WPM: <input type='text' id='wpm' />
<br />
Sex: <select id='sex'>
<option value="m">m</option>
<option value="f">f</option>
</select>
<input type='button' onclick='ajaxFunction()' 
                              value='Query MySQL'/>
</form>
<div id='ajaxDiv'>Your result will display here</div>
</body>
</html>

ajax-example.php:

<?php
$dbhost = "localhost";
$dbuser = "demo";
$dbpass = "demo";
$dbname = "test_db";
    //Connect to MySQL Server
mysql_connect($dbhost, $dbuser, $dbpass);
    //Select Database
mysql_select_db($test_db) or die(mysql_error());
    // Retrieve data from Query String
$age = $_GET['age'];
$sex = $_GET['sex'];
$wpm = $_GET['wpm'];
    // Escape User Input to help prevent SQL Injection
$age = mysql_real_escape_string($age);
$sex = mysql_real_escape_string($sex);
$wpm = mysql_real_escape_string($wpm);
    //build query
$query = "INSERT INTO form2 (name,age,sex,wpm) VALUES ('$name','$age','$sex','$wpm')";;


mysql_select_db('test_db');

$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not enter data: ' . mysql_error());
}

    //Build Result String
$display_string = "<table>";
$display_string .= "<tr>";
$display_string .= "<th>Name</th>";
$display_string .= "<th>Age</th>";
$display_string .= "<th>Sex</th>";
$display_string .= "<th>WPM</th>";
$display_string .= "</tr>";

// Insert a new row in the table for each person returned
$result1=mysql_query("SELECT * FROM form2 WHERE name='$name'"); 
while($row = mysql_fetch_array($result1))
{
    $display_string .= "<tr>";
    $display_string .= "<td>$row[name]</td>";
    $display_string .= "<td>$row[age]</td>";
    $display_string .= "<td>$row[sex]</td>";
    $display_string .= "<td>$row[wpm]</td>";
    $display_string .= "</tr>";

}

$display_string .= "</table>";
echo $display_string;
?>

解决方案

$("button_id").click(function () {
    $.ajax({
        url:"where you should post the data",
        type: "POST",  
        data: the string you should post,  
        success: function (result) {
            //display your result in some DOM element
        }
    });
});

When you receive the data in the php script make query to the database and get your result

hope this would help

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

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