Question

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

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>
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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

I got it to work