Jump to content

[SOLVED] delete and edit


aeafisme23

Recommended Posts

Database Structure:

fields: id, areacode, dealer, dealerinfo

 

test link:  http://thegreatestsave.org/ut/search.php    (574 and 765, 456 are the only working ones i put for test purposes) added from a form from here http://thegreatestsave.org/ut/add_record.html (no validation on it yet so if your going to test it please fill it out all the way until i do come back to it).

 

Basically i have no clue on how to get it to delete the individual record when hitting delete and the more i look at other people code and manipulate the more i get confused. Any help would be appreciated

 

search.php

<?php
//Get variables from config.php to connect to mysql server
require 'config.php';

// connect to the mysql database server.
mysql_connect ($dbhost, $dbusername, $dbuserpass);
//select the database
mysql_select_db($dbname) or die('Cannot select database');

//search variable = data in search box or url
if(isset($_GET['search']))
{
$search = $_GET['search'];
}

//trim whitespace from variable
$search = trim($search);
$search = preg_replace('/\s+/', ' ', $search);

//seperate multiple keywords into array space delimited
$keywords = explode(" ", $search);

//Clean empty arrays so they don't get every row as result
$keywords = array_diff($keywords, array(""));

//Set the MySQL query
if ($search == NULL or $search == '%'){
} else {
for ($i=0; $i<count($keywords); $i++) {
$query = "SELECT * FROM dealer WHERE areacode = '$keywords[$i]'";
}

//Store the results in a variable or die if query fails
$result = mysql_query($query) or die(mysql_error());
}
if ($search == NULL or $search == '%'){
} else {
//Count the rows retrived
$count = mysql_num_rows($result);
}

echo "<html>";
echo "<head>";
echo "<title>search</title>";
echo "</head>";
echo "<body onLoad=\"self.focus();document.searchform.search.focus()\">";
echo "<center>";
echo "<br /><form name=\"searchform\" method=\"GET\" action=\"search.php\">";
echo "<input type=\"text\" name=\"search\" size=\"20\" TABINDEX=\"1\" />";
echo " <input type=\"submit\" value=\"Search\" />";
echo "</form>";
//If search variable is null do nothing, else print it.
if ($search == NULL) {
} else {
echo "You searched for <b><FONT COLOR=\"blue\">";
foreach($keywords as $value) {
   print "$value ";
}
echo "</font></b>";
}
echo "<p> </p><br />";
echo "</center>";

//If users doesn't enter anything into search box tell them to.
if ($search == NULL){
echo "<center><b><FONT COLOR=\"red\">Please enter a search parameter to continue.</font></b><br /></center>";
} elseif ($search == '%'){
echo "<center><b><FONT COLOR=\"red\">Please enter a search parameter to continue.</font></b><br /></center>";
//If no results are returned print it
} elseif ($count <= 0){
echo "<center><b><FONT COLOR=\"red\">Your query returned no results from the database. (redirect goes here)</font></b><br /></center>";
//ELSE print the data in a table
} else {
//Table header
echo "<center><table width=\"680\" id=\"search\" bgcolor=\"#AAAAAA\">";
echo "<tr>";
echo "<td width=\"100\" valign=\"top\"><b>Area Code</b></td>";
echo "<td width=\"150\" valign=\"top\"><b>Dealer</b></td>";
echo "<td width=\"300\" valign=\"top\"><b>Dealer Info</b></td>";
echo "<td width=\"65\" valign=\"top\"> </td><td width=\"65\" valign=\"top\"> </td><tr>";
echo "</table></center>";

//Colors for alternation of row color on results table
$color1 = "#d5d5d5";
$color2 = "#e5e5e5";
//While there are rows, print it.
while($row = mysql_fetch_array($result))
{
//Row color alternates for each row
$row_color = ($row_count % 2) ? $color1 : $color2;
//table background color = row_color variable
echo "<center><table width=\"680\" bgcolor=".$row_color.">";
echo "<tr>";
echo "<td width=\"100\" valign=\"top\">".$row['areacode']."</td>";
echo "<td width=\"150\" valign=\"top\">".$row['dealer']."</td>";
echo "<td width=\"300\" valign=\"top\">".$row['dealerinfo']."</td>"; 
echo "<td width=\"65\" valign=\"top\"><a href=\"#\">Edit</a></td><td width=\"65\" valign=\"top\"><a href=\"delete_record.php?id=$id\">Delete</a></td></tr>";
echo "</table></center>";
$row_count++;
//end while
}
//end if
}

