Jump to content

User form to create file.


magickfirebird

Recommended Posts

I have a sign up page that when the person fills in the form and hits the Enter button it post below for them to verify that their information is correct. From this spot I would like for it to create a new insert into an existing table. Do I need to go back up in my coding to the "try" section when I set my if statements for my first form and create my "insert" code?

Link to comment
Share on other sites

My code so far:

try {$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);} catch(PDOException $e) {echo "Connection failed: " . $e->getMessage();} ini_set('display_errors', 1); error_reporting(E_ALL); $firstnameErr = $lastnameErr = $artistidErr = $emailErr = $passwordErr = "";$firstname = $lastname = $artistid = $email = $password = ""; if ($_SERVER["REQUEST_METHOD"] == "POST")  {if (empty($_POST["firstname"])) {$firstnameErr = "First Name is required";} else {$firstname = regcheck($_POST["firstname"]);     if (!preg_match("/^[a-zA-Z ]*$/",$firstname)) {$firstnameErr = "Only letters and white space allowed"; }}   if (empty($_POST["lastname"])) {$lastnameErr = "Last Name is required";} else {$lastname = regcheck($_POST["lastname"]);     if (!preg_match("/^[a-zA-Z ]*$/",$lastname)) {$lastnameErr = "Only letters and white space allowed"; }} if (empty($_POST["artistid"])) {$artistidErr = "Artist ID is required";} else {$artistid = regcheck($_POST["artistid"]);     if (!preg_match("/^[a-zA-Z ]*$/",$artistid)) {$artistidErr = "Only letters and white space allowed"; }} if (empty($_POST["email"]))    {$emailErr = "Email address is required";} else {$email = regcheck($_POST["email"]);      if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {$emailErr = "Invalid email address format";}} if (empty($_POST["password"])) {$passwordErr = "Password is required";} else {$password = regcheck($_POST["password"]);      if (!preg_match("/^[a-zA-Z ]*$/",$password)) {$passwordErr = "Only letters and white space allowed"; }}} function regcheck($data) {   $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data;}?>      <br /><h2>Registration</h2>    <p><span class="error">* required field.</span></p><form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">  First Name: <input type="text" name="firstname" value="<?php echo $firstname;?>">   <span class="error">* <?php echo $firstnameErr;?></span><br><br>   Last Name: <input type="text" name="lastname" value="<?php echo $lastname;?>">   <span class="error">* <?php echo $lastnameErr;?></span><br><br> Artist ID: <input type="text" name="artistid" value="<?php echo $artistid;?>">   <span class="error">* <?php echo $artistidErr;?></span><br><br> E-mail address: <input type="text" name="email" value="<?php echo $email;?>">   <span class="error">* <?php echo $emailErr;?></span><br><br>   Password: <input type="text" name="password" value="<?php echo $password;?>" /><span class="error">* <?php echo $passwordErr;?></span><br /><br />    <input type="submit" name="submit" value="Enter"> </form> <?php echo "<h2>Your Input:</h2>"; ?>Name:    <?php echo $firstname; ?>   <?php echo $lastname;echo "<br>"; ?>E-mail Address:    <?php echo $email;echo "<br>"; ?>Artist ID:    <?php echo $artistid;echo "<br>"; ?>Password:    <?php echo $password;echo "<br>";?><br /><input type="submit" name="submit" value="Submit to create" />
After information is entered into the form and "Entered" it goes to the "Your input area". From here I would like it to be INSERTED to the table file.
Link to comment
Share on other sites

It would make sense to put code to insert data in the database after the code that checks if the form was submitted and gathers and validates the input. If there were any validation errors you probably don't want to put it in the database, so check if there were validation errors and insert if not. I would reformat that code so it's easier to read, also. You don't get charged money for having more lines or spaces, feel free to space out and indent your code so it's easy to read.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...