Hi everyone,
I’ve finally bitten the bullet and started to learn PHP! As expected, I’m hitting some road blocks along the way but hope you can help
I have a form in which the user will be able to enter 10x numeric IDs and 10x alphanumeric Titles.
The issue:
I want only the values that pass validation to be saved to my database, but at the moment, even if my values don’t pass validation, they’re still saved to my database.
Here’s the code (the HTML and PHP is on one page and the form submission is being captured with REQUEST_METHOD). I’m just validating the first ID and first Title to start with (test of concept before replicating code):
[php]
$ID1Err = $Title1Err = “”;
$ID1 = $Title1 = “”;
// form validation
if (empty($_POST[‘ID1’])) {
$ID1Err = “
please enter an ID”;
}
elseif (!is_numeric($_POST[‘ID1’])) {
$ID1Err = “
numbers only”;
}
else {
$ID1 = mysqli_real_escape_string($con, $_POST[‘ID1’]);
}
if (empty($_POST['Title1'])) {
$Title1Err = "<br /><span class='error'>please enter a Title</span>";
}
else {
$Title1 = mysqli_real_escape_string($con, $_POST['Title1']);
}
// add form field values to database
$sql=“INSERT INTO ids_titles (ID, Title) VALUES (’$ID1’, ‘$Title1’), (’$ID2’, ‘$Title2’), (’$ID3’, ‘$Title3’), (’$ID4’, ‘$Title4’), (’$ID5’, ‘$Title5’), (’$ID6’, ‘$Title6’), (’$ID7’, ‘$Title7’), (’$ID8’, ‘$Title8’), (’$ID9’, ‘$Title9’), (’$ID10’, ‘$Title10’)”;
[/php]
I’ve got a funny feeling this is a rookie error but the simple solution is escaping me (no PHP-pun intended!). At a guess I’d say I need to populate the SQL values with only the form field variables that pass validation?