MySqli problem with loop!

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

Post Reply
mekha
Forum Contributor
Posts: 112
Joined: Sat Mar 31, 2012 6:50 am

MySqli problem with loop!

Post by mekha »

...why is my loop dont work:

Code: Select all

for($i=0; $i<sizeof($someArray); $i++)
{
$sql9 = insertToDb();
				if ($result9 = $mysqli->prepare($sql9)) 
				{
				$result9 -> bind_param("ii", $intval, $intval2); 
				$result9->execute();	
				$result9->store_result();
				$rows9 = $result9->num_rows;
				}
}

to pay attetntion: the array is working good..because if i echo the array....het gets me the right output.
Problem: the result must insert in the database multiple times...(<$someArray)...
for example: echo sizeof($someArray)=> 3 ..... the function insert into the databse only once...and not 3 times!
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: MySqli problem with loop!

Post by social_experiment »

Even though you are looping x times, the inserting is determined by the result of this conditional statement

Code: Select all

 if ($result9 = $mysqli->prepare($sql9)) 
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
mekha
Forum Contributor
Posts: 112
Joined: Sat Mar 31, 2012 6:50 am

Re: MySqli problem with loop!

Post by mekha »

si it must be like this:

Code: Select all

for($i=0; $i<sizeof($someArray); $i++)
{
                                $sql9 = insertToDb();
                                $result9 = $mysqli->prepare($sql9)
                                $result9 -> bind_param("ii", $intval, $intval2); 
                                $result9->execute();    
                                $result9->store_result();
                                $rows9 = $result9->num_rows;               
}
?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: MySqli problem with loop!

Post by social_experiment »

What happens when you modify the code to the sample you gave?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
mekha
Forum Contributor
Posts: 112
Joined: Sat Mar 31, 2012 6:50 am

Re: MySqli problem with loop!

Post by mekha »

adding only 1 value!.....
maybe the "$result" must get different name? like
$result.$i .... but not working :S...i mean for every time...the result name changes to the number of loop
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: MySqli problem with loop!

Post by social_experiment »

mekha wrote:maybe the "$result" must get different name? like
I don't think this is the problem; each time the loop is executed, all variables in the loop gets assigned a new value
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
mekha
Forum Contributor
Posts: 112
Joined: Sat Mar 31, 2012 6:50 am

Re: MySqli problem with loop!

Post by mekha »

do you have some idea's to do :S?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: MySqli problem with loop!

Post by social_experiment »

What results do you get when echoing the query to the browser;

Code: Select all

<?php    
    echo $sql9 . '<br />';
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
mekha
Forum Contributor
Posts: 112
Joined: Sat Mar 31, 2012 6:50 am

Re: MySqli problem with loop!

Post by mekha »

ok...i understood my problem but i do not how to solve!!...

before every thing...this is my full code:

Code: Select all

$sqlw = getAllSubjectsInDbByCatId();
				if ($resultw = $mysqli->prepare($sqlw)) 
				{
					$resultw->bind_param("i",$cat);
					$resultw->execute();	
					$resultw->store_result();
					$rowsw = $resultw->num_rows;
				}
					$countw=0; 
					$countw2=0;// count 
					for($mi=1 ; $mi<$rowsw+1; $mi++)
					{
						$namePOw = "cat_".$mi;
						if($_POST[$namePOw]!="")
						{
							$arrActivew[$countw] = $_POST[$namePOw];
							echo $arrActivew[$countw]."<br>";
							
							/*++++++++++*/
								$sql = getCategoriesSsubjectsById();
								if ($result = $mysqli->prepare($sql)) 
								{
									$rekza = $arrActivew[$countw];
									$result->bind_param("i",$rekza);
									$result->execute();	
									$result->store_result();
									$rowsZ = $result->num_rows;
								}
								$countw++;
								
								for($re=1 ; $re<$rowsZ+1; $re++)
								{
									$namePOw2 = "cat2_".$re;
									if($_POST[$namePOw2]!="")
									{
										$arrActivew2[$countw2] = $_POST[$namePOw2];
										$countw2++;
									}
									
									
								}
							/*++++++++++*/		
							
						}
						
					}
				
			
			
			$sql = insertConSub();
			if($result = $mysqli->prepare($sql)) 
			{
			$result -> bind_param("iiii", $id,$cat,$subject,$subject_sub);
				for($ik = 1; $ik <sizeof($arrActivew2)+1; $ik++) 
				{
				
					$subject = (int)$arrActivew[$ik-1];
					$subject_sub = (int)$arrActivew2[$ik-1];
					
					// Execute the prepared Statement
					$result->execute();	
						
				}
					
				// Close the statement 
		    	        $result->close(); 				
			}

the problem:
the size of arrActivew and arrActivew2 is not the same...
i mean:

Main Cat 1
sub cat 1 | sub cat 2 | sub cat 3
--
Main Cat 2
sub cat 1 | sub cat 2 | sub cat 3
--------
now, when i check Main cat 1 amd check sub cat 1 sub cat 2 from it.....and check Main Cat 2 and check sub cat 1...
the database inserts:
1- Sub cat 1,Main cat 1
2- Sub cat 2,0 (the zero is Main cat 1 because he ran on it before)
3-Sub cat 1 , Main cat 2..
i wish you understood me
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: MySqli problem with loop!

Post by social_experiment »

mekha wrote:the size of arrActivew and arrActivew2 is not the same...
From the code you pasted i see that the value of these two variables are determined by input from a form, can you explain what these values do, how they fit into the script
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
mekha
Forum Contributor
Posts: 112
Joined: Sat Mar 31, 2012 6:50 am

Re: MySqli problem with loop!

Post by mekha »

this is a Main cat , Sub cat , and SubSub cat.....its in ajax and php list...check box for main=> get the div of sub cat and choose it in check box and get the div of the SubSub cat....now the Main cat no problems......and the SubSubcat no problems....the problem is in the Subcat....it inserted only once in the database...i need for all the SubSub cat that bellow the the Subcat in the database to insert Subcat of him (the subcat here like the parent of subsub cat)...and nt subcat for the first one!!....i need it if i check subcat that have id 5...to insert for all the subsubcats that bellow to this id 5.....if i Subcat...and choose 3 subsubcat bellow to these sub cat... to insert:
subsubcat1,5
subsubcat2,5
subsubcat3,5
and not
subsubcat1,5
subsubcat2,0
subsubcat3,0
..wish you understood my description
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: MySqli problem with loop!

Post by social_experiment »

mekha wrote:..wish you understood my description
:) the explanation isn't the problem; it's bringing what you're telling me and the code together.

What code inserts the sub-sub-category into the database?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
mekha
Forum Contributor
Posts: 112
Joined: Sat Mar 31, 2012 6:50 am

Re: MySqli problem with loop!

Post by mekha »

ok man i solved it ....thank you very much...i just made loop into loop into loop :D
Post Reply