Jump to content

error in this code?


roldahayes

Recommended Posts

can anyone help with why this only returns a blank page please:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<SCRIPT language="JavaScript">
function confirmDelete(id) {
if (confirm("Are you sure you wish to delete this?")) {
window.location.href = "deleteItem.php?id=" + id;
}
}
//--> 
</SCRIPT>
<title>List</title>
<meta http-equiv="content-type"
    content="text/html; charset=iso-8859-1" />

<link href="../css/admin.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>List</h1>
<p><a href="newItem.php">Add new item</a></p>
<form action="index.php" method="post">
<p>Search:<input type="text" name="searchtext" /></p>
<input type="submit" value="Search" />
</form>
<?php
// function to make connection to MySQL database
include('../dbcnt.php'); 

$db_conn = db_connect(); // database connection

// The basic SELECT statement
$select = 'SELECT DISTINCT id, sup_name, sup_address, sup_postcode, sup_postcode_2, sup_telephone';
$from   = ' FROM suppliers';
$where  = ' WHERE 1=1';

$searchtext = $_POST['searchtext'];
if ($searchtext != '') { // Some search text was specified
  $where .= " AND (sup_name LIKE '%$searchtext%' OR sup_address LIKE '%$searchtext%' OR sup_postcode LIKE '%$searchtext%' OR sup_postcode_2 LIKE '%$searchtext%' OR sup_telephone LIKE '%$searchtext%')";
}

?>

<table>
<tr><th>No.</th><th>Name</th><th>Address</th><th>Postcode</th><th>Telephone</th><th>Options</th></tr>

<?php
$suppliers = @mysql_query($select . $from . $where );
if (!$suppliers) {
  echo '</table>';
  exit('<p>Error retrieving suppliers from database!<br />'.
      'Error: ' . mysql_error() . '</p>');
}

while ($supply = mysql_fetch_array($suppliers)) {
  echo "<tr valign='top'>\n";
  $id = $supply['id'];
  $sup_name = htmlspecialchars($supply['sup_name']);
   $sup_address = htmlspecialchars($supply['sup_address']);
  	 $sup_postcode = htmlspecialchars($supply['sup_postcode']);
    	  $sup_postcode_2 = htmlspecialchars($supply['sup_postcode_2']);
  		 $sup_telephone = htmlspecialchars($supply['sup_telephone']);
  echo "<td>$id</td>\n";
  echo "<td>$sup_name</td>\n";
    echo "<td>$sup_address</td>\n";
    echo "<td>$sup_postcode $sup_postcode_2</td>\n";
		echo "<td>$sup_telephone</td>\n";
		  echo "<td><a href='editItem.php?id=$id'>Edit</a> | " .
      "<a href='javascript:confirmDelete($id)'>Delete</a></td>\n";

  echo "</tr>\n";
}
?>
</table>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/124653-error-in-this-code/
Share on other sites

with errors on I'm getting this

 

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'username'@'localhost' (using password: YES) in /home/default/test.viewmykitchen.com/user/htdocs/dbcnt.php on line 11

I cannot connect to the database because: Access denied for user 'username'@'localhost' (using password: YES)

Link to comment
https://forums.phpfreaks.com/topic/124653-error-in-this-code/#findComment-643836
Share on other sites

okey dokey...

 

Sort out the connection problem....

 

Now getting this error

 

Notice: Undefined index: searchtext in /home/default/test.viewmykitchen.com/user/htdocs/admin/index.php on line 41

 

line 41 is here:

 

// The basic SELECT statement

$select = 'SELECT DISTINCT id, sup_name, sup_address, sup_postcode, sup_postcode_2, sup_telephone';

$from   = ' FROM suppliers';

$where  = ' WHERE 1=1';

 

$searchtext = $_POST['searchtext'];

if ($searchtext != '') { // Some search text was specified

  $where .= " AND (sup_name LIKE '%$searchtext%' OR sup_address LIKE '%$searchtext%' OR sup_postcode LIKE '%$searchtext%' OR sup_postcode_2 LIKE '%$searchtext%' OR sup_telephone LIKE '%$searchtext%')";

}

 

 

Link to comment
https://forums.phpfreaks.com/topic/124653-error-in-this-code/#findComment-643843
Share on other sites

Your post variable isn't set. Maybe change the if statement like this:

 

$searchtext = $_POST['searchtext'];
if (isset($_POST['searchtext'])) { // Some search text was specified
  $searchtext = $_POST['searchtext'];
  $where .= " AND (sup_name LIKE '%$searchtext%' OR sup_address LIKE '%$searchtext%' OR sup_postcode LIKE '%$searchtext%' OR sup_postcode_2 LIKE '%$searchtext%' OR sup_telephone LIKE '%$searchtext%')";
}

Link to comment
https://forums.phpfreaks.com/topic/124653-error-in-this-code/#findComment-643847
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.