Can anyone please help.I am trying to add values to a table from 2 dropdown lists which are dynamically poulated.
I have managed to achieve it for one drop list but have added a second and cannot create the correct syntax to add both values to the table.

// Check for a URL.
	if (eregi ('^([[:alnum:]\-\.])+(\.)([[:alnum:]]){2,4}([[:alnum:]/+=%&_\.~?\-]*)$', $_POST['url'])) {
		$u = escape_data($_POST['url']);
	} else {
		$u = FALSE;
		echo '<p><font color="red">Please enter a valid URL!</font></p>';
	}
	
	// Check for a URL title.
	if (!empty($_POST['title'])) {
		$t = escape_data($_POST['title']);
	} else {
		$t = FALSE;
		echo '<p><font color="red">Please enter a URL name/title!</font></p>';
	}
	
// Check for a URL date.
	if (!empty($_POST['pub_date'])) {
		$p = escape_data($_POST['pub_date']);
	} else {
		$p = FALSE;
		echo '<p><font color="red">Please enter a date!</font></p>';
	}

		
	// Check for a description.
	if (!empty($_POST['description'])) {
		$d = escape_data($_POST['description']);
	} else {
		$d = FALSE;
		echo '<p><font color="red">Please enter a description!</font></p>';
	}
	
		// Check for a publisher.
	if (isset($_POST['posters']) && (is_array($_POST['posters']))) {
		$poster = TRUE;
	} else {
		$poster = FALSE;
		echo '<p><font color="red">Please choose a Publisher!</font></p>';
	}
	
	// Check for a category.
	if (isset($_POST['types']) && (is_array($_POST['types']))) {
		$type = TRUE;
	} else {
		$type = FALSE;
		echo '<p><font color="red">Please choose a Client!</font></p>';
	}
		
	if ($u && $t && $d && $p && $type && $poster) { // If everything's OK.
	

		// Add the URL to the urls table.
		$query = "INSERT INTO urls (url, title, description, pub_date) VALUES ('$u', '$t', '$d', '$p')";		
		$result = @mysql_query ($query); // Run the query.
		$uid = @mysql_insert_id(); // Get the url ID.

		if ($uid > 0) { // New URL has been added.
		
			// Make the URL associations.
			

			// Build the query.
			$query = 'INSERT INTO url_associations (url_id, url_category_id, approved, url_publisher_id) VALUES ';
			foreach (($_POST['types'] as $v) AND ($_POST['posters'] as $k)){ //This is line I am having issues with
				$query .= "($uid, $v, 'Y', '$k'), ";
			}
			$query = substr ($query, 0, -2); // Chop off the last comma and space.

			$result = @mysql_query ($query); // Run the query.
			
			if (mysql_affected_rows() == count($_POST['types']) AND ($_POST['posters'])) { // I also suspect this will give me issues too.
			
				echo '<p><b>Thank you for your submission!</b></p>';
				$_POST = array(); // Reset values.
				
			} else { // If second query did not run OK.
			
				echo '<p><font color="red">Your submission could not be processed due to a system error. We apologize for any inconvenience.</font></p>'; // Public message.

Any assistance would be gratefully accepted. Kind regards.

Sean

Recommended Answers

All 7 Replies

Can anyone please help.I am trying to add values to a table from 2 dropdown lists which are dynamically poulated.
I have managed to achieve it for one drop list but have added a second and cannot create the correct syntax to add both values to the table.

// Check for a URL.
	if (eregi ('^([[:alnum:]\-\.])+(\.)([[:alnum:]]){2,4}([[:alnum:]/+=%&_\.~?\-]*)$', $_POST['url'])) {
		$u = escape_data($_POST['url']);
	} else {
		$u = FALSE;
		echo '<p><font color="red">Please enter a valid URL!</font></p>';
	}
	
	// Check for a URL title.
	if (!empty($_POST['title'])) {
		$t = escape_data($_POST['title']);
	} else {
		$t = FALSE;
		echo '<p><font color="red">Please enter a URL name/title!</font></p>';
	}
	
// Check for a URL date.
	if (!empty($_POST['pub_date'])) {
		$p = escape_data($_POST['pub_date']);
	} else {
		$p = FALSE;
		echo '<p><font color="red">Please enter a date!</font></p>';
	}

		
	// Check for a description.
	if (!empty($_POST['description'])) {
		$d = escape_data($_POST['description']);
	} else {
		$d = FALSE;
		echo '<p><font color="red">Please enter a description!</font></p>';
	}
	
		// Check for a publisher.
	if (isset($_POST['posters']) && (is_array($_POST['posters']))) {
		$poster = TRUE;
	} else {
		$poster = FALSE;
		echo '<p><font color="red">Please choose a Publisher!</font></p>';
	}
	
	// Check for a category.
	if (isset($_POST['types']) && (is_array($_POST['types']))) {
		$type = TRUE;
	} else {
		$type = FALSE;
		echo '<p><font color="red">Please choose a Client!</font></p>';
	}
		
	if ($u && $t && $d && $p && $type && $poster) { // If everything's OK.
	

		// Add the URL to the urls table.
		$query = "INSERT INTO urls (url, title, description, pub_date) VALUES ('$u', '$t', '$d', '$p')";		
		$result = @mysql_query ($query); // Run the query.
		$uid = @mysql_insert_id(); // Get the url ID.

		if ($uid > 0) { // New URL has been added.
		
			// Make the URL associations.
			

			// Build the query.
			$query = 'INSERT INTO url_associations (url_id, url_category_id, approved, url_publisher_id) VALUES ';
			foreach (($_POST['types'] as $v) AND ($_POST['posters'] as $k)){ //This is line I am having issues with
				$query .= "($uid, $v, 'Y', '$k'), ";
			}
			$query = substr ($query, 0, -2); // Chop off the last comma and space.

			$result = @mysql_query ($query); // Run the query.
			
			if (mysql_affected_rows() == count($_POST['types']) AND ($_POST['posters'])) { // I also suspect this will give me issues too.
			
				echo '<p><b>Thank you for your submission!</b></p>';
				$_POST = array(); // Reset values.
				
			} else { // If second query did not run OK.
			
				echo '<p><font color="red">Your submission could not be processed due to a system error. We apologize for any inconvenience.</font></p>'; // Public message.

Any assistance would be gratefully accepted. Kind regards.

Sean

Hmmm I don't see where you are actually inserting it into a database of any type.

There are a few errors where you run an IF statement you used AND instead of && I have been told that AND works but I prefer to use the && link to link to statements.

Also I noticed you were checking if a drop down box was an array, as far as I know I have never had a drop down box (<select>) post an array.

You might want to change your logic a little and run a query to add the drop down boxes to the database.

I might be wrong my head has been a little off today.


There are a few errors where you run an IF statement you used AND instead of && I have been told that AND works but I prefer to use the && link to link to statements.

Also I noticed you were checking if a drop down box was an array, as far as I know I have never had a drop down box (<select>) post an array.

QUOTE]

Hi Josh,

The insert is this section

// Build the query.
			$query = 'INSERT INTO url_associations (url_id, url_category_id, approved, url_publisher_id) VALUES ';
			foreach (($_POST['types'] as $v) AND ($_POST['posters'] as $k)){
				$query .= "($uid, $v, 'Y', '$k'), ";
			}
			$query = substr ($query, 0, -2); // Chop off the last comma and space.

			$result = @mysql_query ($query); // Run the query.
			
			if (mysql_affected_rows() == count($_POST['types']) AND ($_POST['posters'])) { // Query ran OK.
			
				echo '<p><b>Thank you for your submission!</b></p>';
				$_POST = array(); // Reset values.
				
			} else { // If second query did not run OK.

The line: foreach (($_POST as $v) AND ($_POST as $k)) if it reads foreach (($_POST as $v) adds the values of the category ID to the association table, but when I try the AND to add the other drop down list it throws errors parsing the line.

I have tried a few variations to get this extra drop list to give me a value to then pass the id number into the association table but without any luck.

Thank you for you time and advice Josh.

Sean

Hi Josh, I got it sorted out.
By adding another foreach array it added the second drop list id int to the association table.
Thanks for your help.

No problem

No Problem, and a pleausre no doubt, but it's important that you also know that Thanks for your generosity is sincere.
Ddiolch yn Fawr Iawn Josh.
[Welsh for Many Thanks]

Sean

Ahhh a weksh boy are you? Not far from wales myself.

And we are here to help, always are. It's great to see people come and go knowing that they leave with a bit of gained knowledge.

Ahh dai iawn i chi,
I am not leaving, and not just with a bit of knowledge, more a jolt of logic.
Thanks for the <9 size logic shoe!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.