用事先准备好的声明中插入值数据库(Inserting value to database using

2019-09-29 20:45发布

我试图让用户插入在db表指定行的价值。 不过,我不断收到一个错误:调用一个成员函数寄存器()

下面是我使用更新表的PHP和HTML代码

<?php
ini_set("log_errors", 1);
ini_set("error_log", "/tmp/php-error.log");
session_start();
require_once 'class.user.php';
$user_home = new USER();

if(!$user_home->is_logged_in())
{
    $user_home->redirect('index.php');
}

$stmt = $user_home->runQuery("SELECT * FROM tbl_client_info WHERE UCODE=:uid");
$stmt->execute(array(":uid"=>$_SESSION['userSession']));
$row = $stmt->fetch(PDO::FETCH_ASSOC);

    if($stmt->rowCount() == 1)
    {
        if(isset($_POST['submit']))
        {
            $purchasedata = $_POST['purchasedata']; 
            $cpurchasedata = $_POST['cpurchasedata'];


            if($cpurchasedata!==$purchasedata)
            {
                $msg = "<div class='alert alert-block'>
                        <button class='close' data-dismiss='alert'>&times;</button>
                        <strong>Sorry!</strong>  Input Does Not Match. Make sure the details match. 
                        </div>";
            }
            else
            {

                $stmt = $user-> register("UPDATE tbl_client_info SET purchasedata=? WHERE UCODE=:uid");
                $stmt->execute(array(":purchasedata"=>$purchasedata,":uid"=>$rows['UCODE']));

                //

                $msg = "<div class='alert alert-success'>
                        <button class='close' data-dismiss='alert'>&times;</button>
                        Okay, we have added data to your account.
                        </div>";
            }
        }   
    }
    else
    {
        $msg = "<div class='alert alert-success'>
                <button class='close' data-dismiss='alert'>&times;</button>
                No Sorry That Did Not Work, Try again
                </div>";

    }

?>

<!DOCTYPE html>
<html>
  <head>
    <title>Forgot Password</title>
    <!-- Bootstrap -->
    <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
    <link href="bootstrap/css/bootstrap.css" rel="stylesheet" media="screen">
    <link href="bootstrap/css/bootstrap-responsive.min.css" rel="stylesheet" media="screen">
    <link href="assets/styles.css" rel="stylesheet" media="screen">

    <link href="css/bootstrap.min.css" rel="stylesheet">

    <link href="fonts/css/font-awesome.min.css" rel="stylesheet">
    <link href="css/animate.min.css" rel="stylesheet">
     <!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
    <!--[if lt IE 9]>
      <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <!-- Custom styling plus plugins -->
    <link href="css/custom.css" rel="stylesheet">
  <link href="css/icheck/flat/green.css" rel="stylesheet">
<script src="js/vendor/modernizr-2.6.2-respond-1.1.0.min.js"></script>

<!-- Sweet Alert -->
  <script src="dist/sweetalert-dev.js"></script>
  <link rel="stylesheet" href="dist/sweetalert.css">
  <!--.......................-->

  </head>



<body style="background:#f3f3f3;"> 

    <div id="wrapper">
      <div id="login_content" class="animate form">
        <section class="login_content">
          <form method="post">
            <h1>Purchase Data</h1>

            <div class='alert alert-success'>
            <strong>Hello </strong><?php echo $row['firstname'] ?>! //add more text here
        </div>

        <?php
        if(isset($msg))
        {
            echo $msg;
        }
        ?>
        <input type="text" class="input-block-level" placeholder="500mb" name="purchasedata" required />
        <input type="text" class="input-block-level" placeholder="Retype the bundle" name="cpurchasedata" required />
        <hr />
        <button class="btn btn-large btn-primary" type="submit" name="btn-update-data">Add data to my account</button>
            <div class="clearfix"></div>
            <div class="separator">

              <p class="change_link">All Done ?
                <a href="index.php" class="to_register"> Go Home </a> 

我class.user.php看起来是这样的:

<?php

require_once 'dbconfig.php';

class USER
{   

    private $conn;

    public function __construct()
    {
        $database = new Database();
        $db = $database->dbConnection();
        $this->conn = $db;
    }

    public function runQuery($sql)
    {
        $stmt = $this->conn->prepare($sql);
        return $stmt;
    }

    public function lasdID()
    {
        $stmt = $this->conn->lastInsertId();
        return $stmt;
    }

    public function register($uname,$email,$upass,$code,$purchasedata)
    {
        try
        {                           
            $password = md5($upass);
            $stmt = $this->conn->prepare("INSERT INTO tbl_client_info(User_Name,billingemail,password,purchasedata,tokenCode) 
                                                         VALUES(:User_Name, :billingemail, :password, :purchasedata, :active_code)");
            $stmt->bindparam(":user_name",$uname);
            $stmt->bindparam(":user_mail",$email);
            $stmt->bindparam(":user_pass",$password);
            $stmt->bindparam(":active_code",$code);
            $stmt->bindparam(":purchasedata",$purchasedata);
            $stmt->execute();   
            return $stmt;
        }

它是什么,我做错了什么? 我已经搜查了错误,但我看不到的地方我已经错了。 任何帮助或链接,以帮助将不胜感激。

Answer 1:

$user应该是$user_home 。 你已经同时呼吁使用了错误的varible register功能。



文章来源: Inserting value to database using prepared statement