UnlikePC Posted January 6, 2011 Report Share Posted January 6, 2011 Isuma sakot vajag php skriptu kas uzraksta kveriju pec WHERE ...... Skripts elementars bet gruts. Ceru ka kads spees palidzet. <?php $db_host = "localhost"; $db_user = "root"; $db_passw ="xxx"; $db_db = "autotirdznieciba"; @mysql_connect("$db_host","$db_user","$db_passw") or die("Nevar pieslegties pie mysql servera"); @mysql_select_db("$db_db") or die("Nevar atvert db"); $Marka = $_POST['Marka']; $Motors = $_POST['Motors']; $Dzinejs = $_POST['Dzinejs']; $Virsbuve = $_POST['Virsbuve']; // Te vajag skriptu kaa sataisit, ja kads no dotajiem ir tukss /* Piemeram: Ja vertiba $Marka,$Motors,$Virsbuve ir pilni bet $Dzinejs tukss tad php skripts izveido ssadu kveriju SELECT * FROM 'tabula' WHERE Marka = "$Marka" AND Motors = "$Motors" Virsbuve = "$Virsbuve" Bet ja vertiba $Marka,$Dzinejs ir pilni bet $Motors,$Virsbuve tukss tad ssadu kveriju SELECT * FROM 'tabula' WHERE Marka = "$Marka" AND Dzinejs = "$Dzinejs" bet ja ir aizpildita tikai vertiba $Motors tad: SELECT * FROM 'tabula' WHERE Motors = "$Motors" */ // Te apaksaa ja visi 4 lauki aizpilditi. Bet vajag pirmstam skriptu kas sataisa savadaku queryu ja kads no dotajiem ir tukss $qry = "SELECT * FROM 'auto' WHERE Marka = '$Marka' AND Motors = '$Motors' AND Dzinejs = '$Dzinejs' AND Virsbuve = '$Virsbuve'"; $result = @mysql_query($qry); ?> Quote Link to comment Share on other sites More sharing options...
briedis Posted January 6, 2011 Report Share Posted January 6, 2011 (edited) $a = "xxx"; $b = "yyy"; $z = false; $where = array("1"); //Lai ir vismaz viens nosacījums iekš where. if($a){ $where[] = "a = '$a'"; } if($b){ $where[] = "b = '$b'"; } if($c){ $where[] = "c = '$c'"; } $query = "SELECT * FROM WHERE " . implode(" AND ", $where); Ieteiktu izvairīties arī no SQL injekcijām. Edited January 6, 2011 by briedis Quote Link to comment Share on other sites More sharing options...
UnlikePC Posted January 6, 2011 Author Report Share Posted January 6, 2011 bet ja ir tikai viena vertiba pilna, bet parejas nee. Tad kaa darit? Quote Link to comment Share on other sites More sharing options...
briedis Posted January 6, 2011 Report Share Posted January 6, 2011 bet ja ir tikai viena vertiba pilna, bet parejas nee. Tad kaa darit? Vispār izmēģināji to, ko rakstīju? Quote Link to comment Share on other sites More sharing options...
UnlikePC Posted January 6, 2011 Author Report Share Posted January 6, 2011 Man izmet visus ierakstus kas ir tabulaa Esmu izmainijis php sitaa : <?php $db_host = "localhost"; $db_user = "root"; $db_passw ="0000"; $db_db = "autotirdznieciba2"; @mysql_connect("$db_host","$db_user","$db_passw") or die("Nevar pieslegties pie mysql servera"); @mysql_select_db("$db_db") or die("Nevar atvert db"); $Marka = "Audi"; $Motors = "2.0"; $Dzinejs = false; $Virsbuve = "kupeja"; $where = array(); if($Marka){ $where[] = "Marka = '$Marka'"; } if($Motors){ $where[] = "Motors = '$Motors'"; } if($Dzinejs){ $where[] = "Dzinejs = '$Dzinejs'"; } if($Virsbuve){ $where[] = "Virsbuve = '$Virsbuve'"; } $query = "SELECT * FROM auto WHERE " . implode(" AND ", $where); $result = @mysql_query($query); $result=mysql_query("select * from auto"); while($row=mysql_fetch_array($result)){ ?> <tr> <td><img src="<?=$row['bilde']?>" width="140px" height="100px"></td> <td><?=$row['Marka']?></td> <td><?=$row['Modelis']?></td> <td><?=$row['Motors']?></td> <td><?=$row['Dzinejs']?></td> <td><?=$row['Virsbuve']?></td> <td><?=$row['Atrumkarba']?></td> <td><?=$row['Krasa']?></td> <td><?=$row['Gads']?></td> <td><?=$row['apraksts']?></td> <td>Cena:<big style="color:green"> Ls<?=$row['Cena']?></big></td> <td> <input type="button" value="pieveinot grozam" onclick="pievienot_groza(<?=$row['auto_id']?>)" /> </td> </tr> <? } ?> Quote Link to comment Share on other sites More sharing options...
briedis Posted January 6, 2011 Report Share Posted January 6, 2011 (edited) Paskaties uz šīm rindiņām un padomā: $query = "SELECT * FROM auto WHERE " . implode(" AND ", $where); $result = @mysql_query($query); $result=mysql_query("select * from auto"); while($row=mysql_fetch_array($result)){ Un es silti neieteiktu izmantot "@" operatoru. Edited January 6, 2011 by briedis Quote Link to comment Share on other sites More sharing options...
UnlikePC Posted January 6, 2011 Author Report Share Posted January 6, 2011 Nezinu kapec man izmet visus ierakstus. Vajadzetu atlasit tikai audzikus ar 2.0 kupejas. Bet kad es uzrakstu echo $query; tad vinns rada visu pareizi SELECT * FROM auto WHERE Marka = 'Audi' AND Motors = '2.0' AND Virsbuve = 'kupeja' Kur varetu but problema ? Quote Link to comment Share on other sites More sharing options...
briedis Posted January 6, 2011 Report Share Posted January 6, 2011 Dari tā, sekojoši: 1. Izlasi un apdomā mana augstāk esošā posta saturu 2. Ja nesaproti, kur ir kļūda, dodies uz 1. punktu, ja kļūda skaidra, darbojies tālāk. Quote Link to comment Share on other sites More sharing options...
Blitz Posted January 6, 2011 Report Share Posted January 6, 2011 Izpēti sīki katru rindiņu, un padomā kas katrai funkcijai tiek likts darīt. Quote Link to comment Share on other sites More sharing options...
UnlikePC Posted January 6, 2011 Author Report Share Posted January 6, 2011 Sapratu tik to ka array iekavaas pirmo vertibu jauzdrukaa tagad kad uzrakstu echo $query; rada SELECT * FROM auto WHERE Marka ='Audi' AND Motors = '2.0' AND Virsbuve = 'kupeja' tagad toccna visam butu jaiet, bet nekaa :( tapat visus ierakstus izmet $Marka = "Marka ='Audi'"; $Motors = "2.0"; $Dzinejs = false; $Virsbuve = "kupeja"; $where = array("$Marka"); if($Motors){ $where[] = "Motors = '$Motors'"; } if($Dzinejs){ $where[] = "Dzinejs = '$Dzinejs'"; } if($Virsbuve){ $where[] = "Virsbuve = '$Virsbuve'"; } $query = "SELECT * FROM auto WHERE " . implode(" AND ", $where); echo "$query<br>"; $result = mysql_query($query); Quote Link to comment Share on other sites More sharing options...
briedis Posted January 6, 2011 Report Share Posted January 6, 2011 Nu bāc, skaties uz šitām 3 koda rindiņām un domā loģiski kamēr saproti, kas notiek ne tā!! $query = "SELECT * FROM auto WHERE " . implode(" AND ", $where); $result = @mysql_query($query); $result=mysql_query("select * from auto"); Quote Link to comment Share on other sites More sharing options...
UnlikePC Posted January 6, 2011 Author Report Share Posted January 6, 2011 Sapratu. Cik nejedziga kluda. Vnk es kopeju no daudziem skriptiem ka paraugu. Vel tas ka nezinu php. Paldies stradaa labi. P.S. Kad atskiriba @mysql_query vai mysql_query. Quote Link to comment Share on other sites More sharing options...
briedis Posted January 6, 2011 Report Share Posted January 6, 2011 Palasī gūglē: php error suppression Labāk klausi un nekad nelieto šo operatoru. Ir ļoti reti gadījumi, kad tā lietošana ir attaisnojama. Quote Link to comment Share on other sites More sharing options...
daGrevis Posted January 6, 2011 Report Share Posted January 6, 2011 Vai tad 'elementārs' nav antonīms vārdam 'grūts'? O.o Kā var būt 'elementārs, bet grūts'?? O.o Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.