2

I've setup a sign up page to register users, its passed from HTML and into PHP however the PHP function is not passing it over to the MySQL database

   <?php
     try{
      $db = new PDO ("mysql:host=localhost;dbname=car_rental;port=3306","root","");

    }
    catch (Exception $e){
      echo "SQL is Off";
      exit;

    }
    echo "success"; 

    try{
    $trial = "INSERT INTO users (firstName) VALUES ('trial')";

    }
    catch (Exception $e){
      echo "doesnt work..";
    }
    echo "works?";
     try{
     function NewUser() 
     { 
       $firstName = $_POST['firstName'];
       $lastName = $_POST['lastName'];
       $age = $_POST['age'];
       $email = $_POST['email']; 
      $password = $_POST['pass']; 
      $query = "INSERT INTO users (firstName,lastName,age,email,pass) VALUES ('$firstName','$lastName','$age','$email','$password')";
      echo"user created";
      }
     }
     catch (PDOException $e)
     {
       echo "ERROR -_-";
     }

      ?>

is this the correct implementation to execute a sql query in PHP?

function NewUser() 
 { 
   $firstName = $_POST['firstName'];
   $lastName = $_POST['lastName'];
   $age = $_POST['age'];
   $email = $_POST['email']; 
  $password = $_POST['pass']; 
  $query = "INSERT INTO users (firstName,lastName,age,email,pass) VALUES ('$firstName','$lastName','$age','$email','$password')";
  $db->exec($query);
  echo"user created";
  }

Thanks

6
  • 3
    You just write queries, but forgot to execute Commented Dec 16, 2015 at 6:36
  • 2
    where is query function?? Commented Dec 16, 2015 at 6:36
  • Where is your sql query executing? Commented Dec 16, 2015 at 6:46
  • 1
    Hey, just heads up, this is extremely insecure! You are not sanitizing any of your input, and so people could pass malicious code and run it on your database with an SQL injection. If this is just to learn then you might not care, but if this is going online, you need to fix that. Also, you are saving passwords in plain text, which is very bad, but again, fine if you are just playing around with learning how to call a database from php. I just wanted to mention all that incase this is for anything real. Commented Dec 16, 2015 at 6:46
  • 1
    its just to learn so no need to worry but i will keep it in mind if ever i upload it @rp.beltran Commented Dec 16, 2015 at 6:49

1 Answer 1

2

You just write queries, but forgot to execute

$db = new PDO ("mysql:host=localhost;dbname=car_rental;port=3306","root","");
$query = "INSERT INTO users (firstName,lastName,age,email,pass) VALUES ('$firstName','$lastName','$age','$email','$password')";
$db->query($db); // executes it
Sign up to request clarification or add additional context in comments.

1 Comment

so it basically becomes $db = new PDO ("mysql:host=localhost;dbname=car_rental;port=3306","root",""); function NewUser() { $firstName = $_POST['firstName']; $lastName = $_POST['lastName']; $age = $_POST['age']; $email = $_POST['email']; $password = $_POST['pass']; $query = "INSERT INTO users (firstName,lastName,age,email,pass) VALUES ('$firstName','$lastName','$age','$email','$password')"; $db->query($db); echo"user created"; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.