Jump to content

validation


spanner206

Recommended Posts

Hi ive been trying to do this all day, what i want to do is make a form validatation where it checks if all fields are filled but i cant seem to hack it ive checked w3 schools but i just cant seem to hack it at one point today i got it but it waent putting data into a database. 

 

heres the main document

<?php
$con=mysqli_connect("","","","");
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
 
$sql="INSERT INTO tbl_club_contacts (CompanyName)
VALUES
('$_POST[CompanyName]')";
 
if (!mysqli_query($con,$sql))
  {
   die('Error: ' . mysqli_error($con));
  }
echo "record added";
 
mysqli_close($con);
?>
<html>
<body>
 
<form action="copyofaddleads2.php" method="post">
<input type="submit", value = "go back">
</form>
 
</body>
</html>
 
heres the insert
<?php
$con=mysqli_connect("","","","");
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
 
$sql="INSERT INTO tbl_club_contacts (CompanyName, FirstName, Address1, Address2, Area, City)
VALUES
('$_POST[companyname]','$_POST[firstname]','$_POST[address1]','$_POST[address2]','$_POST[area]','$_POST[city]')";
 
if (!mysqli_query($con,$sql))
  {
   die('Error: ' . mysqli_error($con));
  }
echo "record added";
 
mysqli_close($con);
?>
<html>
<body>
 
<form action="addleads2.php" method="post">
<input type="submit", value = "go back">
</form>
 
</body>
</html>

 

Link to comment
Share on other sites

If all the form fields are required, you could loop through the POST array.

 

 

<?php
foreach($_POST as $fieldName=>$fieldValue) {
     if($fieldValue == '') {
          print "<div>$fieldName is blank</div>";
     }
}
?>

 

 

Side note: you'll also want to look into protecting your queries from SQL injections. One way is to use mysqli_real_escape_string():

http://php.net/manual/en/mysqli.real-escape-string.php

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.