Jump to content

Recommended Posts

I was wondering how to have an link near the row that allows you to delete it?

This is my code

 

<?php include 'header.php';  // Works.


?>
<div id="content">
   <div id="columnsC">

      <img  style="border:0;" src="siteimg/stixy.jpg" alt="stixy"/><Br/>
<p>Stixy helps users organize their world on flexible, shareable Web-based bulletin boards called Stixyboards. Unlike most personal productivity or project management software, Stixy doesn't dictate how users should organize their information. Users can create tasks, appointments, files, photos, notes, and bookmarks on their Stixyboards, organized in whatever way makes sense to them. Then they can share Stixyboards with friends, family, and colleagues.</p>
   </div>
<div id="columnsD">
<?
/**
* User has already logged in, so display relavent links
*/
echo '
';
if($session->logged_in){
$dbh=mysql_connect ("localhost", "my_user", "my_pass") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("my_db");
if(isset($_GET['remove']) && $_GET['remove'] == '1'){
    $sql = "DELETE FROM emailists WHERE stixy=('" . $session->userinfo['email'] . "')";
        if($sqlres = mysql_query($sql)){
            echo '<div id="mes"><ul><li>email address removed</li></ul></div>';
        }else{
            echo mysql_error();
        }
}
//break
if(isset($_GET['insert']) && $_GET['insert'] == 1) {
  $sql = "SELECT stixy FROM emailists WHERE stixy = '{$session->userinfo['email']}'";
  if ($result = mysql_query($sql)) {
    if (!mysql_num_rows($result)) {
      $sql = "INSERT INTO emailists (stixy) VALUES ('" . $session->userinfo['email'] . "')";
      if ($sqlres = mysql_query($sql)){
        echo '<div id="mes"><ul><li>email address added</li></ul></div>';
      } else {
        echo mysql_error();
      }
    } else {
      echo "<div id='mes'><ul><li>email address already exists</li></ul></div>";
    }
  } else {
    echo mysql_error();
  }
}
$query = mysql_query("SELECT stixy FROM emailists WHERE stixy = '{$session->userinfo['email']}'");

$result = mysql_num_rows($query);

if($result !== 0){

echo "<ul><li><a href='stixy.php?remove=1'>Remove yourself from the list[/url]</li></ul>";

} else {

echo "<ul><li><a href='stixy.php?insert=1'>Add yourself to the list[/url]</li></ul>";

}

echo "<ul>";
  $sql = "SELECT stixy FROM emailists";
  if ($result = mysql_query($sql)) {
    if (mysql_num_rows($result)) {
      while ($row = mysql_fetch_assoc($result)) {
        echo "<li>";  echo $row['stixy'] . "</li>";
       
      }
    }
  }
echo "</ul>";

}
else{
?>


<p style="border-style: solid;
border-width: 3px;">some text here</p>

<?
}
?>

</div>



   <?php include 'sidebar.php'; ?>
   <div style="clear: both;"> </div>
</div>
<?php include 'footer.php';  // Works.

?>

Link to comment
https://forums.phpfreaks.com/topic/62725-deleting-a-mysql-row-php/
Share on other sites

