这个问题在这里已经有答案了:
在mysql中何时使用单引号、双引号和反引号(13个答案)
如何防止php中的sql注入(28个答案)
两年前关门了。
我不想准备或绑定参数,我只想简单地执行对过程的调用,该过程存储在我的餐厅数据库中。该过程接收两个输入参数,我需要通过从ajax脚本获得的变量传入这些参数。当我从workbench调用这个过程时,它可以很好地工作,但是当从php脚本调用时,它不会工作。
<?php
//Open Database Connection
$connection = "mysql:host=localhost;dbname=restaurant";
$user = "root";
$pwd = "Dumpweed37!";
$db = new PDO($connection, $user, $pwd);
//Get variables from AJAX
$first = $_GET["first"];
$last = $_GET["last"];
$type = $_GET["type"];
//Add new employee
if ($type==="server"){
$add = $db->exec('CALL AddServer($first, $last)'); }
else if ($type==="bartender"){
$add = $db->exec('CALL AddBartender($first, $last)'); }
else if ($type==="host"){
$add = $db->exec('CALL AddHost($first, $last)'); }
以下是我的存储过程:
DROP PROCEDURE IF EXISTS AddServer$$
CREATE PROCEDURE AddServer(firstname TEXT,lastname TEXT)
BEGIN
INSERT INTO employee VALUE (NULL, firstname,lastname);
INSERT INTO server VALUE (LAST_INSERT_ID(), NULL);
END$$
DROP PROCEDURE IF EXISTS AddBartender$$
CREATE PROCEDURE AddBartender(firstname TEXT,lastname TEXT)
BEGIN
INSERT INTO employee VALUE (NULL, firstname,lastname);
INSERT INTO bartender VALUE (LAST_INSERT_ID());
END$$
DROP PROCEDURE IF EXISTS AddHost$$
CREATE PROCEDURE AddHost(firstname TEXT,lastname TEXT)
BEGIN
INSERT INTO employee VALUE (NULL, firstname,lastname);
INSERT INTO `host` VALUE (LAST_INSERT_ID());
END$$
1条答案
按热度按时间vecaoik11#
我认为这都是因为缺少引号:
但是,您应该注意sql注入,所以您应该考虑使用prepared语句!