Jump to content

Pulling results based off a user query


KrazyTaco

Recommended Posts

Essentially I'm trying to create a search page.  I have successfully built a page that will retrieve all the entrys in the table, but I can't seem to create a page that will pull only information that relates to what a user defines.

 

I'm using MySQL version 5.0.24a

 

So far I have 2 pages to do this.  A "search.html" page which has a box called "id" where a user can put the id of the 'ransom' they added earlier to retrieve it.  This then gets sent to a page called "search.php"

 

$id  = $_POST['id'];

//SQL query
$query = mysql_query("SELECT * FROM `ransom_records` where id=id") or die(mysql_error());
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

 

So what I'm trying to do is take the variable 'id' and have SQL compare the value given and find it within the database, then return only that result.

 

When I run it, I get the following mysql_error

 

Query failed: 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 'Resource id #2' at line 1

 

So basically I'm just asking how to include a variable within a SELECT FROM WHERE statement.

 

Thanks in advance.

Link to comment
Share on other sites

You are running mysql_query twice and need a $id...change to:

 

<?php
$id  = mysql_real_escape_string($_POST['id']);
$query = "SELECT * FROM `ransom_records` where id='{$id}'";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.