I believe the relevant line of code, (the bit that displays the items you'd like the delete link to appear next to), is this one?

 

echo "<li>";  echo $row['stixy'] . "</li>";

 

 

An example of what you're trying to accomplish may be...

 

echo "<li>".$row['stixy']."  <a href='delete.php?Item=".$row['stixy']."'>delete record</a>";

 

Of course, the above pre-supposes that you've got a script called 'delete.php' that can take "Item" as an argument, and that will use that argument to reference the record in the database that you want to delete, and then delete it...I really hope this makes sense!

You could have it on the same page, but that might get a bit crowded.  It would go something like this, (I'm creating my own example here, hopefully you can adapt this for yourself.)

 

 


<?php





if ($_SERVER['REQUEST_METHOD']=="GET" && $_GET['Item']!="") //If we are passing $GET variables to our script via a URL...
                                                            //And the "Item" $GET variable is not blank, we know that the 
														//user has clicked one of the "Delete" links.

            {
		     //Create the SQL Delete Statement:

			 $SQL="DELETE FROM stixy WHERE stixy='".$_GET['Item'};


			 $result=mysql_query($SQL); //Run the SQL...


			 //Done.



		}


echo "<li>".$row['stixy']."  <a href='samepage.php?Item=".$row['stixy']."'>delete record</a>"; //We're linking to the same page we're on, but with $GET variables in our URL...see above


?>



You could have it on the same page, but that might get a bit crowded.  It would go something like this, (I'm creating my own example here, hopefully you can adapt this for yourself.)

 

 


<?php





if ($_SERVER['REQUEST_METHOD']=="GET" && $_GET['Item']!="") //If we are passing $GET variables to our script via a URL...
                                                            //And the "Item" $GET variable is not blank, we know that the 
														//user has clicked one of the "Delete" links.

            {
		     //Create the SQL Delete Statement:

			 $SQL="DELETE FROM stixy WHERE stixy='".$_GET['Item'};


			 $result=mysql_query($SQL); //Run the SQL...


			 //Done.



		}


echo "<li>".$row['stixy']."  <a href='samepage.php?Item=".$row['stixy']."'>delete record</a>"; //We're linking to the same page we're on, but with $GET variables in our URL...see above


?>



 

Could you make it so it echoed something if everything worked?

Also I'm not very good with PHP so can you adapt it?

Ok I integrated it into my site but you script does not work  :'(

 

Actually, it's your script.  Beginner or not, try to solve your own problems. Post your error-free code and explain in detail what does or does not work the way you want/expect. And stop bumping your threads. if you want 'instant' help, hire someone.

i think the last post have an idea that you need

and whats the problem with that the only error i saw with the last post is this

$SQL="DELETE FROM stixy WHERE stixy='".$_GET['Item'};

and i hope you know the error or maybe its not

 

the sample uses stixy for table name and field is that the way you define it to your db

 

can you tell us whats wrong

Here is the general idea:

 

<?php include 'header.php'; ?>
<div id="content">
<div id="columnsC">
	<img  style="border:0;" src="siteimg/stixy.jpg" alt="stixy"/><Br/>
	<p>
		Stixy helps users organize their world on flexible, shareable Web-based bulletin 
		boards called Stixyboards. Unlike most personal productivity or project management 
		software, Stixy doesn't dictate how users should organize their information. Users 
		can create tasks, appointments, files, photos, notes, and bookmarks on their 
		Stixyboards, organized in whatever way makes sense to them. Then they can share 
		Stixyboards with friends, family, and colleagues.
	</p>
</div>
<div id="columnsD">
<?php
if ($session->logged_in) {
	$dbh = mysql_connect ("localhost", "my_user", "my_pass") or die (mysql_error());
	mysql_select_db ("my_db");

	if (base64_decode($_GET['stixy']) != "" && $_GET['delete'] == "true") {
		$query = "DELETE FROM stixy WHERE stixy = '" . base64_decode($_GET['stixy']) . "'";
		mysql_query($query) or die(mysql_error());
		echo "Stixy " . base64_deocde($_GET['stixy']) . " was deleted!!";
	}

	if ($_GET['remove'] == '1'){
		$sql = "DELETE FROM emailists WHERE stixy = '" . $session->userinfo['email'] . "'";

		if (mysql_query($sql)){
			echo '<div id="mes"><ul><li>email address removed</li></ul></div>';
		} else {
			echo mysql_error();
		}
	}

	if (isset($_GET['insert']) && $_GET['insert'] == 1) {
		$sql = "SELECT count(stixy) FROM emailists WHERE stixy = '" . $session->userinfo['email'] . "'";

		if ($result = mysql_query($sql)) {
			if (mysql_result($result, 0) == 0) {
				$sql = "INSERT INTO emailists (stixy) VALUES ('" . $session->userinfo['email'] . "')";

				if (mysql_query($sql)){
					echo '<div id="mes"><ul><li>email address added</li></ul></div>';
				} else {
					echo mysql_error();
				}
			} else {
				echo "<div id='mes'><ul><li>email address already exists</li></ul></div>";
			}
		} else {
			echo mysql_error();
		}
	}

	$result = mysql_query("SELECT COUNT(stixy) FROM emailists WHERE stixy = '" . $session->userinfo['email'] . "'");

	if (mysql_result($result, 0) == 0) {
		echo "<ul><li><a href='stixy.php?remove=1'>Remove yourself from the list</a></li></ul>";
	} else {
		echo "<ul><li><a href='stixy.php?insert=1'>Add yourself to the list</a></li></ul>";
	}


	$sql = "SELECT stixy FROM emailists";
	$result = mysql_query($sql) or die(mysql_error());

	if (mysql_num_rows($result) > 0) {
		echo "<ul>";
		while ($row = mysql_fetch_assoc($result)) {
			echo '
				<li>
					' . $row['stixy'] . ' 
					<a href="' . $_SERVER['PHP_SELF'] . '?stixy=' . base64_encode($row['stixy']) . '&delete=true"' .
					' onclick="return confirm(\'Are you sure?!?!\');">Delete</a>
				</li>';
		}
		echo "</ul>";
	}


} else {
echo '
	<p style="border-style: solid; border-width: 3px;">some text here</p>';
}
?>

</div>



   <?php include 'sidebar.php'; ?>
   <div style="clear: both;"> </div>
</div>
<?php include 'footer.php'; ?>

Ok I'm now getting some error's :(

when i got to stixy.php it gives me these

Notice: Undefined index: stixy in /home/nathan/public_html/stixy.php on line 20

Notice: Undefined index: remove in /home/nathan/public_html/stixy.php on line 26

 

and when I say delete it says

 

Table 'nathan_swapinvites.stixy' doesn't exist

 

using the script that hitman6003 gave me

Notices don't mean anything is really wrong...turn them off by putting

 

error_reporting(E_ALL ^ E_NOTICE);

 

at the top of your page.  In this case, you are getting those because it's putting things into an array before the array has been defined.

 

The other error has to do with your database.  I can't help you there...the error should be fairly self explanatory.

Guest
This topic is now 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.