Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Connect to DB
- */
- $host = "localhost";
- $user = "root";
- $pass = "";
- $dbname = "content";
- try {
- $conn = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
- $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- }
- catch(PDOException $e) {
- echo "Connection failed: " . $e->getMessage();
- }
- if(isset($_POST['user'])) {
- $username = trim($_POST['user']);
- /**
- * Set error to empty in the beginning...
- */
- $error = '';
- /**
- * Example of form validation...
- */
- if(empty($username)) {
- $error = "You must enter a username.";
- }
- /**
- * If the error is still empty, we can go ahead
- * and run our query...
- */
- if($error == '') {
- $sql = "SELECT * FROM users WHERE username = :username";
- $stmt = $conn->prepare($sql);
- $stmt->bindParam(":username", $username, PDO::PARAM_STR);
- $stmt->execute();
- //var_dump($stmt->execute());
- if($stmt->rowCount() > 0) {
- $error = "That user name is taken...";
- } else {
- $error = "Username is not taken...";
- }
- }
- $data = array(
- 'error' => $error
- );
- echo json_encode($data);
- } else {
- die("Cannot access this file directly...");
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement