spanner206 Posted November 6, 2013 Share Posted November 6, 2013 hi i want to put a validation in my code so it will check all fields if they have been used ive got the code pretty much sorted but they i think they is one problem with it on line 30 <!DOCTYPE HTML> <html> <head> <style> .error {color: #FF0000;} </style> </head> <body> <? $con = mysqli_connect("","","",""); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } mysqli_query($con,"INSERT INTO tbl_club_contacts (CompanyName, FirstName, Address1, Address2, Area, City); VALUES ('nelsons', 'luke', '', 'IT', '5 HIGHFIELD ROAD', 'LITTLEOVER', 'DERBY')"); mysqli_close($con); // define variables and set to empty values $companynameErr = $FirstNameErr = $Address1Err = $Address2Err = $AreaErr = $CityErr = ""; $companyname = $FirstName = $Address1 = $Address2 = $Area = $City = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") $allValid = true; if(validation fails); { $allValid = false; } if($allValid) { // connect to db, create query, execute query } As for checking the values you could if($_POST['companyname']==null || $_POST['companyname']=="") { $allValid = false; } ?> <form action="insertaddleads.php" method="post"> <p><span class="error">* required field.</span></p> <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> CompanyName: <input type="text" name="companyname"> <span class="error">* <?php echo $CompanyNameErr;?></span> <br><br> FirstName: <input type="text" name="firstname"> <span class="error">* <?php echo $FirstNameErr;?></span> <br><br> Address 1: <input type="text" name="address1"> <span class="error">* <?php echo $Address1Err;?></span> <br><br> Address 2: <input type="text" name="address2"> <span class="error">* <?php echo $Address2Err;?></span> <br><br> Area: <input type="text" name="area"> <span class="error">* <?php echo $AreaErr;?></span> <br><br> City: <input type="text" name="city"> <span class="error">* <?php echo $CityErr;?></span> <br><br> <input type="submit" name="submit" value="Submit"> </form> <? echo $CompanyName; echo "<br>"; echo $FirstName; echo "<br>"; echo $Address1; echo "<br>"; echo $Address2; echo "<br>"; echo $Area; echo "<br>"; echo $City; echo "<br>"; ?> <?php foreach($_POST as $fieldName=>$fieldValue) { if($fieldValue == '') { print "<div>$fieldName is blank</div>"; } } ?> </body> </html> heres the insert just incase <?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> Quote Link to comment https://forums.phpfreaks.com/topic/283647-validation/ Share on other sites More sharing options...
trq Posted November 6, 2013 Share Posted November 6, 2013 but they i think they is one problem with it on line 30 Really? Quote Link to comment https://forums.phpfreaks.com/topic/283647-validation/#findComment-1457169 Share on other sites More sharing options...
spanner206 Posted November 6, 2013 Author Share Posted November 6, 2013 yh sorry Parse error: syntax error, unexpected 'fails' (T_STRING) in C:\wamp\www\AddLeads\addleads2.php on line 30 Quote Link to comment https://forums.phpfreaks.com/topic/283647-validation/#findComment-1457171 Share on other sites More sharing options...
trq Posted November 6, 2013 Share Posted November 6, 2013 if(validation fails); Is not valid php. Quote Link to comment https://forums.phpfreaks.com/topic/283647-validation/#findComment-1457172 Share on other sites More sharing options...
spanner206 Posted November 6, 2013 Author Share Posted November 6, 2013 what should i put in place then?? Quote Link to comment https://forums.phpfreaks.com/topic/283647-validation/#findComment-1457173 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.