Page 1 of 2

muliple inserts

Posted: Tue Dec 19, 2006 10:06 am
by psurrena

Code: Select all

<?php
	if(isset($_POST['add'])) {
		$data = '<li>' . implode('<li>' , $_POST['office_requirements']);
		$query = "INSERT INTO checkboxes_final (c_id, check_text, check_cat) VALUES ('$c_id', '$data', 'office_requirements')";

		$data_b = '<li>' . implode('<li>' , $_POST['membership_database']);
		$query_b = "INSERT INTO checkboxes_final (c_id, check_text, check_cat) VALUES ('$c_id','$data_b', 'membership_database')";

		mysql_query($query) or die (mysql_error());
	}
?>
The above code does will only insert $data but not $data_b. The arrays are from checkboxes, which work, and it's very importtant the category is correct for each one. $data must have 'office_requirements' in the same row.

How do I get both of these querys to work?

Query

Posted: Tue Dec 19, 2006 10:07 am
by timclaason

Code: Select all

mysql_query($query_b) or die (mysql_error());
Or if you want to be a little cleaner:

Code: Select all

if(isset($_POST['add'])) {
                $data = fixInput( $_POST['office_requirements']);
                addToFinal($cid, $data, office_requirements);
                $data_b = fixInput($_POST['membership_database']);
                addToFinal($cid, $data_b, membership_database);
        }
Then make functions:

Code: Select all

function addToFinal($cid, $textToAdd, $section) {
    $query = "INSERT INTO checkboxes_final(cid, check_text, check_cat) VALUES('$cid', '$textToAdd', '$section')";
    mysql_query($query) or die (mysql_error());
}

function fixInput($theInput) {
    $theInput = '<li>' . implode('<li>', $theInput);
    return $theInput;
}
The fixInput method could also allow you to prevent bad data from going into your database, if you were so inclined to modify it.

Posted: Tue Dec 19, 2006 10:16 am
by John Cartwright
just two expand a little, you have two seperate query statements, but only perform one of them.

Re: muliple inserts

Posted: Tue Dec 19, 2006 10:19 am
by hrubos

Code: Select all

<?php
	if(isset($_POST['add'])) {
		$data = '<li>' . implode('<li>' , $_POST['office_requirements']);
		$query = "INSERT INTO checkboxes_final (c_id, check_text, check_cat) VALUES ('$c_id', '$data', 'office_requirements')";

		$data_b = '<li>' . implode('<li>' , $_POST['membership_database']);
		$query_b = "INSERT INTO checkboxes_final (c_id, check_text, check_cat) VALUES ('$c_id','$data_b', 'membership_database')";

		mysql_query($query) or die (mysql_error());
                mysql_query($queryb) or die (mysql_error());
	}
?>

Posted: Tue Dec 19, 2006 11:40 am
by psurrena
Thanks! I really like how it works with the functions - very clean.

One question, why does it not work with c_id but will with cid?

Posted: Tue Dec 19, 2006 11:42 am
by feyd
Because it's a separator? Take a look at the complex string section:

http://php.net/language.types.string#la ... ng.complex

Posted: Tue Dec 19, 2006 11:58 am
by nhammond
psurrena wrote:Thanks! I really like how it works with the functions - very clean.

One question, why does it not work with c_id but will with cid?
You need to see what your field and variables are called and name them appropriately. There is a difference between c_id and cid just like there is a difference between 'pancake' and 'syrup'.
Your first set of names where it says....INSERT INTO checkboxes_final (c_id, check_text, check_cat) --> these are the field names in the table 'checkboxes_final' in your database
VALUES ('$c_id','$data_b', 'membership_database') -->These are all of your variable names that you are getting from php

Posted: Tue Dec 19, 2006 4:13 pm
by psurrena
I just tried to modify one of the functions above to retrieve results based on category:

Code: Select all

function viewservices($category){
   $query = "SELECT * FROM checkboxes_final WHERE cid='$cid' AND check_cat='$category'";
   $result = mysql_query($query);
   mysql_query($query) or die (mysql_error());
}
and here is where it's called

Code: Select all

viewservices("office_requirements");

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {	
	$check_text = $row['check_text'];
	echo $check_text;
}
Any help or direction?

Posted: Tue Dec 19, 2006 4:27 pm
by John Cartwright
Firstly, why are you performing your query twice in your function?

Secondly, $result is defined in the function, and is not in the same scope when you are trying to access it outside of the function. Have a look at http://www.php.net/manual/en/language.v ... .scope.php

Code: Select all

function viewservices($category){
   $query = "SELECT * FROM checkboxes_final WHERE cid='$cid' AND check_cat='$category'";
   return mysql_query($query);
} 

$result = viewservices("office_requirements");

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {       
        $check_text = $row['check_text'];
        echo $check_text;
}
Should work. Enjoy. Also, you should take a look at mysql_real_ecape_string(). At minimum, any data coming from the user should be passed through this function to avoid sql injection.

Posted: Tue Dec 19, 2006 10:10 pm
by psurrena
Ok, I appreciate all this help and when I think I have it - I thought wrong:

Everything between function() {} works alone but when I use it as a function, it just returns "office_requirements" and not the list items.

HELP!

Code: Select all

function viewServices($category) {
	$query = "SELECT * FROM checkboxes_final WHERE cid='$cid' AND check_cat='$category'";
	$result = mysql_query($query) or die (mysql_error() . 'Cannot Retrieve Information');

	while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
		$check_text = $row['check_text'];
		echo $check_text;
	}
	return $category;
}

$data = viewServices('office_requirements');
	
echo $data;

Posted: Tue Dec 19, 2006 10:13 pm
by John Cartwright
look at it carefully.. which variable are you returning? Also, are you only expecting a single row? If so you might want to add LIMIT 1 on your query.. just in case. :wink:

Posted: Tue Dec 19, 2006 10:25 pm
by psurrena
I want to return $check_text

This project is set up like so:
100 check boxes in 5 or so categories. The user checks which ones they want to appear in the final document under the approriate category via seperate queries. In

Code: Select all

viewServices('office_requirements');
office_requirements is the category.


I know I could do this by just running the query for each category but it would be cumbersome and I wanted to learn a more efficient way.

Posted: Tue Dec 19, 2006 10:35 pm
by John Cartwright
function viewServices($category) {
...
return $category;
}

I want to return $check_text

Lets see if we can find the problem

Posted: Tue Dec 19, 2006 10:52 pm
by psurrena
Sorry, doesn't echo out the list...

Thank you though.

Posted: Tue Dec 19, 2006 10:58 pm
by John Cartwright
what does

Code: Select all

echo mysql_num_rows($result);
inside the function yield?