echo "</body>";
echo "</html>";
if ($search == NULL or $search == '%') {
} else {
//clear memory
mysql_free_result($result);
}
?>

 

delete_record.php

<?php
$con = mysql_connect("localhost","x","x");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }mysql_select_db("x", $con);

mysql_query("DELETE FROM dealer WHERE id=$id");mysql_close($con);
?>
you deleted results (and how do i show them ?)

 

areas i believe im doing wrong are not declaring the right variable to pass to delete_record.php and then correctly coding delete_record.php.

Link to comment
Share on other sites

Heres a little cleanup

 

search.php

<?php
//Get variables from config.php to connect to mysql server
require 'config.php';

// connect to the mysql database server.
if( !mysql_connect($dbhost, $dbusername, $dbuserpass) ) {
die("Error connecting to mysql host<br />".mysql_error());
}

//select the database
if( !mysql_select_db($dbname) ) {
die("Cannot select database<br />".mysql_error());
}

//trim whitespace from variable
$_GET['search'] = preg_replace('/\s+/', ' ', trim($_GET['search']));

//seperate multiple keywords into array space delimited
$keywords = array_diff(explode(" ", $_GET['search']), array(""));

if( !empty($_GET['search']) ) {
foreach($keywords as $k => $v) 
	$keywords[$k] = int($v);

$codes = implode(",", $keywords);

//Store the results in a variable or die if query fails
if( !($result = mysql_query("SELECT * FROM dealer WHERE areacode IN ($codes)") ) {
	die("Error executing MySQL query<br />".mysql_error());
}
$count = mysql_num_rows($result);
}

/** html code **/
echo "You searched for $codes<br />";

//If users doesn't enter anything into search box tell them to.
if( empty($_GET['search']) ){
echo "Enter something...";
} else {
if( $count == 0 ){
	echo "Your query returned no results from the database";
} else {
	$row_count = 0;
	while( $row = mysql_fetch_array($result) ) {
		//table background color = row_color variable
		echo "<center><table width=\"680\" bgcolor=".($row_count % 2) ? $color1 : $color2.">";
		echo "<tr>";
		echo "<td width=\"100\" valign=\"top\">".$row['areacode']."</td>";
		echo "<td width=\"150\" valign=\"top\">".$row['dealer']."</td>";
		echo "<td width=\"300\" valign=\"top\">".$row['dealerinfo']."</td>"; 
		echo "<td width=\"65\" valign=\"top\"><a href=\"#\">Edit</a></td><td width=\"65\" valign=\"top\"><a href=\"delete_record.php?id=$id\">Delete</a></td></tr>";
		echo "</table></center>";
		$row_count++;
	}
}
}

if( isset($result) ) {
mysql_free_result($result);
}
?>

 

With delete.php, it shouldn't be hard at all.

 

1) Get variable, $_GET['id'], cast it as an integer

2) Check to see if dealer exists, if so then 3 else 4

3) Delete dealer

4) Tell user there is no dealer

<?php
/** mysql stuff **/
$id = int($_GET['id']);

if( !($result = mysql_query("SELECT id FROM dealers WHERE id = $id")) ) {
    die("MySQL Error : ".mysql_error());
}

if( mysql_num_rows($result) == 0 ) {
   print "No such dealer";
} else {
    mysql_query("DELETE FROM dealers WHERE id=$id");
}
/** ... **/
?>

Link to comment
Share on other sites

Thanks for the repost, I am getting an error:

Parse error: syntax error, unexpected '{' in /home/thegr29/public_html/ut/search.php on line 29

 

