Updating database after data verification

I am caught between a rock and a hard place here…
My update page (to update an existing entity in the database) is a normal selection page, which then directs you to the page where you can update or change the data (1st srcipt snippet below), up to here everything works well,
but on submit the page must be directed to a page that verifies the data (2nd script snippet below) and if correct, update the database or redirect to the 2nd page to correct the faulty data entry - easy right (well I have been struggling to get this working for 2 days straight. Pleeaaaaase Help. :shrug:

After the selection page, this is the input page-script:-

$labels = array(“company_descr” => “Description:”);

include("…/includes/xxxxxxxxx.inc"); // database login details
$cxn = mysqli_connect($host,$user,$password,$dbname)
or die (“Couldn’t connect to server.”);
$query = “SELECT * FROM company
WHERE company_id=”{$_POST[‘comp’]}"";

$result = mysqli_query($cxn,$query)
or die (“Cannot execute query.”);
$row = mysqli_fetch_assoc($result);
?>

Update Location <?php echo "

Update Company - \"{$_POST['comp']}\"

\n"; echo "

Please change the field(s) that you want to update below.


\n"; echo "";
foreach($labels as $field => $label)
{
	echo "<div class='field'>
		<label for='$field'>$label</label>
			<input type='text' name='$field' id='$field'
				value='$row[$field]' size='35'
				maxlength='35' /></div>\n";
}
echo "<div id='submit'><input type='submit'
				value='Update' />\n";
echo "</div>\n</form>\n</body>\n</html>";

?>

This is the script that is supposed to verify the data and then send you back to the input page or update the database if the data is correct

foreach($_POST as $field => $value)
{
if(empty($value))
{
$blanks[] = $field;
}
else
{
$good_data[$field] = strip_tags(trim($value));
}
}

	if(isset($blanks))
	{
		$message_2 = "The following fields are blank. 
					Please enter the required information:   ";
		foreach($blanks as $value)
		{
			$message_2 .="$value,  ";
		}
		extract($good_data);
		include("../updateInput/updateInputCompanyTest.php");
		exit();
	}
/* validate data */
	foreach($_POST as $field => $value)
	{
		if(empty($value))
		{
			if(preg_match("/descr/i",$field))
			{
				if(!preg_match("/^[A-Za-z0-9.,' -]{1,50}$/",$value))
				{
					$errors[] = "$value is not a valid address or city.";
				}
			}
		}  // end if not empty
	}
	foreach($_POST as $field => $value)
	{
		$$field = strip_tags(trim($value));
	}
	if(@is_array($errors))
	{
		$message_2 = "";
		foreach($errors as $value)
		{
			$message_2 .= $value." Please try again<br />";
		}
		include("../updateInput/updateInputCompanyTest.php");
		exit();
	}  // end if errors are found
	
	else  // add new member to database
	{
		include("../includes/xxxxxxxx.inc"); // database login details
		$cxn = mysqli_connect($host,$user,$password,$dbname)
			or die ("Couldn't connect to server.");
		$query = "INSERT INTO `company` (`company_descr`) VALUES
				('$company_descr')
				WHERE `company_id`=\"{$_POST['comp']}\"";
		$result = mysqli_query($cxn,$query)
			or die ("Couldn't execute query.");
		header("Location: ../backtomainpage.php");
	}
	include("../updateInputCompanyTest.php");

?>

Well, it looks like you just need to alter your format of your query a little…
Your current code:
$cxn = mysqli_connect($host,$user,$password,$dbname)
or die (“Couldn’t connect to server.”);
$query = “INSERT INTO company (company_descr) VALUES
(’$company_descr’)
WHERE company_id=”{$_POST[‘comp’]}"";
$result = mysqli_query($cxn,$query)
or die (“Couldn’t execute query.”);
Should be like this:
[php]
$cxn = mysqli_connect($host,$user,$password,$dbname)
or die (“Couldn’t connect to server.”);
$query = “INSERT INTO company (company_descr) VALUES
(’” . $company_descr . “’)
WHERE company_id=’” . $_POST[‘comp’] . “’”;
$result = mysqli_query($cxn,$query)
or die (“Couldn’t execute query.”);
[/php]
See if that works correctly…

Sponsor our Newsletter | Privacy Policy | Terms of Service