Insert Data to an MySQL Database using PHP

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

I created this, but it does not work


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

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

<!DOCTYPE html>
		<title>Add an E-Mail</title>	
				$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`)
  							('3', ENCRYPT('$userpassword', CONCAT('$6$', SUBSTRING(SHA(RAND()), -16))) , '$useremail');
    					// use exec() because no results are returned
    					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">
Show comments

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

I got it to work