Jump to content

I can't put data into local SQL database using a php script, can you help?


Go to solution Solved by mac_gyver,

Recommended Posts

Hey, sorry I'm a newb so prepare for me to do everything wrong even though I read the FAQ.  

 

I'm trying to use a simple php script to input data to register but I get this error: 

"Failed to run query: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined"  once I fill out the form. 

 

I've seen other people get this error but I seem to have different problems to them and can't figure it out. 

 

My PHP script is just

<?php


require("config.inc.php");

//if posted data is not empty
if (!empty($_POST)) {
  
    if (empty($_POST['username']) || empty($_POST['password'])) {
		
	
	  
	    $response["success"] = 0;
	    $response["message"] = "Please Enter Both a Username and Password.";

		
        die(json_encode($response));
    }

    $query        = " SELECT 1 FROM users WHERE username = :user";
    $query_params = array(
        ':user' => $_POST['username']
    );
    
    try {
        $stmt   = $db->prepare($query);
        $result = $stmt->execute($query_params);
    }
    catch (PDOException $ex) {
        die("Failed to run query: " . $ex->getMessage());

		$response["success"] = 0;
		$response["message"] = "Database Error. Please Try Again!";
		die(json_encode($response));
    }
    

    $row = $stmt->fetch();
    if ($row) {
        die("This username is already in use");
		$response["success"] = 0;
		$response["message"] = "I'm sorry, this username is already in use";
		die(json_encode($response));
    }
    
    $query = "INSERT INTO users ( username, password ) VALUES ( :user, :pass ) ";
    
    $query_params = array(
        ':username' => $_POST['username'],
        ':password' => $_POST['password']
    );
    
    try {
        $stmt   = $db->prepare($query);
        $result = $stmt->execute($query_params);
    }
    catch (PDOException $ex) {
        die("Failed to run query: " . $ex->getMessage());
		$response["success"] = 0;
		$response["message"] = "Database Error. Please Try Again!";
		die(json_encode($response));
    }

	$response["success"] = 1;
	$response["message"] = "Username Successfully Added!";
	echo json_encode($response);

    
} else {
?>
	<h1>Register</h1> 
	<form action="index.php" method="post"> 
	    Username:<br /> 
	    <input type="text" name="username" value="" /> 
	    <br /><br /> 
	    Password:<br /> 
	    <input type="password" name="password" value="" /> 
	    <br /><br /> 
	    <input type="submit" value="Register New User" /> 
	</form>
	<?php
}

?>

and the config: 

<?php 
	$username = "root"; 
    $password = ""; 
    $host = "localhost"; 
    $dbname = "drinkdealz"; 


   
    $options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'); 
     
  
    try 
    { 
       
        $db = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options); 
    } 
    catch(PDOException $ex) 
    { 
       
        die("Failed to connect to the database: " . $ex->getMessage()); 
    } 
     
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
     
  
    $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); 
     
 
    if(function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) 
    { 
        function undo_magic_quotes_gpc(&$array) 
        { 
            foreach($array as &$value) 
            { 
                if(is_array($value)) 
                { 
                    undo_magic_quotes_gpc($value); 
                } 
                else 
                { 
                    $value = stripslashes($value); 
                } 
            } 
        } 
     
        undo_magic_quotes_gpc($_POST); 
        undo_magic_quotes_gpc($_GET); 
        undo_magic_quotes_gpc($_COOKIE); 
    } 
     
    header('Content-Type: text/html; charset=utf-8'); 
     
  
    session_start(); 




?>

Sorry if I uploaded anything wrong and it's only a localhost so I don't really care about passwords etc (none will be the same when I upload it) 

So if any of you can even point me in the direction of what to look for that would be brilliant. Cheers, Kieran

  • Solution

the error is likely coming from your INSERT query (during your debugging you need to pin down where in the code the error is occurring at.)

 

the query is using place-holder/parameter names - :user, :pass

 

the ->execute() method is using ':username' => $_POST['username'] and ':password' => $_POST['password'].

 

the names must match up.

the error is likely coming from your INSERT query (during your debugging you need to pin down where in the code the error is occurring at.)

 

the query is using place-holder/parameter names - :user, :pass

 

the ->execute() method is using ':username' => $_POST['username'] and ':password' => $_POST['password'].

 

the names must match up.

Thanks, that fixed my problem!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.