Php_droid Posted July 15, 2014 Share Posted July 15, 2014 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 Link to comment https://forums.phpfreaks.com/topic/289892-i-cant-put-data-into-local-sql-database-using-a-php-script-can-you-help/ Share on other sites More sharing options...
mac_gyver Posted July 15, 2014 Share Posted July 15, 2014 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. Link to comment https://forums.phpfreaks.com/topic/289892-i-cant-put-data-into-local-sql-database-using-a-php-script-can-you-help/#findComment-1485131 Share on other sites More sharing options...
Php_droid Posted July 15, 2014 Author Share Posted July 15, 2014 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! Link to comment https://forums.phpfreaks.com/topic/289892-i-cant-put-data-into-local-sql-database-using-a-php-script-can-you-help/#findComment-1485311 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.