April 19, 201115 yr Hi, I want to validate a form and then update a mysql table. Specifically, I want to collect customer details, check they've entered a correct telephone number and then update my database. My code checks the validation successfully but doesn't update the table. Help!?... <?php { $server="localhost"; $user="user"; $password="password"; $database="contactinfo"; mysql_connect($server,$user,$password,$database) or die("Cannot connect to mysql."); mysql_select_db($database) or die("Cannot connect to database."); if ($_POST['submit']) if (preg_match('/^[0-9]{11}$/', $_POST['Telnumber'])) { $sql="INSERT INTO customer_details (Name, Address, Postcode, Telnumber) VALUES ('$_POST[Name]','$_POST[Address]','$_POST[Postcode]','$_POST[Telnumber]')"; print "<meta http-equiv=\"refresh\" content=\"0;URL=thanks.php\">"; } else{ print "<p>Please ensure you have entered the correct Telephone Number.</p>"; } } ?> Form is here but this is working OK... Edited April 19, 201115 yr by El Nino
April 20, 201115 yr Author Have you tried removing the preg match match condition first? Also, as you have a field name called 'name' you shoudl surround the field name in ` (can't rember what the symbol is called): INSERT INTO customer_details (`Name`, `Address`, `Postcode`, `Telnumber`) Just tried removing the preg but still doesn't work. Also tried inserting ' around column names but still no luck :-(
April 20, 201115 yr Just tried removing the preg but still doesn't work. Also tried inserting ' around column names but still no luck :-( Can I see the form
April 20, 201115 yr Author Can I see the form Yes, ofcourse, here is all of the code... <?php { $server="localhost"; $user="user"; $password="password"; $database="contactinfo"; mysql_connect($server,$user,$password,$database) or die("Cannot connect to mysql."); mysql_select_db($database) or die("Cannot connect to database."); if ($_POST['submit']) if (preg_match('/^[0-9]{11}$/', $_POST['Telnumber'])) { $sql="INSERT INTO customer_details (Name, Address, Postcode, Telnumber) VALUES ('$_POST[Name]','$_POST[Address]','$_POST[Postcode]','$_POST[Telnumber]')"; print "<meta http-equiv=\"refresh\" content=\"0;URL=thanks.php\">"; } else{ print "<p>Please ensure you have entered the correct Telephone Number.</p>"; } } ?> <p>Enter your details into the following form and click the confirm button.</p> <form method="post" action="checkoutform.php"> <fieldset> <legend>Enter Your Details:</legend> <label for="Name">Name: </label> <input type="text" name="Name" id="Name" /> <br> <label for="Number">Address:</label> <input type="text" name="Address" id="Address" /> <br> <label for="Postcode">Post Code:</label> <input type="text" name="Postcode" id="Postcode" /> <br> <label for="Telnumber">Telephone Number:</label> <input type="text" name="Telnumber" id="Telnumber" /> <br> <input type="submit" name="submit" value="Confirm" class="submit-button" /> </fieldset> </form> </div>
April 20, 201115 yr Can you try this <?php { $server="localhost"; $user="user"; $password="password"; $database="contactinfo"; mysql_connect($server,$user,$password,$database) or die("Cannot connect to mysql."); mysql_select_db($database) or die("Cannot connect to database."); if ($_POST['submit']) if (preg_match('/^[0-9]{11}$/', $_POST['Telnumber'])) { $sql="INSERT INTO customer_details (Name, Address, Postcode, Telnumber) VALUES ('$_POST[Name]','$_POST[Address]','$_POST[Postcode]','$_POST[Telnumber]')"; if (mysql_query($sql)) print "<meta http-equiv=\"refresh\" content=\"0;URL=thanks.php\">"; } else{ print "<p>Please ensure you have entered the correct Telephone Number.</p>"; } } ?> Edited April 20, 201115 yr by pat24
April 20, 201115 yr Author Thanks for your replies on this... Unfortunately, didn't work... I get "Please ensure you have entered the correct Telephone Number" when I enter it correctly or incorrectly and obv doesn't update the table...
April 20, 201115 yr Thanks for your replies on this... Unfortunately, didn't work... I get "Please ensure you have entered the correct Telephone Number" when I enter it correctly or incorrectly and obv doesn't update the table... It works my end perfectly Edited April 20, 201115 yr by pat24
April 20, 201115 yr Author It works my end perfectly Actually, it does work!! Thanks very much for your help. Very much appreciated!!
Create an account or sign in to comment