Jump to content

[SOLVED] For Each & preg_match


bigdspbandj

Recommended Posts

Can anyone see what's wrong here? I am using a regular expression to validate the format of the date. It seems to only return false when the last item in my expected array (adv_seed) is the wrong format. If anything else is in the wrong format my error doesn't show up. None of the db items are being updated either (i've checked column names and everything matches. Please help!!! Thanks :D!

 

//if adv-edit is submitted, update record
if (array_key_exists('advanced_edit', $_POST)) {
//prepare items for insertion into database
foreach ($_POST as $key => $value) {
	if (in_array($key, $adv_expected)) {
		${$key} = mysql_real_escape_string($value);
		$iso_format = preg_match('{[0-9]{4}-[0-9]{2}-[0-9]{2}}', ${$key});
	}
}
if ($iso_format == false) {
	$iso_error = "Please use the provided date format of yyyy-mm-dd";
	$iso_error_main = "There was an error with the dates inputed in the \"Advanced Edit\" area";
}
else {
	$sql = "UPDATE table SET date_new = '$adv_received', date_proofed = '$adv_proofed', date_approved = '$adv_approved', date_printed = '$adv_printed', date_inserted = '$adv_inserted', date_mailed = '$adv_mailed', date_seed = '$adv_seed' WHERE tracking_number = '$tracking_number'";
}
}

Link to comment
Share on other sites

the problem is here // see comments

<?php
foreach ($_POST as $key => $value) {
	if (in_array($key, $adv_expected)) {
		${$key} = mysql_real_escape_string($value);
		$iso_format = preg_match('{[0-9]{4}-[0-9]{2}-[0-9]{2}}', ${$key}); //checks each item
	}
}//loop ends
if ($iso_format == false) { //outside the loop (so only checks the LAST item)
?>

 

May i ask why your looping through the POST ?, do you know the element name you wish to check ?

Link to comment
Share on other sites

The form data is submitted through post. The script checks to see if there is a $_POST with the form data of advanced_edit.

 

It loops through the adv_expected array (which contains the name of each field in the form). It then prepares the data for insertion. If the iso_format is in the wrong format it shouldn't run the query. If the format is ok the query should run.

Link to comment
Share on other sites

Array ( [adv_received] => 2007-09-05 [adv_proofed] => 2007-09-06 [adv_approved] => 0000-00-00 [adv_printed] => 0000-00-00 [adv_inserted] => 0000-00-00 [adv_mailed] => 0000-00-00 [adv_seed] => 2007-10-05 [advanced_edit] => Continue → )

 

That was a good call. How would I go about remedying this?

Link to comment
Share on other sites

ok read comments

 

<?php

//if adv-edit is submitted, update record
if ( isset($_POST['advanced_edit']) )
{
unset($_POST['advanced_edit']); //don't need this 
//prepare items for insertion into database
$iso_format = true;
foreach ($_POST as $key => $value)
{
	if (in_array($key, $adv_expected))
	{
		//Don't Filter yet (will messup the check)
		if( !preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/', $value) )
		{
			//No point continuing, it failed
			$iso_format = false;
			break; 
		}else{
			//Thats OK, set the value for the SQL
			${$key} = mysql_real_escape_string($value);
		}
	}
}
// === with true false value, (good pratice)
if ($iso_format === true) //swapped (just seams right, === true)
{
	$sql = "UPDATE table SET date_new = '$adv_received', date_proofed = '$adv_proofed', date_approved = '$adv_approved', date_printed = '$adv_printed', date_inserted = '$adv_inserted', date_mailed = '$adv_mailed', date_seed = '$adv_seed' WHERE tracking_number = '$tracking_number'";
}else {
	$iso_error = "Please use the provided date format of yyyy-mm-dd";
	$iso_error_main = "There was an error with the dates inputed in the \"Advanced Edit\" area";
}
}

?>

 

of course its untested

 

EDIT:  bug fix

Link to comment
Share on other sites

Few extra words..

i am not sure but i think this line

${$key} = mysql_real_escape_string($value);

may mess it up (on the insert), but i assume your attempting to protect yourself from SQL injections, but you have missed one thing..the fieldnames

maybe try this

$newkey = mysql_real_escape_string($key);
${$newkey} = mysql_real_escape_string($value);

 

or (if the value is failing)

 

$newkey = mysql_real_escape_string($key);
${$newkey} = $value;

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.