Heey..
Ik kwam een zoekscript tegen en probeer hem nu om te bouwen maar hij geeft steeds deze fout:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'template WHERE naam LIKE %% ORDER BY naam DESC LIMIT 0, 25' at l

Ik heb het gevoel dat hier een hele makkelijke oplossing aan zit maar ik kom er maar niet op ....

Dit is het script:


<?php
include("config.php");
?>

<html>
<head>
<title>zoek</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

<?php

if($_SERVER['REQUEST_METHOD'] != 'POST') {

?>
	<form action="zoeken.php" method="post">
	<input type="text" name="trefwoord" value="trefwoord">
	<input type="submit" name="submit" value="Zoeken!">
	</form>

<?php

} else {
	$trefwoord = $_POST['trefwoord'];
	$sql = "SELECT *  template WHERE naam LIKE %" .$trefwoord. "% ORDER BY naam DESC LIMIT 0, 25";
	$res = mysql_query($sql) or die(mysql_error());
	$num = mysql_num_rows($res);
	if (empty($num)){
	$num = "geen";
}
	echo"Er zijn $num zoekresultaten gevonden.<br><br>\n";
	while($row = mysql_fetch_object($res)){
?>
	<a href="/index.php?id=<? echo $row->id ?>"><? echo $row->titel ?></a><br>
	<br>

<?php
}
}

?>

</body>
</html>


Ziet iemand de oplossing?
template WHERE naam LIKE %% ORDER BY naam DESC LIMIT 0, 25' at l

Je moet bij LIKE %% wel iets tussen de %% zetten (je zoekwoord)

Hier een stukje voor in je else (als er een POST request is

<?php
$trefwoord = trim($_POST['trefwoord']);
if(strlen($trefwoord)>0){
    $sql = "SELECT * FROM template WHERE naam LIKE %" .$trefwoord. "% ORDER BY naam DESC LIMIT 0, 25";
    $res = mysql_query($sql) or die(mysql_error());
    $num = mysql_num_rows($res);
    if (empty($num)){
        $num = "geen";
    }
}
?>

ik weet niet of het zo klopt maar hier is template je tabel naam. (Je had geen FROM)
regel 27: SELECT * template , kan dat wel? eerst een * en dan nog een naam??,

ook maar een gok hoor:)

[edit]
TJVB schreef op 17.05.2007 11:04
template WHERE naam LIKE %% ORDER BY naam DESC LIMIT 0, 25' at l

Je moet bij LIKE %% wel iets tussen de %% zetten (je zoekwoord)


kijk even in het script! :)
[/edit]
Lees je error na!

SELECT * template FROM ...

ten eerste moet daar een komma tussen
ten tweede hoef je nooit * te gebruiken als je maar 2 kolommen gebruikt

SELECT id,titel FROM ...
zal beter werken
@Arwin: I dont know:P
@Hipska: Waar moet een komma tussen?
kheb nu:

<?php
include("config.php");
?>

<html>
<head>
<title>zoek</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

<?php

if (!empty($_GET['trefwoord'])) {

?>
	<form action="zoeken.php" method="post">
	<input type="text" name="trefwoord" value="trefwoord">
	<input type="submit" name="submit" value="Zoeken!">
	</form>

<?php

} else {
	$trefwoord = $_POST['trefwoord'];
	$sql = "SELECT id, naam FROM template WHERE naam LIKE %" .$trefwoord. "% ORDER BY naam DESC LIMIT 0, 25";
	$res = mysql_query($sql) or die(mysql_error());
	$num = mysql_num_rows($res);
	if (empty($num)){
	$num = "geen";
}
	echo"Er zijn $num zoekresultaten gevonden.<br><br>\n";
	while($row = mysql_fetch_object($res)){
?>
	<a href="/index.php?id=<? echo $row->id ?>"><? echo $row->titel ?></a><br>
	<br>

<?php
}
}

?>

</body>
</html>


NOg steeds deze error?
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'template WHERE naam LIKE %% ORDER BY naam DESC LIMIT 0, 25' at l

$sql = "
SELECT 
   id,
   naam 
FROM 
   template 
WHERE 
   naam 
LIKE 
   '%" .$trefwoord. "%' 
ORDER BY 
   naam 
DESC 
LIMIT 
   0, 25";


Het ging met die fout misschien ook nog wel om dat je geen enkele quotes om %% hebt staan in die error. Nu moet het wel goed zijn.
Oke bedankt :-) Het werkt ;-) kan ik eindelijk verder ;-)

Reageren