Current location: Hot Scripts Forums » Programming Languages » PHP » Select - Where doesnt work


Select - Where doesnt work

Reply
  #1 (permalink)  
Old 06-03-10, 02:25 AM
gielvandam gielvandam is offline
New Member
 
Join Date: Jun 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Select - Where doesnt work

Hello,
Now I did allready search for days.
I cann't find the mistake.
The form works.
The echo is correct
But the select ... where is wrong.
The result says "Could not successfully run query "
Look at Peter's Mobiele Sites Vieuw responceswhere I test this simple select procedure.

The whole code:

<?php
/*
Template Name: students-vieuw-responces
*/
?>

<?php get_header(); ?>

<?php if (have_posts()) : the_post(); update_post_caches($posts); ?>

<div class="post" id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>

<?php
/*
Vanaf hier de kode om iets op te halen uit de database
*/
?>

<div id="questionForm">
<!-- begin form -->
<form method=post>
<ul>
<li>
<label class="formFieldQuestion">
ID
</label>
<input class=mainForm type=text name=zoeken id=zoeken size='50' value=''>
</li>
<li class="mainForm">
<input id="saveForm" class="mainForm" type="submit" value="Submit" />
</li></ul>
</form>
<!-- end of form -->
</div>

<table border="1">

<?php
echo $_POST ['zoeken'];
/* Verbinding met de MySQL database maken.*/

$db = mysql_connect("localhost" , "debxxxx" , "xxxxx") or die (mysql_error());

/* Selecteer de database waarop de query gedraaid moet worden.*/

mysql_select_db("debxxxxxx", $db);

/* Uitvoeren van de query.*/

$sql = "SELECT * FROM `students` WHERE eu=" .$_POST ['zoeken'].


/* Het resultaat van de query in een array variabele stoppen, zodat we deze later uit kunnen lezen.*/

$result = mysql_query($sql);

if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}

/* Met behulp van de functie mysql_fetch_assoc halen we de rijen

uit de array op.*/

while ($row = mysql_fetch_assoc($result)){
Reply With Quote
  #2 (permalink)  
Old 06-03-10, 03:19 AM
maneetpuri maneetpuri is offline
Newbie Coder
 
Join Date: Nov 2008
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
Hi,

The query looks fine to me -

Change the line –

$result = mysql_query($sql);

In your code with the following line –

$result = mysql_query($sql) or die(mysql_error());

This will show the error that has been generated, which will help you troubleshoot the problem.


Hope this helps.

Cheers,

~Maneet
__________________
Maneet Puri
India Web Site Design Services
PHP Web Development
Reply With Quote
  #3 (permalink)  
Old 06-03-10, 03:55 AM
gielvandam gielvandam is offline
New Member
 
Join Date: Jun 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Dear Maneet or any other reader,

Thank you for your reaction but it still doesn't work.

If I change WHERE eu=" .$_POST ['zoeken']. into WHERE eu='non-EU' "; then the SELECT works. But I cann't use the form then
You can see it yourself at the site At this moment it is static, not using the FORM.
So the mistake must be in that part.

Do you see anything?

Last edited by gielvandam; 06-03-10 at 03:57 AM.
Reply With Quote
  #4 (permalink)  
Old 06-03-10, 05:57 AM
gielvandam gielvandam is offline
New Member
 
Join Date: Jun 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Hello,

I didn't stop searching to solve.
Now I did WHERE 'eu'=' ".$_POST ['zoeken']." ");
And the mistake returned is 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 '' eu' at line 1
I think it has something to do with qoutes.
Reply With Quote
  #5 (permalink)  
Old 06-03-10, 06:30 AM
gielvandam gielvandam is offline
New Member
 
Join Date: Jun 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Hello,

SOLVED.

I made a step before.
$zoeken = $_POST["zoeken"];

Then I changed in SELECT the last part in WHERE firstproject='$zoeken'";

And now it works fine.
I can go on.
Thanks to my puzzeling
Reply With Quote
  #6 (permalink)  
Old 06-03-10, 06:48 AM
scott2500uk's Avatar
scott2500uk scott2500uk is offline
Coding Addict
 
Join Date: Apr 2006
Posts: 275
Thanks: 2
Thanked 2 Times in 2 Posts
The correct $sql is:

PHP Code:

$sql "SELECT * FROM `students` WHERE `eu`='".$_POST['zoeken']."'"
or
PHP Code:

$sql "SELECT * FROM `students` WHERE `eu`='{$_POST['zoeken']}'"
But remember you are taking user inputed data and can be dangerous for your database. Make sure you make the data safe first with the use of mysql_real_escape_string():

PHP Code:

$sql "SELECT * FROM `students` WHERE `eu`='".mysql_real_escape_string($_POST['zoeken'])."'"
Reply With Quote
Reply

Bookmarks

Tags
select where form


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
select() confusion: when to stop reading a socket ??? mailjol PHP 1 11-26-07 11:48 AM
Why doesn't this php code work and how do I select the highest value and add 1? Oskare100 PHP 1 09-22-06 08:20 AM
How to make search engine to work? mhs12grade1992 PHP 1 02-16-05 04:50 PM
I have a visual basic problem I cannot solve. I need to hire someone to do this work chrismonroe10 Job Offers & Assistance 2 02-11-05 11:24 AM
Select List insert into DB The Omega JavaScript 1 01-12-05 12:51 PM


All times are GMT -5. The time now is 08:15 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.