Jump to content
php.lv forumi

Palidzat DB filtracija


UnlikePC

Recommended Posts

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);

?>

Link to comment
Share on other sites

$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 by briedis
Link to comment
Share on other sites

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>  
       <? } ?>

Link to comment
Share on other sites

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 by briedis
Link to comment
Share on other sites

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 ?

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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");

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...