Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Validate form then update table

Featured Replies

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 by El Nino

  • 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 :-(

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

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

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 by pat24

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

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 by pat24

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.