Home  >  Article  >  Backend Development  >  用ajax怎么提交插入数据库?

用ajax怎么提交插入数据库?

WBOY
WBOYOriginal
2016-06-20 12:35:34844browse

我昨天只是试着用ajax做了一个验证用户名的效果 用的是数据的查询  但是我做了一个提交插入数据库的页面    我还是用的查询的那一套办法做的 试了一下不成功 插不到数据库里面  我这种做法是不是不对呢  
这是数据库


这是前端页面

<!DOCTYPE html><html><head>    <meta charset="utf-8">	<title></title>	<script type="text/javascript">		function ajax(url,funsucc){            var oAjax=new XMLHttpRequest();            oAjax.open('GET',url,true);            oAjax.send();                oAjax.onreadystatechange=function(){              if(oAjax.readyState==4){                if(oAjax.status==200){                  funsucc(oAjax.responseText);                }           }}}	</script>	<script type="text/javascript">		window.onload=function(){			var oTxt=document.getElementById('txt1');			var oBtn=document.getElementById('btn1');			oBtn.onclick=function(){                ajax("ajaxinsert.php?id="+oTxt.value);  //不知道这里怎么写  只写了一个url			}		}	</script></head><body><form>	<input type="text" id="txt1">    <button type="submit" id="btn1">提交</button></form></body></html>


后台(ajaxinsert.php)
$pdo=new PDO("mysql:host=localhost;dbname=t1","root","");
$id=$_GET["id"];
$stmt=$pdo->prepare("insert into ajax(txt)values(:txt)");
$stmt->execute(array($id));
?>


回复讨论(解决方案)

$stmt=$pdo->prepare("insert into ajax (txt) values (:txt)");
$stmt->execute(array(‘:txt' =>$id));

$stmt=$pdo->prepare("insert into ajax (txt) values (?)");
$stmt->execute(array($id));

改成这样

<?php$pdo=new PDO("mysql:host=localhost;dbname=t1","root","");$id=$_GET["id"];$stmt=$pdo->prepare("insert into ajax(txt) values(?)");$stmt->execute(array($id));?>

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn