Jump to content

foreach values in sql query


tealcsg
 Share

Recommended Posts

Hi,I need help.I heave a foreach loop and I need to use values of that loop in a sql query.foreach loop looks like this:

foreach($kolicina as $keyKol=>$kol){	echo $keyKol." - ".$kol."<br/>";}

and result is:65 - 1566 - 1067 - 2068 - 3069 - 1182 - 1that is OKMy problem is how to make a sql query wich will be executed for every foreach value.I tried this:

$sql="UPDATE narudzbenica SET kolicina='".$kol."' WHERE broj_narudzbe='".$keyKol."'";

but doesn't work.This query update just last foreach value (kolicina='1' .... broj_narudzbe='82'), wich is not good. I need all iterations to be extcuted, but I don't know how.I hope that you will understand what i want to do.If don't please ask.Thanks in advanced

Link to comment
Share on other sites

I did not understand anything of what you are telling.Can you tell me what is $kolicina and $keyKol, and explain what do you want?If you want to get the result rows as an array, this function can help you:

function mysql_get_array($q){	$x=array();	$n=mysql_num_rows($q);	$i=0;	while ($r = mysql_fetch_array($q))	{		if ($i>=$n)		break;		$x[$i]=$r;		$i++;	}	return $x;}

Link to comment
Share on other sites

Before all this there is a input form.User edit few values named kolicina (translated = amount or quantity) end send changes.In foreach loop is array ($kolicina) and array key (as $kolKey=>$kol).I need those variables in my sql query (but all iterations, not just one).As $sql says UPDATE table_name (narudzbenica) SET quantity (kolicina)(a cell in a table) = 'one iterations of foreach variable $kol' WHERE id_orders (broj_narudzbe)(a row in a table) = 'one iterations of foreach varable $keyKol'.

foreach($kolicina as $keyKol=>$kol){	echo $keyKol." - ".$kol."<br/>";$sql="UPDATE narudzbenica SET kolicina='".$kol."' WHERE broj_narudzbe='".$keyKol."'";}

And repeate query as long as there are foreach iterations.I need help with building a query, because this query does not work.Is it better now? Because my english is not very good.

Link to comment
Share on other sites

probably you querying your sql outside of your foreach loop..though i am not sure about that from your code which you had provided. posting the whole code would be much better.

Link to comment
Share on other sites

Here it is:

require_once('../../Connections/konekcija.php');$id_narudzbenica=$_GET['id_narudzbenica'];$id_nl=$_POST['id_nl'];$broj_narudzbe=$_POST['broj_narudzbe'];$kolicina = $_POST['kolicina'];$status=$_POST['status'];$otpremnica=$_POST['otpremnica_br'];$br_otp_prema_ht=$_POST['br_otp_prema_ht'];foreach($kolicina as $keyKol=>$kol){	echo $keyKol." - ".$kol."<br/>";$sql="UPDATE narudzbenica SET kolicina='".$kol."' WHERE broj_narudzbe='".$keyKol."'";}if (mysql_query($sql)){if (mysql_affected_rows() > 0 ){echo "<br/><br/><br/><br/><table width=\"100%\" border=\"1\" cellspacing=\"0\" cellpadding=\"2\" align=\"center\">  <tr>	<td align=\"center\" height=\"25\" align=\"center\">Promjene narudžbenice su uspješno spremljene.<br><img src='../slike/ok.png'><br/><a href=\"#\" onclick=\"java script:window.close()\">  <input type=\"button\" name=\"zatvori\" id=\"zatvori\" value=\"zatvori\" /></a></td>  </tr></table>";} else {echo "<br/><br/><br/><br/><table width=\"100%\" border=\"1\" cellspacing=\"0\" cellpadding=\"2\" align=\"center\">  <tr>	<td align=\"center\" height=\"25\" align=\"center\">Narudžbenica nije izmjenjena - nema promjena.<br/><img src='../slike/ok_egal.png'><br/><a href=\"#\" onclick=\"java script:window.close()\">  <input type=\"button\" name=\"zatvori\" id=\"zatvori\" value=\"zatvori\" /></a></td>  </tr></table>";}} else {echo "<br/><br/><br/><br/><table width=\"100%\" border=\"1\" cellspacing=\"0\" cellpadding=\"2\" align=\"center\">  <tr>	<td align=\"center\" height=\"25\" align=\"center\">Nastala je greška pri izmjeni narudžbenice.<br><img src='../slike/ok_nok.png'><br/><a href=\"#\" onclick=\"java script:window.close()\">  <input type=\"button\" name=\"zatvori\" id=\"zatvori\" value=\"zatvori\" /></a></td>  </tr></table>" . mysql_error();}

Link to comment
Share on other sites

put the mysql_query and other things inside the foreach loop.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...