Jump to content
Larry Ullman's Book Forums

Recommended Posts

hi.... i followed the ecommerce chapter of your book and want to know if we want to add a search form in the page and perform fulltext searcher what modifications we have to do in the code ? please help

Share this post


Link to post
Share on other sites

That's a very brief question, how are we supposed to know that considering we have no idea even what your web site is or what the functions of it must be?

  • Upvote 1

Share this post


Link to post
Share on other sites

<form action="browse_dresses.php" method="get">

<fieldset>

<legend>Site Search</legend>

<input type="text" name="a" value= "<? $product_search; ?>" />

<input type="submit" name="go" id="go" value="Search" />

</fieldset>

<?php

require_once ('../mysqli_connect.php');

$query = ("SELECT * FROM first_name,last_name, MATCH(first_name, last_name) AGAINST('$product_search') AS first_name FROM designers WHERE MATCH(first_name, last_name) AGAINST('$product_search') ORDER BY first_name DESC");

 

// Perform Query

$results = mysql_query($query);

 

// Check result

// This shows the actual query sent to MySQL, and the error. Useful for debugging.

if (!$results) {

$message = 'Invalid query: ' . mysql_error() . "\n";

$message .= 'Whole query: ' . $query;

die($message);

}

 

// Store item information

$chosen_item = mysql_fetch_array($results); ?>

 

 

i am using this code and having a number of errors and have no idea how to fix

Share this post


Link to post
Share on other sites

  1. What errors are you receiving?
  2. Your query is incorrect. You need to have a tablename following the first FROM. The second FROM clause is redundant and will also throw up an error.
  3. Which fields do you want to retrieve? You've specified 2 sets of fields - all by using *, and first_name, last_name. Run your query directly using phpmyadmin or some other mysql client and you will get some helpful error messages.
  4. where is $product_search coming from - your code does not show where you are setting it to something that you can search against so I'm guessing you are getting some kind of undefined variable error message.

  • Upvote 1

Share this post


Link to post
Share on other sites

actually i followed the same code as in the book php6 and mysql chapter ecommerce with different table and field names modified and i want to add a search form on the home page to let the users enter a keyword and it ll search the two designers and dresses tables. as they were artists and prints in the chapter and have the same fields as in chapter. what ll be the correct code then?

Share this post


Link to post
Share on other sites

You could use the query

$first_name = $_POST['first_name']; //don't forget mysqli_real_escape_string() function.
$last_name = $_POST['last_name'];
$q = "SELECT * FROM your_table WHERE first_name LIKE '%$first_name%' AND last_name LIKE '%$last_name%'";

Share this post


Link to post
Share on other sites

Notice: Undefined index: first_name in C:\xampp\htdocs\includes\header.php on line 52

 

Notice: Undefined index: last_name in C:\xampp\htdocs\includes\header.php on line 53

 

Notice: Undefined variable: query in C:\xampp\htdocs\includes\header.php on line 56

 

Notice: Undefined variable: query in C:\xampp\htdocs\includes\header.php on line 62

Invalid query: Query was empty Whole query:

 

it gives these errors

Share this post


Link to post
Share on other sites

Perhaps the SQL search you are performing doesn't actually match any records in the DB.

Before you worry about the PHP logic, I would recommend doing some testing by running SQL queries with static values directly on the DB from phpMyAdmin, or something like that.

 

Once you have confirmed that the queries themselves are okay, then confirm you're getting the values you're expecting in the PHP script by echoing the POST values to the screen. If that's okay, then go from there.

Share this post


Link to post
Share on other sites

$q = "SELECT * FROM your_table WHERE first_name LIKE '%$first_name%' AND last_name LIKE '%$last_name%'";

this query returns nothing but when i use some alphabet like h between % this, then i get results matching with the alphabet. what should i do?

Share this post


Link to post
Share on other sites

Notice: Undefined variable: first_name in C:\xampp\htdocs\includes\header.php on line 16

 

Notice: Undefined variable: last_name in C:\xampp\htdocs\includes\header.php on line 16

 

these errors come up when i echo

Share this post


Link to post
Share on other sites

Just as an aside, if you ever again post a message that just says "please help me" or something along those lines, I will permanently ban you from the forums. You've posted your message, now be patient. People will help when and if they can. If you need something to pass the time, try reading the forum guidelines.

  • Upvote 1

Share this post


Link to post
Share on other sites

<input type="text" name="a" value= "<? $product_search; ?>" /> I beleive is incorrect.

it should be

use 'post'
<input type="text" name="product_search" size="40" maxlength="60"   />

Then

$product_search = $_POST['product_search']; // you have to assign your GET or POST data to the variable that you are trying to match in the database...

 

