Jump to content

I was unable to assign an ID to the data alongside which the delete button is..


aditya1990

Recommended Posts

I was reading the book Build your own database driven website using php and mysql and I came across this project where I had to make a page which displayed the jokes from the ijdb database and gave a button with an option to delete the joke from the database.Kevin,the author,gave the code as an all-purpose single page...but I wanted to split the script into 2 pages.But I unable to assign an ID to the joke alongside which the delete button was there.

Here's my code:

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
  <title>Joke texts from the database</title>
  </head>
  <body>
    <?php
         $dbcnx=@mysql_connect('localhost','root','MyNewPassword');
         if(!$dbcnx){
              echo "There was an error connecting to the database";
              exit();
         }
         $dbcny=@mysql_select_db('ijdb');
         if(!$dbcny){
            echo "Unable to locate the database at this time.";
            exit();
         }
         ?>
         <p>Here are all the jokes in our database:</p>
         <blockquote>
         <?php
            $result = @mysql_query('SELECT joketext,jokedate FROM joke');
             if(!$result){
                  echo "Query unsuccessful'.mysql_error().'";
             exit();
             }
             while($row=mysql_fetch_array($result)){
                 echo "<p><table><tr><td>".$row[joketext]." &nbsp &nbsp &nbsp </td>".$row[jokedate]."</tr></table></p><form action='jokelist_enter.php' method=POST><input type=submit value='Delete it!' name='delete'> ";                 
             }
         ?>
         </blockquote>
         <p><b>If you want to enter your own joke,please do so here:</b></p><br />
         <form action="jokelist_enter.php" method="POST">
         <label>
         Enter here:
         <textarea rows="10" cols="40" name="joke">
         </textarea>
         </label>
         <br />
         <input type="submit" value="Enter it!" />
  </body>
</html>

and

<?php
    $dbcnx=@mysql_connect('localhost','root','MyNewPassword');
    if(!$dbcnx){
    echo("Unable to connect to the database...sorry.");
    exit();
    }
    $dbcny=@mysql_select_db('ijdb');
    if(!$dbcny){
    echo("Could not find the database.");
    exit();
    }
    $date=mysql_query('CURDATE();');
    $joke=$_POST["joke"];
    $sql=@mysql_query("INSERT INTO joke SET
joketext='$joke',
jokedate='$date'");
    if(!$sql){
    echo "Sorry...we could not enter your joke into the database.";
    exit();
    }
    $value=$_POST['delete'];
    $jokeid=$_POST[$result = @mysql_query('SELECT id,joketext,jokedate FROM joke')];
  $sql1=@mysql_query("DELETE * from joke where id='$result[id]' from joke");
  if(!$sql1){
  echo "Sorry could not delete the joke from the database.";
  echo mysql_error();
  }
?>
<html>
<head>
<title>
</title>
</head>
<body>
<a href="jokelist.php">Click here to see your joke in the list of jokes!</a>
</body>
</html>

Any help will be gladly accepted. ;D

Archived

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