我不想准备或绑定params,我只想简单地执行对程序的调用,该程序存储在我的餐馆数据库中。该过程接收2个输入参数,我需要通过我从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$$
答案 0 :(得分:2)
我认为这完全是缺少引号:
$db->exec("CALL AddServer('$first', '$last')");
但是,你应该注意sql注入,所以你应该考虑使用预准备语句!