[英]php mysql insert statement using variables
我的PHP代碼拋出錯誤如下:
$stmt = $con->prepare('INSERT INTO listOfRides (address, time) VALUES
('$address', '$time')') ;
我看過其他文章,似乎我在正確地使用變量,並在其周圍加上了單引號,但是在訪問URL時顯示以下錯誤:
Parse error: syntax error, unexpected T_VARIABLE in /home/gbidjght/public_html
/insertRide.php on line 79
任何幫助表示贊賞
如果轉義了單引號,則最終會將字符串文字“ $ address”和“ $ time”插入到數據庫中:
$stmt = $con->prepare('INSERT INTO listOfRides (address, time) VALUES (\'$address\', \'$time\')');
但是,假設它們應該是變量,則應在SQL語句周圍使用雙引號,以允許PHP實際上將變量解析為其值:
$stmt = $con->prepare("INSERT INTO listOfRides (address, time) VALUES ('$address', '$time')");
話雖這么說,既然您已經准備好聲明,為什么不使用占位符呢? 這將是防止SQL注入的更安全方法。
$stmt = $con->prepare("INSERT INTO listOfRides (address, time) VALUES (?, ?)");
$stmt->execute(array($address, $time));
將外部引號更改為雙引號
$stmt = $con->prepare("INSERT INTO listOfRides (address, time) VALUES
('$address', '$time')") ;
您不能將mysql '
放入php '
用這個
$stmt = $con->prepare("INSERT INTO listOfRides (address, time) VALUES
('$address', '$time')") ;
因為的'
S上的錯誤來了。 添加"
而不是'
嘗試以下操作-
$stmt = $con->prepare("INSERT INTO listOfRides (address, time) VALUES ('$address', '$time')") ;
$stmt = $con->prepare("INSERT INTO `listOfRides` (`address`, `time`)
VALUES
($address, $time)") ;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.