if (isset($_POST['product_search']) {
$product_search = $_POST['product_search'];
}

 

I highly recommend that you read more than just the e-commerce section of the book. Doing so will enable you to fully understand the internal workings of php and the code that is above.

 

cheers

jp

Share this post


Link to post
Share on other sites

hi thanks for your reply. i am using this code now to search a single table when user enters a first name. the code is :

 

<html>

<head>

<title>Search the Database</title>

</head>

 

<body>

 

 

<html>

<head>

<title>Search the Database</title>

</head>

 

<body>

 

<form action="searchlast.php" method="post">

Search: <input type="text" name="term" /><br />

<input type="submit" name="submit" value="Submit" />

</form>

 

 

</body>

</html>

 

 

 

</body>

</html>

<?php

echo $_POST['term'];

 

 

 

require_once ('mysqli_connect.php');

 

 

 

 

 

$term = $_POST['term'];

 

$sql = mysql_query("select * from designers where first_name like '%$term%'");

 

 

 

 

 

$term = $_POST['term'];

 

$sql = mysql_query("select * from designers where first_name like '%$term%'");

 

while ($row = mysql_fetch_array($sql)){

echo 'designer_id: '.$row['designer_id'];

echo '<br/> First Name: '.$row['first_name'];

echo '<br/> Last Name: '.$row['last_name'];

echo '<br/><br/>';

}

 

?>

 

and i am getting these errors:

 

Notice: Undefined index: term in C:\xampp\htdocs\searchlast.php on line 30

 

Notice: Undefined index: term in C:\xampp\htdocs\searchlast.php on line 40

 

Notice: Undefined index: term in C:\xampp\htdocs\searchlast.php on line 48

 

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\searchlast.php on line 52

 

do someone know the solution please?

Share this post


Link to post
Share on other sites

thanks for your reply.

now i am using the following code to search a single table when user enters a keyword.

<html>

<head>

<title>Search the Database</title>

</head>

 

<body>

 

 

<html>

<head>

<title>Search the Database</title>

</head>

 

<body>

 

<form action="searchlast.php" method="post">

Search: <input type="text" name="term" /><br />

<input type="submit" name="submit" value="Submit" />

</form>

 

 

</body>

</html>

 

 

 

</body>

</html>

<?php

echo $_POST['term'];

 

 

 

require_once ('mysqli_connect.php');

 

 

 

 

 

$term = $_POST['term'];

 

$sql = mysql_query("select * from designers where first_name like '%$term%'");

 

 

 

 

 

$term = $_POST['term'];

 

$sql = mysql_query("select * from designers where first_name like '%$term%'");

 

while ($row = mysql_fetch_array($sql)){

echo 'designer_id: '.$row['designer_id'];

echo '<br/> First Name: '.$row['first_name'];

echo '<br/> Last Name: '.$row['last_name'];

echo '<br/><br/>';

}

 

?>

 

and getting the following errors

Notice: Undefined index: term in C:\xampp\htdocs\searchlast.php on line 30

 

Notice: Undefined index: term in C:\xampp\htdocs\searchlast.php on line 40

 

Notice: Undefined index: term in C:\xampp\htdocs\searchlast.php on line 48

 

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\searchlast.php on line 52

 

do someone know the solution please?

Share this post


Link to post
Share on other sites

You can't just blindly try to grab values from the $_POST array. You have to first test whether the $_POST array has any values in it to begin with, which will only be the case when the form is submitted. Try putting a majority (if not all) of your PHP code within the following if condition:

 

if (isset($_POST['term']))

Share this post


Link to post
Share on other sites

As an aside It would help if you proofread your post before posting as you appear to have duplicate tags and queries. Also it would help if you inserted your code between code tags (there is an icon you can use on the edit bar).

 

Here's why you're getting the undefined index error: The first time your script is run, the form has not been submitted so the $_POST array will not have any values. You need to start with a conditional to check for this index

if (isset($_POST['term'])) {

You're using the improved version of mysql (mysqli) to connect to the database and the old version mysql to make your query.

 

Here is a simplified version of what I think you're trying to do using mysqli. You may need to change the $dbc variable to match what is in your mysqli_connect.php file

<form action="searchlast.php" method="post">
Search: <input type="text" name="term" /><br />
<input type="submit" name="submit" value="Submit" />
</form>
<?php
if (isset($_POST['term'])) {
echo $_POST['term'];
require_once ('mysqli_connect.php');
$term = $_POST['term'];
$query = "select * from designers where first_name like '%$term%'";
$result = mysqli_query($dbc, $query);
while ($row = mysqli_fetch_array($result)){
echo 'designer_id: '.$row['designer_id'];
 echo '<br/> First Name: '.$row['first_name'];
 echo '<br/> Last Name: '.$row['last_name'];
 echo '<br/><br/>';
}
}
?>

  • Upvote 1

Share this post


Link to post
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...
Sign in to follow this  

×
×
  • Create New...