The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

insert data by passing parameters through url

unmaxmacha's Avatar

unmaxmacha

11 Sep, 2015 07:25 AM

{"error":{"text":SQLSTATE[HY093]: Invalid parameter number: parameter was not defined}}

my code:
<?php
 require_once '../Slim/Slim.php';
 require_once '../db.php';

 $app = new Slim();

$app->get('/create', 'addUser'); // Add Users

 $app->run();

 unction addUser(){
  // error_log('addUser\n', 3, '/var/tmp/php.log');
  // $request = Slim::getInstance()->request();
  // $user = json_decode($request->getBody());
  echo $sql = "INSERT INTO tbl_user (username, password, address, phone) VALUES (:username, :password, :address, :phone)";
  try {
   $db = getConnection();
   $stmt = $db->prepare($sql);
   $stmt->bindParam("username", $user->username);
   $stmt->bindParam("password", $user->password);
   $stmt->bindParam("address", $user->address);
   $stmt->bindParam("phone", $user->phone);
   $stmt->execute();
   $wine->user_id = $db->lastInsertId();
   $db = null;
   echo json_encode($user);
  } catch(PDOException $e) {
   // error_log($e->getMessage(), 3, '/var/tmp/php.log');
   echo '{"error":{"text":'. $e->getMessage() .'}}';
  }
 }

?>

result :
INSERT INTO tbl_user (username, password, address, phone) VALUES (:newuser, :newpass, :newaddress, :newphone){"error":{"text":SQLSTATE[HY093]: Invalid parameter number: parameter was not defined}}

  1. 1 Posted by Qiniso on 11 Sep, 2015 10:50 AM

    Qiniso's Avatar

    Try modifying yout $sql= and your $stmt->execute() like below, and see what result you get.

    $sql  = "INSERT INTO tbl_user (username, password, address, phone) VALUES (?,?,?,?)";
    $stmt = $db->prepare($sql);
    $stmt->execute([
        $user->username,
        $user->password,
        $user->address,
        $user->phone
    ]);
    

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Already uploaded files

  • index.php 3.2 KB

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac