-1

I was hoping I could get a little help or a push in the right direction, I am new to php, and still learning.

Currently am trying to develop a site that can add, edit, delete and search from my MYSQL database. with the add, edit, delete part I feel pretty confident and should not have a issue. I am just having trouble with the search function. I can manually search my database and display everything within that table, but now I am trying to make it so that I can enter a first name or last name and all the data about query in the table will display.

currently I have 2 php files (search.php): with a "input type: textbox" and submit button ( so I can type a first name and hit search) and I also have a (searchresult.php): which I want to search for the first name I entered and display the cust_id, first name, last name and email (which are populated in my customers table within my database).

currently I am getting an error of: " warning mysql_fetch_array() expects parameter 1 to be resource, Boolean given" , iv been reading up about the issue but still not sure what to do from here, and was hoping for some help with my code, and maybe a better ( dumbed down ) explanation of the error.

Below ill show my code for both pages:

Below is my "search.php" code: just a simple / basic html

<!DOCTYPE HTML>
<html>
<head>
<title>Conxbiz - Search Customer</title>
<link rel="stylesheet" type="text/css" href="../conxbiz/css/main.css">
</head>

<body>
<form name="form" action="searchresult.php" method="get"> 
<input name="firstname" type="text"> 
<input type="submit" name="search" value="Search"> 
</form> 

<?php

?>


</body>
</html>

and below is my "searchresult.php" code:

<!DOCTYPE HTML>
<html>
<head>
<title>Conxbiz - Home</title>
<link rel="stylesheet" type="text/css" href="../conxbiz/css/main.css">
</head>

<body>


<?php
$firstname = $_GET['firstname'];

echo "<table>";
echo "<tr>";
echo "<th>cust_id</th>";
echo "<th>First Name</th>";
echo "<th>Last Name</th>";
echo "<th>Email</th>";
echo "</tr>";

mysql_connect("localhost", "root", "") or die("Could not connect: " . mysql_error());
mysql_select_db("conxbiz");

$query = mysql_query("SELECT * FROM customers WHERE firstname '$firstname'");


while ($row = mysql_fetch_array($query, MYSQL_ASSOC))
{
$cust_id = $row['cust_id'];
$firstname = $row['firstname'];
$lastname = $row['lastname'];
$email = $row['email'];

echo "<tr>";
echo "<td>" . $row['cust_id'] . "</td>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "</tr>";
}

echo "</table>";
?>


</body>
</html>

so to re-cap, my aim is that I want to enter say "adam" into a textbox, hit search and have the php code pull up "cust_id, firstname, lastname and email" and display this to the screen of all the adams I have in the data base.

Is it a case of:

  1. its not reading what I am passing from the textbox, so its failing.
  2. its reading what im passing but my SQL is wrong and so its failing
  3. is it passing say a number instead of the text, so its not expecting it.
  4. its returning true / false and not displaying data ?
  5. my code is all wrong and I should just give up :( lol

id like help with my coding but I am hoping I can also get an explanation of the error for future reference!

my coding isn't the best, but like I said I am still learning, and for now just trying to get the basic functions to work.... baby steps, lol...

I am guessing my coding is totally wrong :( but hope someone could help me

thank you for any help / guidance in advance!

Radu Rădeanu
  • 2,474
  • 2
  • 23
  • 42
  • "I am new to php, and still learning". Stop learning deprecated functions then. Use either PDO or mysqli. – Mike Apr 26 '14 at 23:47

3 Answers3

3

This is invalid syntax:

SELECT * FROM customers WHERE firstname '$firstname'

You need an operator, like =:

SELECT * FROM customers WHERE firstname = '$firstname'

Do note that even with this change, your code is vulnerable to SQL injection. In fact, if it's feasible, I'd recommend moving away from the mysql_* functions completely (they're deprecated) and moving to PDO, which in addition makes it easier to write code that's not vulnerable to SQL injection.

icktoofay
  • 122,243
  • 18
  • 242
  • 228
  • omg wow ! thank you !!! cant believe I forgot an "=" sign , iv been looking at my code on and off for 2 days :( I will look into PDO :) again thank you ! – user3577164 Apr 27 '14 at 00:01
0

In the line:

$query = mysql_query("SELECT * FROM customers WHERE firstname '$firstname'");

put an equal sign after firstname like this

$query = mysql_query("SELECT * FROM customers WHERE firstname = '$firstname'");

Also, using database objects and methods prefixed with mysql like mysql_query is fairly outdated, and unsafe. I would suggest update to methods prefixed with mysqli or if you want to secure your application, use PDO.

Mike
  • 22,114
  • 13
  • 72
  • 84
golgothan3
  • 733
  • 5
  • 9
  • 1
    thank you ! a simple "=" sign :( lol I will look into PDO ! to think id been looking at that code on and off for 2 days raking my brain :( lol – user3577164 Apr 27 '14 at 00:02
0

You get this error whenever the query you pass to the query to the mysql_query has some mistake in it. This means that there is something wrong with your query. First thing you need to check is the query that you have written.

In this case you have forgotten to place the "=" sign in-between firstname '$firstname'".

Hence try

$query = mysql_query("SELECT * FROM customers WHERE firstname = '$firstname'");

or since it is a string (of type char or varchar) try using like operator.

$query = mysql_query("SELECT * FROM customers WHERE firstname like '$firstname'");

Aragorn
  • 34
  • 5