Sycrid
Sycrid

Reputation: 19

PHP form search for MySQL DB

I need some help getting a search function to work. I have previously coded something to work similar to this, where if I click on a hyperlink, I'm able to carry a variable forward and then assign this into an SQL script so it pulls only this one thing back from the DB. (Predefined variable, and not user input). I've tried modifying the script I've been using to allow for a form based text box to have user input which is then searched through a single database field, with a LIKE statement.

This is what I have, and it's not returning anything.

Input Form

<form class="formFormat"  method="post" action="SearchResult.php">
        <label class="lableInput2">Key Words</label>
        <input type="text" class="textInput" name="JobDetails" />
        <input type="image" src="img/blue/buttonsearch.jpg" value="search" class="buttonInput" alt="Submit Form" border="0" />
</form>

Returning Page

    <?php
include('conn_mysql.inc');
include('corefuncs.php');
 // create database connection
$conn = dbConnect('query');
// initialize flag
$deleted = false;
// get details of selected record
if ($_GET && !$_POST) {
  // check that primary key is numeric
  if (isset($_GET['JobDetails']) && is_numeric($_GET['JobDetails'])) {
    $JobDetails = $_POST['JobDetails'];
    }
  else {
    $JobDetails = NULL;
    }
  if ($JobDetails) {
    $sql = "SELECT * FROM jobs WHERE JobDetails LIKE '%JobDetails%'";
    $result = mysql_query($sql) or die (mysql_error());
    $row = mysql_fetch_assoc($result);
    }
  }
?>
        <p><h1><?php echo ($row['JobTitle'].'<span class="jobid"> #'.$row['IDJobs'].'</span>');?></h1></p>
                <p><strong><?php echo ($row['Location']); ?></strong></p>
                <p><strong>£<?php echo ($row['JobValue']); ?>.00</strong></p>
                <p><strong><a href="" class="colour">www.companyurl.com - BAD IDEA?</a></strong></p>
                <p><strong>Open for Bidding</strong></p>
                    <br />
                <p><span class="jobid">Job Posted: <?php echo ($row['JobPostDate']); ?></span></p>
                <p><?php print ($row['JobDetails']); ?></p>
                <p><span class="jobid">Job Deadline: <?php echo ($row['JobDeadline']); ?></span></p>

I know that I need to loop the output, so it displays more than one, but at the moment it simply returns the following error for every field (obv the line changes depending on what's trying to extract.

"( ! ) Notice: Undefined variable: row in C:\wamp\www\ReEmployWork\SearchResult.php on line 54"

Can anyone assist? I'm a bit lost with this, and I believe I'm either going in the wrong direction or just missing something.

Upvotes: 0

Views: 6206

Answers (3)

Sycrid
Sycrid

Reputation: 19

For future readers. I scrapped the code I tried to modify and I took it from the beginning. There's enough information above for anyone to do this. Have a go, and you may end up with a result similar to what I coded.

$JobDetails =  $_POST['JobDetails'];
$JobDetails = mysql_real_escape_string($JobDetails);
$sql = "SELECT * FROM `jobs` WHERE `JobDetails` LIKE '%{$JobDetails}%'";
$result = mysql_query($sql) or die (mysql_error());
?>

The above is what I coded and it runs like a dream. You make a lot more mistakes modifying code than you do, if you just code from scratch, so if you're learning dabble and play with code already wrote, but if you need something yourself which is unique then you're best starting from scratch.

Upvotes: 0

Stian Fauskanger
Stian Fauskanger

Reputation: 216

You left your $ before JobDetails in you query.

Also remeber to use http://php.net/manual/en/function.mysql-real-escape-string.php

A suggestion:

$escaped_value = mysql_real_escape_string($JobDetails)
$sql = "SELECT * FROM jobs WHERE JobDetails LIKE '%$escaped_value%'";

Upvotes: 2

user4035
user4035

Reputation: 23719

You missed $ before the variable name. Instead of:

$sql = "SELECT * FROM jobs WHERE JobDetails LIKE '%JobDetails%'";

write:

$sql = "SELECT * FROM jobs WHERE JobDetails LIKE '%$JobDetails%'";

Upvotes: 2

Related Questions