在我的 javascript ajax 代碼中,我將一些數(shù)據(jù)發(fā)送到另一個(gè) php 文件,以便將數(shù)據(jù)記錄到 mysql 數(shù)據(jù)庫。當(dāng)我只使用數(shù)字?jǐn)?shù)據(jù)時(shí),它會將所有數(shù)據(jù)記錄到 mysql(像這樣)var myKeyVals = { pays: '123', order_id :'555' } var saveData = $.ajax({ type: 'POST', url: "<?=$base_url?>pay/Paypal2.php", data: myKeyVals, dataType: "text", success: function(resultData) { alert('Transaction completed by ' + details.payer.name.given_name+'--'+data.orderID) }}但是當(dāng)我在數(shù)據(jù)中使用字母字符時(shí),ajax 腳本不會像這樣發(fā)送數(shù)據(jù) var myKeyVals = { pays: 'abc784', order_id :'ab45c' } var saveData = $.ajax({ type: 'POST', url: "<?=$base_url?>pay/Paypal2.php", data: myKeyVals, dataType: "text", success: function(resultData) { alert('Transaction completed by ' + details.payer.name.given_name+'--'+data.orderID) }}我的錄音機(jī)頁面 php 代碼是這樣的 $x=$_POST['pays']; $y=$_POST['order_id']; $sql = "INSERT INTO table(pays, order_id) VALUES ($x, $y)"; if ($conn->query($sql) === TRUE) { echo "New record created successfully"; } else { echo "Error: " . $sql . "<br>" . $conn->error; } $conn->close();
1 回答

慕運(yùn)維8079593
TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超5個(gè)贊
如果你想防止一些 SQL 注入并解決你的問題,只需使用參數(shù)
$stmt = $conn->prepare("INSERT INTO table(pays, order_id) VALUES (?, ?)");
if(!$stmt) die('we have had some problem here');
$stmt->bindparam('s', $x); //replaces first ?
$stmt->bindparam('s', $y); //replaces second ?
$stmt->execute();
您還可以應(yīng)用一些好的做法,例如,如果 $y 是數(shù)字
$y=$_POST['order_id'];
if( !is_numeric($y) ) die('$y must be a numeric value');
或者如果 $y 是僅由字母數(shù)字字符形成的哈希,您也可以檢查它
if( !preg_match("/[^a-z0-9]+$/i",$y) ) die('$y has other characters other than a-z0-9');
- 1 回答
- 0 關(guān)注
- 171 瀏覽
添加回答
舉報(bào)
0/150
提交
取消