Insert Data to an MySQL Database using PHP

July 6, 2015 1.2k views
PHP MySQL Ubuntu

Hello i need to create a form that adds Data to a MySQL Database using PHP.

I created this, but it does not work

THIS NEEDS TO BE ADDED:

INSERT INTO `mailserver`.`virtual_users`
  (`domain_id`, `password` , `email`)
VALUES
  ('5', ENCRYPT('newpassword', CONCAT('$6$', SUBSTRING(SHA(RAND()), -16))) , 'email3@newdomain.com');

It would be also nice that PHP queries the latest domain_id and adds a 1 after every new Email Address

<!DOCTYPE html>
<html>
    <head>
        <title>Add an E-Mail</title>    
    </head> 
    <body>
        <?php
            if(isset($_POST['submit'])){
                $servername = "localhost";
                $username = "lalalala";
                $password = "heheheh";
                $dbname = "mailserver"; 

                try {
                        $userpassword = = $_POST['password'];
                        $useremail = = $_POST['email']; 

                        $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
                        // set the PDO error mode to exception
                        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                        $sql = "
                            INSERT INTO `mailserver`.`virtual_users`
                            (`domain_id`, `password` , `email`)
                            VALUES
                            ('3', ENCRYPT('$userpassword', CONCAT('$6$', SUBSTRING(SHA(RAND()), -16))) , '$useremail');
                        ";
                        // use exec() because no results are returned
                        $conn->exec($sql);
                        echo "New record created successfully";
                    }
                    catch(PDOException $e)
                        {
                            echo $sql . "<br>" . $e->getMessage();
                        }

                        $conn = null;
                    }
        ?>
        <form action="" method="post">
            <p>Email<input type="email" name="email" placeholder="Your Email"></p>
            <p><input type="password" name="password" placeholder="Your Password"></p>
            <input type="submit" name="submit" value="Submit">
        </form>
    </body>
</html>
1 comment
  • You can cancle this It would be also nice that PHP queries the latest domain_id and adds a 1 after every new Email Address

1 Answer
Have another answer? Share your knowledge.