it's saying that {  is structured wrong somehow and i guess i can't see where im missing or add one too many {, any help would be so much appreciated

 

 

this is the newly implemented code:

 

<?php
//Get variables from config.php to connect to mysql server
require 'config.php';

// connect to the mysql database server.
if( !mysql_connect($dbhost, $dbusername, $dbuserpass) ) {
die("Error connecting to mysql host<br />".mysql_error());
}

//select the database
if( !mysql_select_db($dbname) ) {
die("Cannot select database<br />".mysql_error());
}

//trim whitespace from variable
$_GET['search'] = preg_replace('/\s+/', ' ', trim($_GET['search']));

//seperate multiple keywords into array space delimited
$keywords = array_diff(explode(" ", $_GET['search']), array(""));

if( !empty($_GET['search']) ) {
foreach($keywords as $k => $v) 
	$keywords[$k] = int($v);

$codes = implode(",", $keywords);

//Store the results in a variable or die if query fails
if( !($result = mysql_query("SELECT * FROM dealer WHERE areacode IN ($codes)") ) 
{
	die("Error executing MySQL query<br />".mysql_error());
}
$count = mysql_num_rows($result);
}

/** html code **/
echo "You searched for $codes<br />";

//If users doesn't enter anything into search box tell them to.
if( empty($_GET['search']) ){
echo "Enter something...";
} else {
if( $count == 0 ){
	echo "Your query returned no results from the database";
} else {
	$row_count = 0;
	while( $row = mysql_fetch_array($result) ) {
		//table background color = row_color variable
		echo "<center><table width=\"680\" bgcolor=".($row_count % 2) ? $color1 : $color2.">";
		echo "<tr>";
		echo "<td width=\"100\" valign=\"top\">".$row['areacode']."</td>";
		echo "<td width=\"150\" valign=\"top\">".$row['dealer']."</td>";
		echo "<td width=\"300\" valign=\"top\">".$row['dealerinfo']."</td>"; 
		echo "<td width=\"65\" valign=\"top\"><a href=\"#\">Edit</a></td>
		<td width=\"65\" valign=\"top\"><a href=\"delete_record.php?id=$id\">Delete</a></td></tr>";
		echo "</table></center>";
		$row_count++;
	}
}
}

if( isset($result) ) {
mysql_free_result($result);
}
?>

Link to comment
Share on other sites

should be

	$result = mysql_query("SELECT * FROM dealer WHERE areacode IN ($codes)");
if(!$result) 
{
	die("Error executing MySQL query<br />".mysql_error());
}
$count = mysql_num_rows($result);

not

if( !($result = mysql_query("SELECT * FROM dealer WHERE areacode IN ($codes)") ) 
{
	die("Error executing MySQL query<br />".mysql_error());
}
$count = mysql_num_rows($result);

Link to comment
Share on other sites

Thanks for clearing up that problem, but now another:

It happens when i put in a value into the text box, you can see it here to test: http://www.thegreatestsave.org/ut/search.php

 

Fatal error: Call to undefined function: int() in /home/thegr29/public_html/ut/search.php on line 23

 

//seperate multiple keywords into array space delimited
$keywords = array_diff(explode(" ", $_GET['search']), array(""));

if( !empty($_GET['search']) ) {
foreach($keywords as $k => $v) 
	$keywords[$k] = int($v);

$codes = implode(",", $keywords);

//Store the results in a variable or die if query fails
$result = mysql_query("SELECT * FROM dealer WHERE areacode IN ($codes)");
if(!$result) 
{
	die("Error executing MySQL query<br />".mysql_error());
}
$count = mysql_num_rows($result);
}

Link to comment
Share on other sites

Thanks teng84, i know you probably hate when people repost so much but i really think that this will be the last one consider what you did last fixed search.php, now delete.php is having that int problem. first to recap:

 

Fatal error: Call to undefined function: int() in /home/thegr29/public_html/ut/delete_record.php on line 11

 

$id = int($_GET['id']);

 

i then changed it to both $id = (int)($_GET['id']); and $id = (int($_GET['id'])); and each came with a mysql error. I usuallly dont have so many problems but ive never used int in code before. Thanks again teng84 and others for your help, its very much appreciated.

 

 

Link to comment
Share on other sites

http://www.thegreatestsave.org/ut/search.php

 

if you put in 574 , and delete a record it goes to delete_record.php and it says No such dealer.  and it is not deleting. I will paste complete code one for search.php and delete_record.php, i believe the problem lies in delete_record.php.... ahhhhhh

 

<?php
//Get variables from config.php to connect to mysql server
require 'config.php';

// connect to the mysql database server.
if( !mysql_connect($dbhost, $dbusername, $dbuserpass) ) {
die("Error connecting to mysql host<br />".mysql_error());
}

//select the database
if( !mysql_select_db($dbname) ) {
die("Cannot select database<br />".mysql_error());
}

//trim whitespace from variable
$_GET['search'] = preg_replace('/\s+/', ' ', trim($_GET['search']));

//seperate multiple keywords into array space delimited
$keywords = array_diff(explode(" ", $_GET['search']), array(""));

if( !empty($_GET['search']) ) {
foreach($keywords as $k => $v) 
$keywords[$k] = (int)$v;	
$codes = implode(",", $keywords);

//Store the results in a variable or die if query fails
$result = mysql_query("SELECT * FROM dealer WHERE areacode IN ($codes)");
if(!$result) 
{
	die("Error executing MySQL query<br />".mysql_error());
}
$count = mysql_num_rows($result);
}

/** html code **/
echo "You searched for $codes<br />";

//If users doesn't enter anything into search box tell them to.
if( empty($_GET['search']) ){
echo "<html><head>";
echo "<title>search</title>";
echo "</head>";
echo "<body onLoad=\"self.focus();document.searchform.search.focus()\">";
echo "<center>";
echo "<br /><form name=\"searchform\" method=\"GET\" action=\"search.php\">";
echo "<input type=\"text\" name=\"search\" size=\"20\" TABINDEX=\"1\" />";
echo " <input type=\"submit\" value=\"Search\" />";
echo "</form>";
} else {
if( $count == 0 ){
	echo "Your query returned no results from the database";
} else {
	$row_count = 0;
	while( $row = mysql_fetch_array($result) ) {
		//table background color = row_color variable
		echo "<center><table width=\"680\" bgcolor=".($row_count % 2) ? $color1 : $color2.">";
		echo "<tr>";
		echo "<td width=\"100\" valign=\"top\">".$row['areacode']."</td>";
		echo "<td width=\"150\" valign=\"top\">".$row['dealer']."</td>";
		echo "<td width=\"300\" valign=\"top\">".$row['dealerinfo']."</td>"; 
		echo "<td width=\"65\" valign=\"top\"><a href=\"#\">Edit</a></td>
		<td width=\"65\" valign=\"top\"><a href=\"delete_record.php?id=$id\">Delete</a></td></tr>";
		echo "</table></center>";
		$row_count++;
	}
}
}

if( isset($result) ) {
mysql_free_result($result);
}
?>

 

<?php
//Get variables from config.php to connect to mysql server
require 'config.php';
// connect to the mysql database server.
if( !mysql_connect($dbhost, $dbusername, $dbuserpass) ) {
die("Error connecting to mysql host<br />".mysql_error());
}
//select the database
if( !mysql_select_db($dbname) ) {
die("Cannot select database<br />".mysql_error());
}



$id = ((int)($_GET['id']));
if( !($result = mysql_query("SELECT id FROM dealer WHERE id = $id")) ) {
    die("MySQL Error : ".mysql_error());
}

if( mysql_num_rows($result) == 0 ) {
   print "No such dealer";
} else {
    mysql_query("DELETE FROM dealer WHERE id=$id");
}
/** ... **/
?>

Link to comment
Share on other sites

it will not delete because there is no value for your id

 

seee this line

echo "<td width=\"65\" valign=\"top\"><a href=\"#\">Edit</a></td>
		<td width=\"65\" valign=\"top\"><a href=\"delete_record.php?id=$id\">Delete</a></td></tr>";

 

you have a variable $id which i think you dont set?

Link to comment
Share on other sites

ok so i see definitely what you are talking about

 

http://www.thegreatestsave.org/ut/delete_record.php?id=

 

its not passing the auto increming id in my database, if i manually put in anumber like 5 for instance it will erase it from the database through my control panel so i know the code works in delete_records.php, but

 

<a href=\"delete_record.php?id=$id\">[code]

is not pulling the id. is this something i need to use a isset($id) at the top of the mysql on search.php?

[/code]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

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