dongxian1921
2013-12-07 03:55
浏览 42
已采纳

在php中使用存储过程

Im using mysql stored procedures in my databases and now I want to be able to use it with my php code but none of the examples I've searched show a clear way to do this. Can someone tell me why this isn't working?

<html>
<head>
<title>Add</title>
<link rel="stylesheet" type="text/css" href="main.css">
<?php include("navbar.php")?>
</head>

<body>
<?php
if(isset($_POST['submit'])) {
    $link = mysqli_connect("localhost", "root", "", "test");

    $firstname = $_POST['firstname'];
    $lastname = $_POST['lastname'];

    mysqli_query($link, "CALL AddUser($firstname, $lastname)");
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    First Name:<input type="text" name="firstname" required>
    Last Name:<input type="text" name="lastname" required>
    <input type="submit" value="submit">
</form>

</body>
</html>

Here is the stored procedure

CREATE DEFINER=`root`@`localhost` PROCEDURE `AddUser`(IN in_first_name VARCHAR(100), IN in_last_name VARCHAR(100) )
BEGIN
    INSERT INTO `users`(first_name, last_name) VALUES (in_first_name, in_last_name );
END

Here is the table in the database test

CREATE TABLE IF NOT EXISTS `users` (
    `users_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
    `first_name` varchar(100) NOT NULL,
    `last_name` varchar(100) NOT NULL,
    PRIMARY KEY (`users_id`)
 ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;

--

-- Dumping data for table users

INSERT INTO `users` (`users_id`, `first_name`, `last_name`) VALUES
(1, 'Joey', 'Rivera'),
(2, 'John', 'Doe'),
(3, 'Cesar', 'Guerrero'),
(4, 'Maribel', 'Guerrero'),
(8, 'Marc', 'Jacobs');

图片转代码服务由CSDN问答提供 功能建议

我在我的数据库中使用mysql存储过程现在我希望能够将它与我的php代码一起使用但是没有 我搜索过的例子显示了一个明确的方法。 有人可以告诉我为什么这不起作用?

 &lt; html&gt; 
&lt; head&gt; 
&lt; title&gt;添加&lt; / title&gt; 
&lt; link rel =“stylesheet”type =“text / css”href  =“main.css”&gt; 
&lt;?php include(“navbar.php”)?&gt; 
&lt; / head&gt; 
 
&lt; body&gt; 
&lt;?php 
if(isset($ _ POST [  'submit'])){
 $ link = mysqli_connect(“localhost”,“root”,“”,“test”); 
 
 $ firstname = $ _POST ['firstname']; 
 $ lastname =  $ _POST ['lastname']; 
 
 mysqli_query($ link,“CALL AddUser($ firstname,$ lastname)”); 
} 
?&gt; 
&lt; form method =“post”action =“  &lt;?php echo $ _SERVER ['PHP_SELF'];?&gt;“&gt; 
名字:&lt; input type =”text“name =”firstname“required&gt; 
姓氏:&lt; input type =”  text“name =”lastname“required&gt; 
&lt; input type =”submit“value =”submit“&gt; 
&lt; / form&gt; 
 
&lt; / body&gt; 
&lt; / html&gt; 
   
 
 

这是存储过程

  CREATE DEFINER =`root` @`localhost` PROCEDURE`AddUser`(IN in_first_name VARCHAR  (100),IN in_last_name VARCHAR(100))
BEGIN 
 INSERT INTO`users`(first_na  me,last_name)VALUES(in_first_name,in_last_name); 
END 
   
 
 

这是数据库中的表 test \ n

  CREATE TABLE IF NOT EXISTS`users`(
`users_id` int(10)unsigned NOT NULL AUTO_INCREMENT,
`in first_name` varchar(100)NOT NULL,
`last_name` varchar  (100)NOT NULL,
 PRIMARY KEY(`users_id`)
)ENGINE = InnoDB DEFAULT CHARSET = latin1 AUTO_INCREMENT = 10; 
   
 
 

-

- 转储表用户的数据
  INSERT INTO`user`(`users_id`,`first_name`  ,``last_name`)VALUES 
(1,'Joey','Rivera'),
(2,'John','Doe'),
(3,'Cesar','Guerrero'),
  (4,'Maribel','Guerrero'),
(8,'Marc','Jacobs'); 
   
 

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dtzhfb4869 2013-12-07 04:36
    已采纳

    u should have to name attribute for submit button..i hope this will help.

     <input type="submit" name='submit' value="submit">
    
    打赏 评论
  • douguangxiang0363 2013-12-07 03:58

    String literals should be quoted. Try to change

    mysqli_query($link, "CALL AddUser($firstname, $lastname)");
    

    to

    mysqli_query($link, "CALL AddUser('$firstname', '$lastname')");
                                      ^          ^  ^         ^
    

    and add at least some the basic error handling

    $link = mysqli_connect("localhost", "root", "", "test");
    if (mysqli_connect_errno()) {
        echo "Connect failed: " . mysqli_connect_error();
        exit();
    }
    if (!$mysqli_query("CALL AddUser('$firstname', '$lastname')")) {
        echo "CALL failed: " . $mysqli_errno($link) . " - " . $mysqli_error($link);
        exit();
    }
    

    On a side note: consider to learn and use prepared statements intend of interpolating query strings.


    A version of your code using prepared statements might look like

    <?php
    if (isset($_POST['submit'])) {
    
        $firstname = $_POST['firstname'];
        $lastname = $_POST['lastname'];
    
        $link = new mysqli('localhost', 'root', '', 'test');
        if (mysqli_connect_errno()) {
            die('Connect failed: ' . mysqli_connect_error()); //TODO: a better error handling
        }
        $stmt = $link->prepare("CALL AddUser(?, ?)");
        if (!$stmt) {
            die('Prepare failed: ' . $link->error); //TODO: a better error handling
        }
        $stmt->bind_param('ss', $firstname, $lastname);
        if(!$stmt->execute()) {
            die('Execute failed: ' . $link->error); //TODO: a better error handling
        }
    }
    ?>
    

    And as @MayurKukadiya mentioned add name attribute to your input

        <input type="submit" name='submit' value="submit">
    
    打赏 评论
  • dry69034 2013-12-07 04:26

    PDO is your need, make sure you have pdo-mysql extention enabled and installed

    <?php
    $stmt = $dbh->prepare("CALL sp_returns_string(?)");
    $stmt->bindParam(1, $return_value, PDO::PARAM_STR, 4000); 
    
    // call the stored procedure
    $stmt->execute();
    
    print "procedure returned $return_value
    ";
    ?>
    

    http://php.net/manual/en/pdo.prepared-statements.php

    打赏 评论

相关推荐 更多相似问题