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 "<br>";
    }
}
?>
</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.

Recommended Answers

All 15 Replies

Your query might be failing

$result = mysql_query($sql);

Try

$result = mysql_query($sql);
if (!$result)
{
  echo mysql_error();
}

I added the mysql error to that line and now it gives me an error at the very end of my code (which is </html>). I know this happens alot but now I'm stuck again. Index code did not change but here is the new delete.php

<html>
<head>
<title>Deleted</title>
</head>
<body>
<?
if($_GET["cmd"]=="delete")
{
    $sql = "DELETE FROM posts WHERE id='$id'";
$result = mysql_query($sql);
if (!$result)
  {
  die('Could not delete: ' . mysql_error());
  }
else {
    echo "Post deleted!";
}
?>
</body>
</html>

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

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)

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

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

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.

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

eg.

$_GET['id'] = $id;

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?

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

From your code it would seem not...

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 "<br>";
    }
}
?>
</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!";
}
?>

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!

<?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 '<meta http-equiv="refresh"' . 'content="0;URL=commentsuccess.php?employer_id=' .$employer_id .'">';
                 }


                 }
                 } 
?>
        <body style ="background-color:;">
        <fieldset>
        <legend><h1>MAIN CENTRE - LATEST EVENT</h1></legend>
     <FORM action="maincentreevents add.php" method="post">
     <table border=0 align="center"width="754"height="20">
     <tr>
     <td><h3>Event Title<br/></h3></td>
     <td width="300"height="30" align="center"><input type="text " name="Event Title" size="50" maxlength="50" /></td>
     </tr><br/> 
     <tr>
     <td width=385><h3>Event Short Description<br/></h3></td>
     <td align="center"><textarea rows="2"cols="40" name="Event Short Description" size="100" maxlength="50">
     </textarea></td>
     </tr>
     <tr>
     <td width="235"><h3>Event Description</h3></td><br/>
     <td align="center"><textarea rows="2"cols="40" name="Event Description" size="100" maxlength="50">
     </textarea></td>
     </tr>
     <tr>
     <td width="250"><h3>Event Heldon</h3></td><br/>
     <td align="center"><input type="0" name="Event Heldon" size="50" maxlength="50"></td>
     </tr>
     <tr>
     <td colspan="20" align="center">
     <input type="submit" value="ADD">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>         
     </tr>
     </table>
     </form>
commented: What was the point of this :( +0

whoops i did it wrong way round.

$id = $_GET['id'] ;

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.