954,585 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Select post from database and delete it?

I for the life of me can't get this to work and I get no errors.

admin.php

<html>
<head>
<title>Admin Area</title>
</head>
<body>
<? 
//connect to mysql
//change user and password to your mySQL name and password
$conn = mysql_connect("host","username","password");
if (!$conn)
  {
  die('Could not connect to the database: ' . mysql_error());
  }

mysql_select_db("database", $conn);

//If cmd has not been initialized
if(!isset($cmd)) 
{
   //display all the posts
   $result = mysql_query("SELECT * FROM posts ORDER BY id"); 
   
   //run the while loop that grabs all the post scripts
   while($r=mysql_fetch_array($result)) 
   { 
      //grab the title and the ID of the post
      $topic=$r["topic"];//take out the topic
	  $name=$r{"name"];//take out the name
      $id=$r["id"];//take out the id
     
	 //make the title a link
      echo "<a href='delete.php?cmd=delete&id=$id'>$title - Delete</a>";
      echo "";
    }
}
?>
</body>
</html>


delete.php

<?
if($_GET["cmd"]=="delete")
{
    $sql = "DELETE FROM posts WHERE id='$id'";
    $result = mysql_query($sql);
    echo "Post deleted!";
}
?>


Obviously I will not post my host information here so those are just placeholders for mysql and I know they work.

InsaneVr6
Light Poster
38 posts since Jul 2009
Reputation Points: 10
Solved Threads: 1
 

Your query might be failing

$result = mysql_query($sql);

Try

$result = mysql_query($sql);
if (!$result)
{
  echo mysql_error();
}
ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
 

I added the mysql error to that line and now it gives me an error at the very end of my code (which is

InsaneVr6
Light Poster
38 posts since Jul 2009
Reputation Points: 10
Solved Threads: 1
 

What's the error? (also, your code doesn't have an ending brace for the outer if)

ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
 

Thanks I fixed the bracket...

My error is

Could not delete: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
InsaneVr6
Light Poster
38 posts since Jul 2009
Reputation Points: 10
Solved Threads: 1
 

Does this error mean something is wrong on the server mysql end? Because I really don't see a flaw in the code.

InsaneVr6
Light Poster
38 posts since Jul 2009
Reputation Points: 10
Solved Threads: 1
 

Hey are you using mysql command line utility? Your text looks out to me. there is no 'd' in mysqld.sock

i.e /var/run/mysqld/mysql.sock

Not

/var/run/mysqld/mysqld.sock
toomuchfreetime
Light Poster
29 posts since Nov 2006
Reputation Points: 10
Solved Threads: 1
 

I get my hosting through 1and1.com and they use phpmyadmin to work with the databases.

So no I only use the command line interface on my local machine.

InsaneVr6
Light Poster
38 posts since Jul 2009
Reputation Points: 10
Solved Threads: 1
 

Shouldn't the delete.php page also be getting the $id

eg.

$_GET['id'] = $id;
glycerine
Newbie Poster
17 posts since Feb 2009
Reputation Points: 10
Solved Threads: 1
 

I tried that and I got the same result.

To be honest I thought this would be alot easier since it's just a mysql delete query. I just want to be able to see everything and pick the ones I want gone.

If vbulletin and other forums can do it WHY CAN'T I?

InsaneVr6
Light Poster
38 posts since Jul 2009
Reputation Points: 10
Solved Threads: 1
 

In delete.php do you actually have a mysql_connect statement?

From your code it would seem not...

Will Gresham
Master Poster
755 posts since May 2008
Reputation Points: 96
Solved Threads: 125
 

Try this as xan mentioned.

<html>
<head>
<title>Admin Area</title>
</head>
<body>
<? 
//connect to mysql
//change user and password to your mySQL name and password
$conn = mysql_connect("host","username","password");
if (!$conn)
  {
  die('Could not connect to the database: ' . mysql_error());
  }

mysql_select_db("database", $conn);

//If cmd has not been initialized
if(!isset($cmd)) 
{
   //display all the posts
   $result = mysql_query("SELECT * FROM posts ORDER BY id"); 
   
   //run the while loop that grabs all the post scripts
   while($r=mysql_fetch_array($result)) 
   { 
      //grab the title and the ID of the post
      $topic=$r["topic"];//take out the topic
	  $name=$r{"name"];//take out the name
      $id=$r["id"];//take out the id
     
	 //make the title a link
      echo "<a href='delete.php?cmd=delete&id=$id'>$title - Delete</a>";
      echo "";
    }
}
?>
</body>
</html>
<?
//connect to mysql
//change user and password to your mySQL name and password
$conn = mysql_connect("host","username","password");
if (!$conn)
  {
  die('Could not connect to the database: ' . mysql_error());
  }

mysql_select_db("database", $conn);

//select posts id from the url
$_GET['id'] = $id;

//if cmd is delete then delete the post from the table
if($_GET['cmd'] == "delete")
{
    $sql = "DELETE FROM posts WHERE id='$id'";
    $result = mysql_query($sql);
    echo "Post deleted!";
}
?>
glycerine
Newbie Poster
17 posts since Feb 2009
Reputation Points: 10
Solved Threads: 1
 

Jeez what a rookie mistake. I don't know why I thought I didn't need to connect to the database in the second script.

Thanks alot everyone problem solved!

InsaneVr6
Light Poster
38 posts since Jul 2009
Reputation Points: 10
Solved Threads: 1
 

<?php
$con = mysql_connect("localhost","root","Pass123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("skysharjah", $con);
if($_POST['tbl_latest_events'] != "")
{


$latest_event_title =$_POST['txtEvent Title'];
$latest_event_short_description =$_POST['txtEvent Short Description'];
$latest_event_description =$_POST['txtEvent Description'];
$latest_event_heldon =$_POST['intEvent Heldon'];


}

if($_GET['tbl_latest_event_id'] == 0 || $_GET['tbl_latest_event_id'] == "")
{
if ($_GET['method'] == "post")
{
$result =insert_tbl_latest_events($latest_event_title,$latest_event_short_description,$latest_event_description,$latest_event_heldon);
if ($result == true)
{
$showmessage = "Comments Posted successfully.";
//echo '';
}


}
}
?>

MAIN CENTRE - LATEST EVENT

Event Title

Event Short Description


Event Description

Event Heldon


              

gdya
Newbie Poster
1 post since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

whoops i did it wrong way round.

$id = $_GET['id'] ;
glycerine
Newbie Poster
17 posts since Feb 2009
Reputation Points: 10
Solved Threads: 1
 

Yeah I caught that but I didn't need that line anyway. Thanks I got everything sorted out. Much appreciated!

InsaneVr6
Light Poster
38 posts since Jul 2009
Reputation Points: 10
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You