November 14, 201213 yr Hello, im trying to make a simple write to database form. I have made the database and the table. Here is the form: <form name="input" action="write.php" method="post"> <input class="textbox" type="text" name="email" value="Enter Your Email" onfocus="this.value=''"> <input class="button" type="submit" value="Enter Competition"> </form> this is write.php: <?php $con = mysql_connect("localhost","user","pass"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("win2012", $con); if (empty($Email)) { echo '<div id="errormessage">', "Email box cannot be empty. Please fill in.", '</div>'; } // This checks the email is valid if (!filter_var($Email, FILTER_VALIDATE_EMAIL)) { echo '<div id="errormessage">', "$Email is not a valid email, please correct any errors.", '</div>'; } //Now insert the clean data if (!empty($Email) && (filter_var($Email, FILTER_VALIDATE_EMAIL))) { $sql="INSERT INTO win2012 (email) VALUES ('$Email')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo '<div id="successmessage">', "Success! Suggestion Sent.", '</div>'; mysql_close($con); } ?> But i keep getting the error messages... and nothing is writing to db etc. I used some of old cold i had but cant get it working...
November 14, 201213 yr I don't know much PHP but don't you need to actually take the form input? e.g. $Email = $_POST['email'];
November 14, 201213 yr Yes you need to take the value from the email textbox $Email = $_POST['email'];.I think you didn't take that value
November 14, 201213 yr Your code won't work for a number of reasons. Firstly the above 2 replies are correct - to access form data use the $_POST global variable using the name of the field. If you've named it email, use $_POST['email']. You need to check for its existence before you can access it. Try something like if (isset($_POST['email']) && (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL))) { $em = $_POST['email']; $sql="INSERT INTO win2012 (email) VALUES ($em)"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } else { echo '<div id="successmessage">', "Success! Suggestion Sent.", '</div>'; } mysql_close($con); } else { echo "<div id=\"errormessage\">Please enter a valid email address</div>"; } Also you should switch to the mysqli extension as mysql is now deprecated.
Create an account or sign in to comment