call to undefined function php mysql - php

I am new to coding and keep getting this error and I am not sure what the reason is... I am trying to search/retrieve data from a Mysql database... Idea is that someone selects a category of search (such as first name) and inputs a first name and then code retrieves all relevant matches from database table Customers.
I am getting the following error:
Fatal error: Call to undefined function query() in ..../search.php on line 36
Can anyone help.
<html>
<head>
<title>pageName</title>
<style type="text/css">
table {
background-color: #ADD8E6;
border: 1px solid black;
font-family: Arial; font-size: 14px;
}
th {
text-align: left;
}
</style>
</head>
<body>
<h1>MEMBERS SEARCH</h1>
<form method="post" action="search.php">
<input type="hidden" name="submitted" value="true"/>
<label> Search Category:
<select name="category">
<option value="firstname">First NAME</option>
<option value="lastname">Last NAME</option>
</select>
</label>
<label> Search Criteria:<input type="text" name="criteria" /></label>
<input type="submit" />
</form>
<?php
if (isset($_POST['submitted'])){
// connect to the DB
include('connect.php');
$category = $_POST['category'];
$criteria = $_POST['criteria'];
$query = "SELECT * FROM Customers WHERE $firstname LIKE '%" . $criteria ."%'";
$result = $query ($con, $query) or die ('error getting data from database');
$num_rows = mysql_num_rows ($result);
echo "$num_rows results found";
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
?>
<table>
<tr>
<td width="300" ><font face="Arial Black" size="2"><?php echo $row['firstname']?> <?php echo $row['lastname']?></font></td>
</tr>
</table>
<table>
<tr>
?>
</body>
</html>

Try changing your code to look like this.
<form method="post" action="search.php">
<input type="hidden" name="submitted" value="true"/>
<label> Search Category:
<select name="category">
<option value="firstname">First NAME</option>
<option value="lastname">Last NAME</option>
</select>
</label>
<label> Search Criteria:<input type="text" name="criteria" /></label>
<input type="submit" />
</form>
<?php
if (isset($_POST['submitted'])){
// connect to the DB
include('connect.php');
$category = mysqli_real_escape_string($con, $_POST['category']);
$criteria = mysqli_real_escape_string($con, $_POST['criteria']);
$query = "SELECT * FROM Customers WHERE firstname LIKE '%" . $criteria ."%'";
$result = mysqli_query($con, $query);
$num_rows = mysqli_num_rows($result);
echo "$num_rows results found";
Brief explanation of changes:
Added 'real_escape_string' function to your user submitted variables
to help prevent sql injection
Changed '$firstname' in your query to 'firstname'
Took out 'or die()' part after query and used mysqli_ functions to
handle the data.

You have $query instead of query, it should be a function not a variable. That may fix your problem, if there is more let me know.
$result = $query ($con, $query) or die ('error getting data from database');
should be:
$result = query($con, $query) or die('error getting data from database');

Also you have query mistake:
$query = "SELECT * FROM Customers WHERE $firstname LIKE '%" . $criteria ."%'";
Should be:
$query = "SELECT * FROM Customers WHERE firstname LIKE '%" . $criteria ."%'";
Should be firstname and not $firstname.
Also
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
While loop doesnt have a closing brackets
It should be like:
<table>
<?php
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
?>
<tr>
<td width="300" ><font face="Arial Black" size="2"><?php echo $row['firstname']?> <?php echo $row['lastname']?></font></td>
</tr>
<table> <---- This is html mistake also remove it
<tr> <----- This is html mistake also remove it
<?php
}
?>
</table>
I moved your table tag around loop because this is logical way to every row from loop generates row in table, but in some examples you can loop also whole tables just you will get lots of tables in your html code.

I think what you are looking for is mysqli_query($con, $query).
Try replacing your query() function call with mysqli_query()
Note that mysql_query() is depricated in favor of mysqli_query() for security reasons.
See the documentation for it here:
http://php.net/manual/en/mysqli.query.php
If that doesn't help then I'd suggest examining the code in your 'connect.php' that you are including. Also it would be helpful to get the exact text of the error, it would help us debug your code.

The line
$result = $query ($con, $query) or die ('error getting data from database');
doesn't make sense at all. $query is a variable containing the SQL instructions, not a function. And the variable $con has not been defined, at least not in the code you've shown.
Also, you should use the function mysqli_query() - http://php.net/manual/en/mysqli.query.php
So the line should be:
$result = mysqli_query($query) or die ('error getting data from database');
If you are using procedural style and $con is the link identifier returned by mysqli_connect() or mysqli_init(), use:
$result = mysqli_query($con,$query) or die ('error getting data from database');

Assuming you are using php mysql extension, below is the corrected solution to your problem.
<?php
if (isset($_POST['submitted'])){
include('connect.php');
$category = $_POST['category'];//I guess it is search category(e.g. firstname)
$criteria = strtolower($_POST['criteria']);
$query = "SELECT * FROM Customers WHERE LOWER({$category}) LIKE '%{$criteria}%'";
$result = mysql_query($query) or die (mysql_error());
$num_rows = mysql_num_rows($result);
echo "{$num_rows} results found";
echo '<table>';
while ($row = mysql_fetch_assoc($result)) {
?>
<tr>
<td width="300" >
<font face="Arial Black" size="2"><?php echo "{$row['firstname']} {$row['lastname']}"?></font>
</td>
</tr>
<?php } echo '</table>'; ?>

Related

Search multiple Criteria - And/or search in PHP

Not sure what I am doing wrong here. I would like to create a search that allows the user to do an and or search.
However when I use the below code, if I type in Brown as Colour1 it will return all results, same as post code.
The goal is to allow the user to search multiple fields to return a match. So Colour1 and Postcode
<html>
<head>
<title> Logo Search</title>
<style type="text/css">
table {
background-color: #FCF;
}
th {
width: 250px;
text-align: left;
}
</style>
</head>
<body>
<h1> National Logo Search</h1>
<form method="post" action="singlesearch2.php">
<input type="hidden" name="submitted" value="true"/>
<label>Colour 1: <input type="text" name="criteria" /></label>
<label>Colour 2: <input type="text" name="criteria2" /></label>
<label>PostCode: <input type="text" name="criteria3" /></label>
<label>Suburb: <input type="text" name="criteria4" /></label>
<input type="submit" />
</form>
<?php
if (isset($_POST['submitted'])) {
// connect to the database
include('connect.php');
//echo "connected " ;
$criteria = $_POST['criteria'];
$query = "SELECT * FROM `Mainlist` WHERE (`Colour1`like '%$criteria%')
or
('Colour2' like '%$criteria2%')
or
('PostCode' = '%$criteria3%')
or
('Suburb' like '%$criteria4%')
LIMIT 0,5";
$result = mysqli_query($dbcon, $query) or die(' but there was an error getting data');
echo "<table>";
echo "<tr> <th>School</th> <th>State</th> <th>Suburb</th> <th>PostCode</th> <th>Logo</th> <th>Uniform</th></tr>";
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo "<tr><td>";
echo $row['School'];
echo "</td><td>";
echo $row['State'];
echo "</td><td>";
echo $row['Suburb'];
echo "</td><td>";
echo $row['PostCode'];
echo "</td><td><img src=\"data:image/jpeg;base64,";
echo base64_encode($row['Logo']);
echo "\" /></td></td>";
echo "</td><td><img src=\"data:image/jpeg;base64,";
echo base64_encode($row['Uniform']);
echo "\" /></td></td>";
}
echo "</table>";
}// end of main if statment
?>
</body>
</html>
I can get it to work correctly when I use a dropdown list to select the criteria however I would like them to have multiple options to filter results.
<form method="post" action="multisearch.php">
<input type="hidden" name="submitted" value="true"/>
<label>Search Category:
<select name="category">
<option value="Colour1">Main Colour</option>
<option value="Colour2">Secondary Colour</option>
<option value="PostCode">Post Code</option>
</select>
<label>Search Criteria: <input type="text" name="criteria" /></label>
<input type="submit" />
</form>
<?php
if (isset($_POST['submitted'])) {
// connect to the database
include('connect.php');
echo "connected " ;
$category = $_POST['category'];
$criteria = $_POST['criteria'];
$query = "SELECT * FROM `Mainlist` WHERE $category LIKE '%$criteria%'";
$result = mysqli_query($dbcon, $query) or die(' but there was an error getting data');
In general you run your query with AND condition because it has criteria and you have it means that you have to show value by matching each criteria with receptive column. but it also depends on users or client how they want to show their field.
In short it doesn't have any rule how to show.
Played around with it for a bit and found the answer. I needed to define the criteria. see below code
<?php
if (isset($_POST['submitted'])) {
// connect to the database
include('connect.php');
//echo "connected " ;
$criteria = $_POST['criteria'];
$criteria2 = $_POST['criteria2'];
$criteria3 = $_POST['criteria3'];
$criteria4 = $_POST['criteria4'];
$criteria5 = $_POST['criteria5'];
$query = "SELECT * FROM `Mainlist` WHERE (`Colour1`like '%$criteria%') and (`Colour2`like '%$criteria2%')
and (`PostCode`like '%$criteria3%') and (`Suburb`like '%$criteria4%') and (`State`like '%$criteria5%')

Displaying query results after submitting a PHP form

I am currently working on a school project using php and mysql. I have created a form with three drop down boxes where users select types of data they are looking for. However, I am having a lot of trouble displaying the results after the form is submitted. Here is my current code:
<?php
require_once 'connection.php';
?>
<form action="stats.php" method ="post">
<input type="hidden" name="submitted" value="true" />
<fieldset>
<legend>
Specify Date, Month, and County
</legend>
<p>
<label for="year">
Please select a year
</label>
<select name= 'year'>
<?php
$query = "select distinct year from unemployed";
$result = $conn->query($query);
while($row = $result->fetch_object()) {
echo "<option value='".$row->year."'>".$row->year."</option>";
}
?>
</select>
</p>
<p>
<label for="month">
Please select a month
<label>
<select name= 'month'>
<?php
$query = "select distinct month from unemployed";
$result = $conn->query($query);
while($row = $result->fetch_object()) {
echo "<option value='".$row->month."'>".$row->month."</option>";
}
?>
</select>
</p>
<p>
<label for="location">
Please specify a location
</label>
<select name='select'>
<?php
$query = "select * from unemployed";
$result = $conn->query($query);
while ($finfo = $result->fetch_field()) {
echo "<option value='".$finfo->name."'>".$finfo->name."</option>";
}
?>
</select>
</p>
<input type ="submit" />
</fieldset>
</form>
<?php
if (isset($_POST['submitted'])) {
include('connection.php');
$gYear = $_POST["year"];
$gMonth = $_POST["month"];
$gSelect = $_POST["select"];
$query = "select $gSelect from unemployed where year='$gYear' and month='$gMonth'";
$result = $conn->query($query) or die('error getting data');
echo"<table>";
echo "<tr><th>Year</th><th>Time</th><th>$gSelect</th></tr>";
while ($row = $result->fetch_object()){
echo "<tr><td>";
echo $row['Year'];
echo "</td><td>";
echo $row['Month'];
echo "</td><td>";
echo $row['$gSelect'];
echo "</td></tr>";
}
echo "</table";
} // end of main if statement
?>
I am almost certain my problem lies within my while statement. I get the titles of my table columns to show up (Year, Month, $gSelect), but I am not getting my query results to be displayed.
I have tried:
while ($row = $result->fetch_object())
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
Neither of these are working for me. I have looked at php.net for guidance. I am still confused with what to do. If anyone could help me, I would really appreciate it.
Always check your returns:
if( ! $result = $conn->query($query) ) {
die('Error: ' . $conn->error());
} else {
while($row = $result->fetch_object()) {
echo "<option value='".$row->year."'>".$row->year."</option>";
}
}
Also putting error_reporting(E_ALL); at the top of your script while you're developing it will help enormously as well.
You should really look into passing variables as parameters to your query instead of injecting them as variables directly into your query. This can lead to sql injection attacks.
Also, here's a quick example of how to write a query using PDO and mysql:
//Simple Query
$dbh = new PDO('mysql:host=localhost;dbname=testdb;charset=utf8', 'username', 'password');
//Useful during development.
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//mysql can have prepares depending on the version: http://stackoverflow.com/questions/10113562/pdo-mysql-use-pdoattr-emulate-prepares-or-not
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$sth = $dbh->query("SELECT * FROM table");
var_dump($sth->fetchAll(PDO::FETCH_ASSOC));
//Now with pass params and a prepared statement:
$query = "SELECT * FROM table WHERE someCol = ?";
$sth = $dbh->prepare($query);
$sth->bindValue(1,"SomeValue");
$sth->execute();
$results = $sth->fetchAll(PDO::FETCH_ASSOC));

PHP: Query was empty when searching

I need a little help for my problem...
Scene: I was trying to search something in my database but the result is "Query was empty" but the one I'm trying to search is already in my database. I'm trying to search the "Atrium Hotel"
Here's my screenshot of my Database:
Here's my screenshot of my result Page:
And Lastly here's my code:
<input type='submit' name='search' value='Search Building' onClick="this.form.action='search_bldg.php'; this.form.submit()">
<input type="text" id="idSearch"name="searchBldg" size="40" value="<?php echo $_POST['searchBldg']; ?>">
<fieldset width= "50px">
<legend>BUILDING/S</legend>
<?php
$search = $_POST["searchBldg"];
$data = mysql_query("SELECT * FROM tbldata WHERE fldBldgName LIKE '%$search%'");
$result = mysql_query($data) or die(mysql_error());
while($row = mysql_fetch_array( $result ))
{
echo $row['fldBldgName'];
}
?>
</fieldset>
I was wondering what is the problem in my query...
Thanks in advance...
You are executing your query twice (Line #7 and #8). That may be the problem. Try something like this:
<input type='submit' name='search' value='Search Building' onClick="this.form.action='search_bldg.php'; this.form.submit()">
<input type="text" id="idSearch"name="searchBldg" size="40" value="<?php echo $_POST['searchBldg']; ?>">
<fieldset width= "50px">
<legend>BUILDING/S</legend>
<?php
$search = $_POST["searchBldg"];
$query= "SELECT * FROM tbldata WHERE fldBldgName LIKE '%$search%'"; //Your sql
$result = mysql_query($query) or die(mysql_error()); //execute your query
while($row = mysql_fetch_array( $result ))
{
echo $row['fldBldgName'];
}
?>
</fieldset>
P.S. use mysqli_* or PDO instead of mysql_* since it is deprecated as of PHP 5.4
You should use mysqli or PDO since mysql_* is depreciated.
Try the below code: It's using mysqli . It should be working...
<?php
$search = $_POST["searchBldg"];
//connecting to db...mysqli_connect("example.com","peter","abc123","my_db")
$con=mysqli_connect(host,username,password,dbname);
//searching...
$query= "SELECT * FROM tbldata WHERE fldBldgName LIKE '%$search%'";
//execute the query
$result = mysqli_query($query) or die(mysqli_error());
while($row = mysqli_fetch_array( $result ))
{
echo $row['fldBldgName'];
}
mysqli_close($con);
?>
There are some issues in your code that I'll demonstrate by commenting your code:
<input type='submit' name='search' value='Search Building' action='search_bldg.php' onClick="this.form.submit();"> <!-- setting action-attribute directly in form-tag, no need to use js for that -->
<input type="text" id="idSearch" name="searchBldg" size="40" value="<?php echo $_POST['searchBldg']; ?>"> <!-- added a space between id="search" and name-attribute -->
<fieldset width= "50px">
<legend>BUILDING/S</legend>
<?php
$search = $_POST["searchBldg"];
$data = "SELECT * FROM tbldata WHERE fldBldgName LIKE '%$search%'"; //mysql_query is removed, because the actual query is executed below
$result = mysql_query($data) or die(mysql_error());
while($row = mysql_fetch_assoc( $result )) //mysql_fetch_array doesn't return associative arrays, therefore it's replaced with mysql_fetch_assoc
{
echo $row['fldBldgName'];
}
?>
</fieldset>
You should of course sanitize data (so not unwanted data gets inserted into db) and use something else besides mysql_* as stated in previous answers.
Another issue (which is not really a problem) is the name of elements, column-names etc. searchBldg is very similar to searchB1dg and it might be easy to do typing-errors which might be hard to find.

populate a select box with php mysql

I'm having difficulty populating a select box within a form to display existing "forenames" of nurses from the "Nurses" table. Could anyone tell me what Im doing wrong? Thanks in advance!
Here is the form
<form method="post" action="insert.php">
<br>
<tr><td align="left"><strong>Nurse Information</strong></td>
</td>
<tr>
<td>nurse_name</td>
<td><select name="valuelist">
<option value="valuelist" name="nurse_name" value='<?php echo $nurse_name; ?>'></option>
</select></td>
<tr>
The QUERY which should populate the nurse_forename:
<html><head><title>Connect to Database</title></head><body>
<font size="4">Query gets Forename of nurse</font>
<br><br><font size="4">Choose a name</font><br><br>
<form action="insert.php" method="post">
<select name="valuelist">;
<?php
$value=$_POST ["valuelist"];
$con = mysql_connect("localhost","root","") or die('Could not connect: ' . mysql_error());
mysql_select_db("a&e", $con) or die('Could not select database.');
$fetch_nurse_name = mysql_query("SELECT DISTINCT $nurse_name FROM nurse");
$result = mysqli_query($con, $query) or die("Invalid query");
while($throw_nurse_name = mysqli_fetch_array($fetch_nurse_name)) {
echo '<option value=\"'.$nurse_name['nurse_name'].'">'.$throw_nurse_name['nurse_name'].'</option>';
}
echo "</select>";
mysqli_close($con);
?>
<input type="submit" value="Submit">
</form></body></html>
Try this:
<html><head><title>Connect to Database</title></head><body>
<font size="4">Query gets Forename of nurse</font>
<br><br><font size="4">Choose a name</font><br><br>
<form action="insert.php" method="post">
<select name="valuelist">;
<?php
$value=$_POST ["valuelist"];
$con = mysql_connect("localhost","root","") or die('Could not connect:'.mysql_error());
mysql_select_db("a&e", $con) or die('Could not select database.');
$fetch_nurse_name = mysql_query("SELECT DISTINCT Forename FROM nurse");
while($throw_nurse_name = mysql_fetch_array($fetch_nurse_name)) {
echo '<option value=\"'.$throw_nurse_name[0].'">'.$throw_nurse_name[0].'</option>';
}
echo "</select>";
?>
<input type="submit" value="Submit">
</form></body></html>
Dont use mysql and mysqli together....you should use mysqli or PDO, but not a mix of both ;)
PS: Edited ;)
Saludos.
Apologies if this duplicates other answers, Here's an answer using mysql_ syntax although you should of course be using mysqli_ or PDO for this...
<form action="insert.php" method="post">
<select name="valuelist">;
<?php
//path to connection statements
include('path/to/connection/stateme.nts');
//fetch nurse name
$query = "SELECT nurse_name FROM nurse;";
$result = mysql_query($query) or die(mysql_error()); //note: use mysql_error() for development only
//print results
while($row = mysql_fetch_assoc($result)) {
echo '<option value=\"'.$row['nurse_name'].'">'.$row['nurse_name'].'</option>';
}
echo "</select>";
?>
<input type="submit" value="Submit">
</form>
Check your MySQL table and column name that you using. Sometimes it does not work if you don't write those names exactly which in your MySQL table. suppose,
$query = "SELECT nurse_name FROM nurse";
in above SQL if MySQL table name is 'NURSE' and column name is 'NURSE_NAME' then write exactly like this.
$query = "SELECT NURSE_NAME FROM NURSE";
So, you look that sometime MySQL table, column name work in case sensitive.

Newbie: Building an employee directory with scripts for add/edit/search/delete. Errors

I am building an employee directory that has 3 simple forms. The first adds records, the second searches for records, the third searches then deletes records.I want to display all the records on the same page, and then when a search is done, just display those records that fit the search keyword.
I have built the DB and the Table correctly. The first form adds records to the DB successfully. Before I make the search and delete forms work correctly I am trying to get the records to display. They are not displaying. Sometimes I can get my html table to display, but none of the records appear. However, I know that the records exist because I can see them in MyAdmin.
I am getting this error right now, but my errors are changing by the moment as I try new things: Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in C:\xampp\htdocs\Employees.php on line 84
I would love some help to do the following:
1. Help me understand why I am getting this error.
2. Help me understand how to display my records (I've done this successfully before, but with a simpler task).
I know this code is unfinished. I am building it piece by piece and trying to get each individual piece to function before I add the next. Thanks!
<html>
<body>
<?php error_reporting (E_ALL ^ E_NOTICE);
$keyword = $_GET['keyword']; ?>
<?php
$con = mysql_connect("localhost", "employees", "employeepw");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("employees", $con);
mysql_query("INSERT INTO employeeinfo (firstname, lastname, phone, email, department, position)
VALUES ('$_POST[firstname]', '$_POST[lastname]', '$_POST[phone]', '$_POST[email]', '$_POST[department]', '$_POST[position]')");
mysql_query($sql,$con);
function buildQuery() {
$keyword = $_GET['keyword'];
$sql = "SELECT * from employeeinfo WHERE
(
firstname LIKE '%$keyword%'
OR
lastname LIKE '%$keyword%'
OR
phone LIKE '%$keyword%'
OR
email LIKE '%$keyword%'
OR
department LIKE '%$keyword%'
OR
position LIKE '%$keyword%'
)";
return $sql;
} ?>
<form action="Employees.php" method="post">
<fieldset>
<legend>Submit Employee Info</legend>
Firstname: <input type="text" name="firstname" />
Lastname: <input type="text" name="lastname" />
Phone: <input type="text" name="phone" />
Email: <input type="text" name="email" />
Department: <input type="text" name="department" />
Position: <input type="text" name="position" />
<input type=submit name=submit value=Submit />
</fieldset>
</form>
<form action="Employees.php" method=get>
<fieldset>
<legend>Search Employee Info</legend>
<label for="keyword">Enter Keyword</label>
<input id="keyword" name="keyword" value="<?php echo "$keyword"; ?>" />
<input type=submit name=submit value=Search />
</fieldset>
</form>
<form action="Employees.php" method=get>
<fieldset>
<legend>Delete Employee Info</legend>
<label for="keyword">Enter Keyword</label>
<input id="keyword" name="keyword" value="<?php echo "$keyword"; ?>" />
<input type=submit name=submit value=Delete />
</fieldset>
</form>
<?
function getRecords()
{
$sql = buildQuery();
$resource = mysql_query($sql);
}
while($row = mysql_fetch_array($resource)) { // The error is for this row
$results[] = $row;
}
return $results;
$records = getRecords(); {
foreach ($records as $record) {
}?>
<table>
<tbody>
<table border='1'>
<tr>
<td><?= $row['firstname']; ?></td>
<td><?= $row['lastname']; ?></td>
<td><?= $row['phone']; ?></td>
<td><?= $row['email']; ?></td>
<td><?= $row['department']; ?></td>
<td><?= $row['position']; ?></td>
<td>Return to Search</td>
</tr>
<? } ?>
</tbody>
</table>
</body>
</html>
You aren't getting any rows back. Try changing this
function getRecords()
{
$sql = buildQuery();
$resource = mysql_query($sql);
}
to
function getRecords()
{
$sql = buildQuery();
echo $sql;
exit();
$resource = mysql_query($sql);
}
this will output the SQL you are querying against the database. If it is not immediately apparent, copy and run this query against your database. See if any rows come back. If not then thats your problem!
Also you can use "echo mysql_error();" to get the text of the last error mysql threw.
EXAMPLE:
// Perform Query
$result = mysql_query($query);
// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
Your mysql query is not going through, or it is retuning 0 rows.
Try to put this
if(!$resource){
echo mysql_error();
echo ' <br>Query: .$sql;
}
statement directly after $resource = mysql_query($sql);
and see what it outputs. Also, make sure error reporting is turned on.
I edited my answer on your post yesterday, you might want to look at it; it may give you some ideas for a different approach.
The link is here:
Fatal error: Call to undefined function getRecords() in C:\xampp\htdocs\Employees.php on line 101
Maybe.. just maybe there is some problem with your code..
function getRecords()
{
$sql = buildQuery();
$resource = mysql_query($sql);
}
while($row = mysql_fetch_array($resource)) { // The error is for this row
$results[] = $row;
}
return $results;
my guess is that your above code should be something like this
function getRecords()
{
$sql = buildQuery();
$resource = mysql_query($sql);
$results = array();
if($resource != null)
{
while($row = mysql_fetch_array($resource))
{
$results[] = $row;
}
}
return $results;
}
Hope it helps :)
Please never do such thing:
mysql_query("INSERT INTO employeeinfo (firstname, lastname, phone, email, department, position) VALUES ('$_POST[firstname]', '$_POST[lastname]', '$_POST[phone]', '$_POST[email]', '$_POST[department]', '$_POST[position]')");
This will produce a HUGE hole in your site security called sql-injection.
You should always check any data gained from user.
The simple example of how it should be:
$firstname = mysql_real_escape_string($_POST['firstname']);
$lastname = mysql_real_escape_string($_POST['lastname']);
$phone = mysql_real_escape_string($_POST['phone']);
$email = mysql_real_escape_string($_POST['email']);
$department = mysql_real_escape_string($_POST['department']);
$position = mysql_real_escape_string($_POST['position']);
mysql_query("INSERT INTO employeeinfo (firstname, lastname, phone, email, department, position)
VALUES ('{$firstname}', '{$lastname}', '{$phone}', '{$email}', '{$department}', '{$position}')");
For more info read: http://php.net/manual/en/mysqli.real-escape-string.php
I think you should find a good book for novices in PHP and read at least from time to time it.
It will help you understand what you writing and how to make it better.
The first book that i googled for this purpose - http://www.amazon.com/Learning-MySQL-JavaScript-Step-Step/dp/0596157134/

Categories