muliple inserts

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

muliple inserts

Post 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?
timclaason
Forum Commoner
Posts: 77
Joined: Tue Dec 16, 2003 9:06 am
Location: WI

Query

Post 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.
Last edited by timclaason on Tue Dec 19, 2006 10:17 am, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

just two expand a little, you have two seperate query statements, but only perform one of them.
hrubos
Forum Contributor
Posts: 172
Joined: Sat Oct 07, 2006 3:44 pm

Re: muliple inserts

Post 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());
	}
?>
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Because it's a separator? Take a look at the complex string section:

http://php.net/language.types.string#la ... ng.complex
nhammond
Forum Newbie
Posts: 14
Joined: Mon Dec 18, 2006 11:35 am

Post 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
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Post 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;
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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:
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

function viewServices($category) {
...
return $category;
}

I want to return $check_text

Lets see if we can find the problem
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Post by psurrena »

Sorry, doesn't echo out the list...

Thank you though.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

what does

Code: Select all

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