Jump to content

Recommended Posts

I found this in tutorials http://www.phpfreaks.com/tutorial/simple-sql-search

and i liked the set up of that (thanks to who wrote it). 

 

I am making a "request off" db for work and i am looking for how to delete row buy row using sid

 

I have searched for it here and cant figure it out.

 

heres the page code i am usein to display it  and would like to know how to get delete to work let alone code it.

 

<?
ini_set("display_errors", "1");
error_reporting(E_ALL);

include("dbinfo.php");

mysql_connect('localhost',$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM simple_search ORDER BY sid";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();

$i=0;
while ($i < $num) {
$sid=mysql_result($result,$i,'sid');
$stitle=mysql_result($result,$i,'stitle');
$sdescription=mysql_result($result,$i,'sdescription');
$sbody=mysql_result($result,$i,'sbody');


?>
<div align="center">

<br />
<br />
<? echo $sid; ?>  
<?php echo $stitle; ?>  
<?php echo $sdescription; ?>  
<?php echo $sbody; ?>   
<? echo "<a href='link.for.delete.code.will.go.here'><input name='' type='button' value='delete'/></a>";   ?>
<?
$i++;}
?>

 

this is code that i have found and was trying to use for delete:

 

<?php  
include "dbinfo.php"; 
// if id provided, then delete that record  
$sid=$_GET['sid'] ;  
// create query to delete record  
$query = "DELETE FROM simple_search WHERE id = '$sid' ";  
$result = mysql_query($query) or die ("epic FAIL!")
?>

Link to comment
https://forums.phpfreaks.com/topic/186499-delete-function/
Share on other sites

<?
ini_set("display_errors", "1");
error_reporting(E_ALL);

include("dbinfo.php");

mysql_connect('localhost',$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

if (isset($_GET['delete'])) {
$query = "DELETE FROM simple_search WHERE id = '".$_GET['delete']."'";  
$result = mysql_query($query) or die ("epic FAIL!")
}
$query="SELECT * FROM simple_search ORDER BY sid";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();

$i=0;
while ($i < $num) {
$sid=mysql_result($result,$i,'sid');
$stitle=mysql_result($result,$i,'stitle');
$sdescription=mysql_result($result,$i,'sdescription');
$sbody=mysql_result($result,$i,'sbody');


?>
<div align="center">

<br />
<br />
<? echo $sid; ?> 
<?php echo $stitle; ?>  
<?php echo $sdescription; ?> 
<?php echo $sbody; ?>  
<? echo "<a href=nameofthisscript.php?delete=$sid'><input name='' type='button' value='delete'/></a>";   ?>
<?
$i++;}
?>

may work. its not tested tho :)

Link to comment
https://forums.phpfreaks.com/topic/186499-delete-function/#findComment-984893
Share on other sites

There are a few different issues.

 

1. That code is way more complex than it needs to be. There is a lot of "logic" in there to extract and display the records that is not needed. A simple while loop with mysql_fetch_assoc(), or similar function will provide all the logic for stepping through the records and providing variables to display.

 

2. You are putting a button withing anchor tags. That doesn't work. You would either need to a) create a standard text link or b) create mini forms with the buttons as submit buttons. I'll provide an example with just text links. There is a third option of using JavaScript onthe buttons, but that again introduces unneeded complexity.

 

3. That code is using short tags (i.e. <?). That's not good programming standard as not all servers will support that.

 

4. There are problems with the HTML. For instance, each loop is opening a new DIV but never closing them.

 

<?php

ini_set("display_errors", "1");
error_reporting(E_ALL);
include("dbinfo.php");

mysql_connect('localhost',$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query = "SELECT `stitle`, `sdescription`, `sbody`, `sid` FROM simple_search ORDER BY sid";
$result = mysql_query($query);

if (!$result)
{
    //Temporary error handling
    echo "There was a problem:<br />".mysql_error();
}

while($record = mysql_fetch_assoc($result))
{
    echo "<br /><br />";
    echo "{$record['sid']} {$record['stitle']} {$record['sdescription']} {$record['sbody']} ";
    echo "<a href=\"deletePage.php?sid={{$record['sid']}}\">Delete</a>\n";
}

?>

Link to comment
https://forums.phpfreaks.com/topic/186499-delete-function/#findComment-984906
Share on other sites

code for display:

<?php

ini_set("display_errors", "1");
error_reporting(E_ALL);
include("dbinfo.php");

mysql_connect('localhost',$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query = "SELECT `stitle`, `sdescription`, `sbody`, `sid` FROM simple_search ORDER BY sid";
$result = mysql_query($query);

if (!$result)
{
    //Temporary error handling
    echo "There was a problem:<br />".mysql_error();
}

while($record = mysql_fetch_assoc($result))
{
    echo "<br /><br />";
    echo "{$record['sid']} {$record['stitle']} {$record['sdescription']} {$record['sbody']} ";
    echo "<a href=\"deletepage.phpl?sid={{$record['sid']}}\">Delete</a>\n";
}

?>

 

deletepage.php

<?php  
include "dbinfo.php"; 
// if id provided, then delete that record  
$sid=$_GET['sid'] ;  
// create query to delete record  
$query = "DELETE FROM simple_search WHERE id = '$sid' ";  
$result = mysql_query($query) or die ("epic FAIL!")
?>

 

Link to comment
https://forums.phpfreaks.com/topic/186499-delete-function/#findComment-984919
Share on other sites

<?php  
ini_set("display_errors", "10");
error_reporting(E_ALL);
include "dbinfo.php";
mysql_connect('localhost',$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
// if id provided, then delete that record  
$sid=$_GET['sid'] ;  
// create query to delete record  
$query = "DELETE FROM simple_search WHERE sid = '$sid' "; 
$result = mysql_query($query) or die ("epic FAIL because: ".mysql_error());
?>

 

once you click delete from the "display" page it takes you to the page this code is on.

Link to comment
https://forums.phpfreaks.com/topic/186499-delete-function/#findComment-984957
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.