无法找到错误:无法运行查询:SQLSTATE [HY093]:参数号无效:参数未定义

时间:2016-09-15 13:37:46

标签: php mysql twitter-bootstrap pdo

我试图为客户编写注册页面,但每次都会出现此错误。

无法运行查询:SQLSTATE [HY093]:参数号无效:参数未定义

我已按照本教程:http://untame.net/2013/06/how-to-build-a-functional-login-form-with-php-twitter-bootstrap/ 数据库是这样的: DB

代码是这样的:

<?php
// create a new cURL resource
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token");

//Here you will have to add your options, I think this code will bring you far :-)
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt ($ch, CURLOPT_TIMEOUT, 30);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);

// grab URL and pass it to the browser
curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);
?>

我已经看到所有变量都没问题,我也不知道错误在哪里。

我的config.php

    <?php


        require("config.php");
    if(empty($_SESSION['user'])) 
    {
        header("Location: index.php");
        die("Redirecting to index.php"); 
    }
    require("config.php");

    if(!empty($_POST)) 
    { 

        // Ensure that the user fills out fields 
        if(empty($_POST['username'])) 
        { die("Please enter a username."); } 
        if(empty($_POST['password'])) 
        { die("Please enter a password."); } 
        if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) 
        { die("Invalid E-Mail Address"); } 

        // Check if the username is already taken
        $query = " 
            SELECT 
                1 
            FROM users 
            WHERE 
                username = :username 
        "; 
        $query_params = array( ':username' => $_POST['username'] ); 
        $query_params = array( ':nombre' => $_POST['nombre'] );
        $query_params = array( ':empresa' => $_POST['empresa'] );
        $query_params = array( ':apellidos' => $_POST['apellidos'] );

        try { 
            $stmt = $db->prepare($query); 
            $result = $stmt->execute($query_params); 
        } 
        catch(PDOException $ex){ die("Failed to run query: " . $ex->getMessage()); } 
        $row = $stmt->fetch(); 
        if($row){ die("This username is already in use"); } 
        $query = " 
            SELECT 
                1 
            FROM users 
            WHERE 
                email = :email 
        "; 
        $query_params = array( 
            ':email' => $_POST['email'] 
        ); 
        try { 
            $stmt = $db->prepare($query); 
            $result = $stmt->execute($query_params); 
        } 


        catch(PDOException $ex){ die("Failed to run query: " . $ex->getMessage());} 
        $row = $stmt->fetch(); 
        if($row){ die("This email address is already registered"); } 

        // Add row to database 
        $query = " 
            INSERT INTO users ( 
                username, 
                password, 
                salt, 
                email,
                empresa,
                nombre,
                apellidos

            ) VALUES ( 
                :username, 
                :password, 
                :salt, 
                :email,
                :empresa,
                :nombre,
                :apellidos
            )"; 
        /*      



        */

        // Security measures
        $salt = dechex(mt_rand(0, 2147483647)) . dechex(mt_rand(0, 2147483647)); 
        $password = hash('sha256', $_POST['password'] . $salt); 
        for($round = 0; $round < 65536; $round++){ $password = hash('sha256', $password . $salt); } 
        $query_params = array( 
            ':username' => $_POST['username'], 
            ':password' => $password, 
            ':salt' => $salt, 
            ':email' => $_POST['email'], 
            ':empresa' => $_POST['empresa'],
            ':nombre' => $_POST['nombre'],
            ':apellidos' => $_POST['apellidos']
        ); 
        try {  
            $stmt = $db->prepare($query); 
            $result = $stmt->execute($query_params); 
        } 
        catch(PDOException $ex){ die("Failed to run query: " . $ex->getMessage()); } 
        header("Location: index.php"); 
        die("Redirecting to index.php"); 
    } 
?>

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Bootstrap Tutorial</title>
    <meta name="description" content="Bootstrap Tab + Fixed Sidebar Tutorial with HTML5 / CSS3 / JavaScript">
    <meta name="author" content="Untame.net">

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
    <script src="assets/bootstrap.min.js"></script>
    <link href="assets/bootstrap.min.css" rel="stylesheet" media="screen">
    <style type="text/css">
        body { background: url(assets/bglight.png); }
        .hero-unit { background-color: #fff; }
        .center { display: block; margin: 0 auto; }
    </style>
</head>

<body>

<div class="navbar navbar-fixed-top navbar-inverse">
  <div class="navbar-inner">
    <div class="container">
      <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </a>
      <a class="brand">Registro en 4tickets</a>
      <div class="nav-collapse">
        <ul class="nav pull-right">
          <li><a href="index.php">Volver a casa</a></li>
        </ul>
      </div>
    </div>
  </div>
</div>

<div class="container hero-unit">
    <h1>Register</h1> <br /><br />
    <form action="register.php" method="post"> 
        <label>Username:</label> 
        <input type="text" name="username" value="" /> 
        <label>Email: <strong style="color:darkred;">*</strong></label> 
        <input type="text" name="email" value="" /> 
        <label>Password:</label> 
        <input type="password" name="password" value="" /> <br />
        <label>Nombre:</label>
        <input type="text" name="nombre" value="" /> <br />
        <label>apellidos:</label>
        <input type="text" name="apellidos" value="" /> <br />
        <label>Empresa:</label>
        <input type="text" name="empresa" value="" /> <br />
        <p style="color:darkred;">* Poner bien las direcciones de mail</p><br />
        <input type="submit" class="btn btn-info" value="Register" /> 
    </form>
</div>

</body>
</html>

我只添加了3件事:empresa,nombre和apellidos,这些都不是教程中我需要的。

提前致谢, 约翰。

1 个答案:

答案 0 :(得分:0)

你在这里传递了错误的参数:

    // Check if the username is already taken
    $query = " 
        SELECT 
            1 
        FROM users 
        WHERE 
            username = :username 
    "; 
    $query_params = array( ':username' => $_POST['username'] ); 
    $query_params = array( ':nombre' => $_POST['nombre'] );
    $query_params = array( ':empresa' => $_POST['empresa'] );
    $query_params = array( ':apellidos' => $_POST['apellidos'] );

您尝试传递的参数太多,但每次都要覆盖$query_params。在本节中取出hombre,empress和apellidos,它应该提交。