在PHP中插入存储过程

时间:2016-04-06 11:46:11

标签: php mysql stored-procedures

我有一个简单的insert语句来保存PHP中的表单细节。

我想将其转换为商店程序。

以下是我目前的代码

    $servername = "localhost";
    $username = "aaaaaa";
    $password = "ppppp";
    $dbname = "xxxx_database";

    $sName = $_POST["name"];
    $sEmail = $_POST["email"];
    $sPhone = $_POST["Number"];
    $sInterest = $_POST["interest"];
    $Comments = $_POST["Inquiry"];


    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    $sql = "INSERT INTO ContactForm (Name, Email, PhoneNumber,Interest,Comments) VALUES ('$sName',  '$sEmail', '$sPhone','$sInterest', '$Comments')";

    if ($conn->query($sql) === TRUE) {
        echo "New record created";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }

$conn->close();

这是我的商店程序

CREATE DEFINER = `xxx`@`localhost` PROCEDURE `SpInsertContactForm` ( IN `Name` TEXT CHARSET armscii8, IN `Email` TEXT CHARSET armscii8, IN `PhoneNumber` TEXT CHARSET armscii8, IN `Interest` TEXT CHARSET armscii8, IN `Comments` TEXT CHARSET armscii8 ) NOT DETERMINISTIC NO SQL SQL SECURITY DEFINER INSERT INTO ContactForm( Name, Email, PhoneNumber, Interest, Comments )
VALUES (
Name, Email, PhoneNumber, Interest, Comments
)

我如何使用此商店程序。我是php和mysql的新手需要指针。

我使用SP作为

if (!$mysqli->query("CALL SpInsertContactForm($sName,  $sEmail, $sPhone,$sInterest, $Comments)")) 
        {
            echo "CALL failed: (" . $mysqli->errno . ") " . $mysqli->error;
        }

这不会在DB

中保存任何内容

1 个答案:

答案 0 :(得分:1)

你使用这个:$mysqli->query("CALL SpInsertContactForm('<your_namevalue>','<your_emailvalue>','<your_phonevalue>','<your_interestvalue>','<your_commentvalue>')

如需了解更多信息,请点击以下链接: http://php.net/manual/en/mysqli.quickstart.stored-procedures.php