douxun4173 2015-08-03 00:32 采纳率: 0%
浏览 52
已采纳

数据无法在表单提交时发送? [关闭]

This is my code:

<?php
require_once "connect.php";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  $email = test_input($_POST["email"]);
    if(!empty($email))
    {
        $insert = $db->prepare("INSERT INTO creations(email) VALUES (?)");
        $insert->bind_param('s', $email);
    }
}

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
?>

<!DOCTYPE html>
<html>
    <body>
        <form method="post" action="">

            <input type="text" name="email">
            <input type="submit">
        </form>
    </body>
</html>

When I submit anything in the email input box, I get an error saying this:

Fatal error: Call to a member function bind_param() on a non-object in /home/xx/xx/xx/xx on line 12.

I've tried this with a few different forms and got the same error. I've double checked everything on both my database and in my PHP code...

  • 写回答

1条回答 默认 最新

  • douruoshen1449 2015-08-03 00:54
    关注

    This should do the trick let me know if you have any problems.

    //CODE

    <?php
    //PATH --- /var/www/html/connect.php
    require_once($_SERVER['DOCUMENT_ROOT'] . "/connect.php");
    if (isset($_POST["email"])){
        $email = filter_data($_POST["email"]);
        $stmt = $db->prepare("INSERT INTO creations(email) VALUES (?)"); 
        $stmt->bind_param('s', $email);
        $stmt->execute();
        $stmt->close(); 
    }
    function filter_data($data) {
      $data = trim($data);
      $data = stripslashes($data);
      $data = htmlspecialchars($data);
      return $data;
    }
    ?>
    <!DOCTYPE html>
    <html>
        <body>
            <form method="post" action="">
                <input type="text" name="email">
                <input type="submit">
            </form>
        </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向