以下是Ajax 提交表單數據到入庫的全盤操作流程,希望本文對廣大php開發者有所幫助,感謝閱讀本文
*******php項目中當我們要對數據庫進行寫入操作時,有時會因為代碼沒有做防sql注入工作,導致各種不可預知的錯誤******* 1,index.htm 這是一個很簡單的注冊頁面l 我這是以ajax形式提交數據 代碼如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>RegisterForm</title> <script type="text/javascript" src='jquery-1.3.1.js'></script> </head> <body> <h1>This is a test form! </h1> <font color="#8b0000"><table class='tb' border="0" cellpadding="5" cellspacing=0> <form action='_process.php' method='post' enctype='multipart/form-data'> <tr><td class="tdleft"><em>user :</em></td><td><input type='text' id='name' name='name' value='' size=15/></td></tr> <tr><td class="tdleft"><em>ages : </em></td><td><input type='text' id='ages' name='ages' value='' size=15/></td></tr> <tr><td class="tdleft"><em>pass : </em></td><td><input type='password' id='password' name='password' value='' size=15/></td></tr> <tr><td class="tdleft"><em>addr : </em></td><td><input type='text' id='addr' name='addr' value='' size=15/></td></tr> <tr><td class="tdleft"><em>email : </em></td><td><input type='text' name='email' id='email' value='' size=15/></td></tr> <tr><td class="tdleft"><input type='button' value='register' id="but"/></td><td><input type='reset' value='reseting'/></td></tr> </form> </table></font> <span id='msg' style='display:none'>正在加載... ...</span> <p id="result" class=""></p> <style type="text/css"> body{text-align:center;} .error{color:red;} .tb{margin:0 auto;width:350px;height:200px;text-align:center;} .tdleft{width:150px;text-align:left;} </style> <script type='text/javascript'> $("#but").click(function(){ var name = $.trim($("#name").val()); var ages = $.trim($("#ages").val()); var pn = /^d+$/; var addr = $.trim($("#addr").val()); var pass = $.trim($("#password").val()); var email = $.trim($("#email").val()); var reg = /^[a-zA-Z0-9_]+@[a-zA-Z0-9_]+(.[a-zA-Z]+)+$/gi; if(name.length==0){ alert("請認真填寫姓名!");return false; } if(ages.length==0){ alert("請填寫年齡!");return false; } if(!pn.test(ages)){ alert("請填寫有效數字!");return false; } if(pass.length==0||pass.length>6){ alert("請認真填寫密碼!");return false; } if(addr.length==0){ alert("請認真填寫地址!");return false; } if(email.length==0){ alert("請認真填寫郵件!");return false; } if(!reg.test(email)){ alert("電子郵件檢測失敗");return false; } var send = {'name':name,'ages':ages,'pass':pass,'addr':addr,'email':email}; $.post('_process.php',send,function(data){ if(data.res==-1){ $("#result").addClass("error"); } $("#result").html(data.msg); //$("form")[0].reset(); },'json'); }) $("#msg").ajaxStart(function(){ $(this).fadeIn(); }).ajaxStop(function(){ $(this).fadeOut(); }) </script> </body> </html> 2,_process.php接收ajax提交數據 並做相關處理的文件 代碼如下: <?php header('Content-Type:text/html;charset=utf-8'); sleep(1); $conn = mysqli_connect("localhost","root","root","register") or die("Unable to connect!".mysqli_connect_error()); mysqli_query($link,"set names utf8"); $name = isset($_POST['name'])?mysqlQuotes(trim($_POST['name'])):''; $ages = isset($_POST['ages'])?intval(trim($_POST['ages'])):''; $pass = isset($_POST['pass'])?mysqlQuotes(trim($_POST['pass'])):''; $addr = isset($_POST['addr'])?mysqlQuotes(trim($_POST['addr'])):''; $email = isset($_POST['email'])?mysqlQuotes(trim($_POST['email'])):''; if(empty($name)){ echo json_encode(array('res'=>-1,'msg'=>'請您認真輸入姓名'));exit; } //查詢username在 tb_register 表中是否存在,不存在則入庫,存在則告知已注冊 $sql0 = "select count(1) as nums from tb_register where username='{$name}'"; $row0 = select_one($conn,$sql0); if($row0['nums']>=1){ echo json_encode(array('res'=>-1,'msg'=>'對不起,該用戶名已被注冊!'));exit; } if(empty($ages)){ echo json_encode(array('res'=>-1,'msg'=>'請您認真輸入年齡'));exit; } if(!preg_match("/^d+$/",$ages)){ echo json_encode(array('res'=>-1,'msg'=>'請輸入合法的數字'));exit; } if(empty($pass)){ echo json_encode(array('res'=>-1,'msg'=>'請您認真輸入密碼'));exit; } if(empty($addr)){ echo json_encode(array('res'=>-1,'msg'=>'請您認真輸入地址'));exit; } if(empty($email)){ echo json_encode(array('res'=>-1,'msg'=>'請您認真輸入郵箱'));exit; } if(!preg_match("/^[a-zA-Z0-9_]+@[a-zA-Z0-9_]+(.[a-zA-Z]+)+$/",$email)){ echo json_encode(array('res'=>-1,'msg'=>'您的郵箱格式不正確'));exit; } $add_day = date("YmdHis"); //以下是入庫操作 $sql = "insert into tb_register set username='{$name}',ages='{$ages}',password='{$pass}',address='{$addr}',email='{$email}',add_day='{$add_day}'"; mysqli_query($conn,$sql); echo json_encode(array('res'=>1,'msg'=>'恭喜你,注冊成功!'));exit; //預防mysql注入攻擊函數 入庫數據 function mysqlQuotes($content){ if(!get_magic_quotes_gpc()) //先看看是否開啟自動過濾機制 未開啟則手動加函數過濾 { $content = addslashes($content); } return stripslashes(htmlspecialchars($content)); } function select_one($conn,$sql){ $res = mysqli_query($conn,$sql) or die("Failed".mysqli_error()); $result = array(); if(!empty($res)){ $result = mysqli_fetch_assoc($res); } mysqli_free_result($res); return $result; } ******以上 自定義函數mysqlQuotes中********** Tips1, (PS:T不錯的PHP Q扣峮:276167802,驗證:csl) addslashes 函數的作用是給指定的字符加上反斜槓,主要有以下特殊字符 單引號(') , 雙引號(") , 反斜槓() , NULL Tips2, htmlspecialchars 函數的作用是把一些特殊的字符轉換為html實體 ,這些特定的字符有: & (和號) ==> & " (雙引號) ==> " ' (單引號) ==> ' < (小於號) ==> < > (大於號) ==> > Tips3, stripslashes 函數作用是刪除 由addslashes ()函數添加的飯斜槓,恢復原來的摸樣 如,$username 用戶輸