Hello!

I have been trying to make a news script where you can edit/delete news. I've looked for tutorials and tried to make mine too. But I still have some problems, and I don't know what is wrong.

So, I'm posting the code here, maybe someone can help me.

<?php 
include("db.php");

if(!isset($cmd)) 
{
   $result = mysql_query("select * from xuudis_uudis order by id"); 
   
   while($r=mysql_fetch_array($result)) 
   { 
      $title=$r["uudis_pealkiri"];
      $id=$r["id"];
     
      echo "<a href='edit.php?cmd=edit&id=$id'>$title - Muuda!</a>";
      echo "<br>";
    }
}
?>
<br />
<br />
<br />
<?php
if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
{
   if (!isset($_POST["submit"]))
   {
      $id = $_GET["id"];
      $sql = "SELECT * FROM xuudis_uudis WHERE id=$id";
      $result = mysql_query($sql);        
      $myrow = mysql_fetch_array($result);
      ?>
	  
      <form action="edit.php" method="post">
      <input type=hidden name="id" value="<?php echo $myrow["id"] ?>">
   
      Pealkiri: <INPUT TYPE="TEXT" NAME="title" VALUE="<?php echo $myrow["uudis_pealkiri"] ?>" SIZE=40><br /><br />
      Kuupäev: <INPUT TYPE="TEXT" NAME="who" VALUE="<?php echo $myrow["uudis_kuu"] ?>" SIZE=30><br><br />
	  Uudis: <TEXTAREA NAME="message" ROWS=5 COLS=55><?php echo $myrow["uudis_uudis"] ?></TEXTAREA><br><br />
    
   
      <input type="hidden" name="cmd" value="edit">
   
      <input type="submit" name="submit" value="Muuda!">
   
      </form>
   
<?php } ?>

<?php
   if ($_POST["$submit"])
   {
      $title = $_POST["title"];
	  $message = $_POST["message"];
	  $who = $_POST["who"];
	  
	  $sql = "UPDATE xuudis_uudis SET uudis_pealkiri='$title', uudis_uudis='$message', uudis_kuu='$who' WHERE id=$id";
	  
      $result = mysql_query($sql);
      echo "Thank you! Information updated.";
	}
}
?>

The mysql table's name is xuudis_uudis.

Im trying to make a script where you can edit news.
I want that you could edit Title [uudis_pealkiri]
Date[uudis_kuu]
Content[uudis_uudis]

Everything works, it connects to the database.
Gets the information.

But when I edit the text and 'submit' it, it does nothing.
So, I am a bit confused.

Thanks,

Taavi

Recommended Answers

All 4 Replies

Line 49

if ($_POST["$submit"])
// should be
if (isset($_POST['submit']))
//pLS CHANGE 
if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit") 

//to 

if($_REQUEST["cmd"]=="edit")
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>

      <div align="center">
  <?php 
include("db.php");

if(!isset($cmd)) 
{
   $result = mysql_query("select * from xuudis_uudis order by id"); 
   
   while($r=mysql_fetch_array($result)) 
   { 
      $title=$r["uudis_pealkiri"];
      $id=$r["id"];
     
      echo "<a href='edit.php?cmd=edit&id=$id'>$title - Muuda!</a>";
      echo "<br>";
    }
}
?>
  <br />
  <br />
  <br />
  <?php
if($_REQUEST["cmd"]=="edit")
{
   if (!isset($_POST["submit"]))
   {
      $id = $_GET["id"];
      $sql = "SELECT * FROM xuudis_uudis WHERE id=$id";
      $result = mysql_query($sql);        
      $myrow = mysql_fetch_array($result);
      ?>
        
      </div>
      <form action="edit.php" method="post">
        <div align="center">
        <input type=hidden name="id" value="<?php echo $myrow["id"] ?>">
   
      Pealkiri: 
      <INPUT TYPE="TEXT" NAME="title" VALUE="<?php echo $myrow["uudis_pealkiri"] ?>" SIZE=40>
      <br />
      <br />
      Kuupäev:
      <INPUT TYPE="TEXT" NAME="who" VALUE="<?php echo $myrow["uudis_kuu"] ?>" SIZE=30>
      <br>
      <br />
	  Uudis: 
	  <TEXTAREA NAME="message" ROWS=5 COLS=55><?php echo $myrow["uudis_uudis"] ?></TEXTAREA>
	  <br>
	  <br />
    
   
      <input type="hidden" name="cmd" value="edit">
   
      <input type="submit" name="submit" value="Muuda!">
   
        </div>
      </form>
   
      <div align="center">
  <?php } ?>
        
  <?php
if (isset($_POST['submit']))
      $title = $_POST["title"];
	  $message = $_POST["message"];
	  $who = $_POST["who"];
	  
	  $sql = "UPDATE xuudis_uudis SET uudis_pealkiri='$title', uudis_uudis='$message', uudis_kuu='$who' WHERE id=$id";
	  
      $result = mysql_query($sql);
      echo "Thank you! Information updated.";
	}
?>
        
        
        
      </div>
</body>
</html>

Thanks for the help, but there is still some problems.

1. The 'Thank you! Information updated. ' - just comes there, if I open the page.

2. If I edit one news content other go blank.

Taavi

Thanks everyone!

I solved the problem, myself.

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.