Rumah > Artikel > pembangunan bahagian belakang > CI框架中使用ajax操作数据库问题
前台:
<script>//调用方法用ajax操作add_dbdata.phpvar xmlHttpfunction add_dbdata(str,str1){ //alert(str); if (str=="") { document.getElementById("txtHint").innerHTML=""; return; } if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("txtHint").innerHTML=xmlhttp.responseText; } } //xmlhttp.open("GET","add_dbdata.php?q="+str,true); //alert(str1); //alert(str); xmlhttp.open("GET","add_dbdata.php?q="+str,true); //xmlhttp.open("GET","<?=base_url()?>project_c/add_zijiedian/"+str1+str,true); 这样写,也不好使,不知道哪有问题 xmlhttp.send();}
<?phpheader("Content-type:text/html;charset=utf8");$q=$_GET["q"];$q=(int)$q;echo "<script>alter('kkkkkkk');</script> ";$con = mysql_connect('localhost', 'root', '');if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("test", $con);mysql_query("set names utf8");//$sql = " SELECT * FROM ecm_acategory ";$sql = " insert into data (name) values('999')";echo $sql;$result = mysql_query($sql);mysql_close($con);?>
你
xmlhttp.open("GET","=base_url()?>project_c/add_zijiedian/"+str1+str,true);
然后看看浏览器中的源代码
//插入预算表 $this->db->query("select @myLeft:=PLeft from constrproj where ConstrProjName = '$dangqiandianname' "); $this->db->query("UPDATE constrproj SET PRight = PRight + 2 WHERE PRight > @myLeft"); $this->db->query("UPDATE constrproj SET PLeft = PLeft + 2 WHERE PLeft > @myLeft"); $query3 = $this->db->query("select * from constrproj where ConstrProjID= ( SELECT min(ConstrProjID) from constrproj)"); $arr = $query3->result_array(); $ParentID = $arr[0]['ConstrProjID']; //echo '<script>alert("'.$ParentID.'");</script>'; $this->db->query("INSERT INTO constrproj(ConstrProjName,PLeft,PRight,ParentID,KindLvl) VALUES('$zijiediandianname',@myLeft + 1, @myLeft +2,'$ParentID',1)" ); $query2 = $this->db->query("select * from constrproj"); $result_count2 = count($query2->result());
PLeft 不能为空
你向 PLeft 列负的值是 @myLeft + 1
显然 @myLeft 是空值 NULL
那不是乱码,而是 url 编码
入库前先解码(urldecode)
搞定 谢谢!