Jump to content

editing a row


alen

Recommended Posts

try that
[code]
<?
error_reporting(E_ALL ^ E_NOTICE);
mysql_connect("localhost","nancy","blabla");
mysql_select_db("nancy");

if(!isset($_GET["cmd"])){
$result = mysql_query("select * from news order by id");
while($r=mysql_fetch_array($result)){
  echo "<p>endre nyheter:</p>";
  echo "<a href='edit.php?cmd=edit&id=$r["title"]'>$r["id"]</a><br/>";
}
}else{
if(!isset($_POST["submit"])){
  $result = mysql_query("SELECT * FROM news WHERE id=$_GET['id']");
  $myrow = mysql_fetch_array($result);
?>
<form action="edit.php?cmd=edit" method="post">
<table>
  <tr>
  <td>tittel:</td>
  <td><input type="text" name="title" value="<?php echo $myrow["title"] ?>" size='40'></td>
  </tr>
  <tr>
  <td>forfatter:</td>
  <td><input type="text" name="author" value="<?php echo $myrow["author"] ?>" size='40'></td>
  </tr>
  <tr>
  <td>tekst:</td>
  <td><textarea name="news" cols='30' rows='5'><? echo $myrow["news"] ?></textarea></td>
  </tr>
  <tr>
  <td></td>
  <td><input type="submit" name="submit" value="endre"></td>
  </tr>
</table> 
</form>
<?
}elseif($_POST["submit"]){
  $result = mysql_query("UPDATE news SET title='$_POST["title"]', `news`='$_POST["news"]', `author`='$_POST["author"]' WHERE id=$id");
  echo "query finished.";
}
}
?>
[/code]
Link to comment
Share on other sites

sorry... that wasnt tested... this should work...

[code]
<?
error_reporting(E_ALL ^ E_NOTICE);
mysql_connect("localhost","nancy","blabla");
mysql_select_db("nancy");

if(!isset($_GET["cmd"])){
$result = mysql_query("select * from news order by id");
while($r=mysql_fetch_array($result)){
  echo '<p>endre nyheter:</p><a href="edit.php?cmd=edit&id='.$r[title].'">'.$r[id].'</a><br/>';
}
}else{
if(!isset($_POST["submit"])){
  $result = mysql_query("SELECT * FROM news WHERE `id`='$_GET[id]'");
  $myrow = mysql_fetch_array($result);
?>
<form action="edit.php?cmd=edit" method="post">
<table>
  <tr>
  <td>tittel:</td>
  <td><input type="text" name="title" value="<?php echo $myrow["title"] ?>" size='40'></td>
  </tr>
  <tr>
  <td>forfatter:</td>
  <td><input type="text" name="author" value="<?php echo $myrow["author"] ?>" size='40'></td>
  </tr>
  <tr>
  <td>tekst:</td>
  <td><textarea name="news" cols='30' rows='5'><? echo $myrow["news"] ?></textarea></td>
  </tr>
  <tr>
  <td></td>
  <td><input type="submit" name="submit" value="endre"></td>
  </tr>
</table> 
</form>
<?
}elseif($_POST["submit"]){
  $result = mysql_query("UPDATE news SET title='$_POST[title]', `news`='$_POST[news]', `author`='$_POST[author]' WHERE id=$id");
  echo "query finished.";
}
}
?>
[/code]
Link to comment
Share on other sites

It didn't actually do what I wanted to.. But here's my solution:

[code]
<?
error_reporting(E_ALL ^ E_NOTICE);

mysql_connect("localhost","nancy","blabla");

mysql_select_db("nancy");

if(!isset($_GET["cmd"]))
{
  $result = mysql_query("select * from news order by id");
 
  while($r=mysql_fetch_array($result))
  {
      $title=$r["title"];
      $id=$r["id"];
   
echo "<p>endre nyheter:</p>";
      echo "<a href='edit.php?cmd=edit&id=$id'>$title</a><br/>";
    }
}
?>
<?
if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
{
  if (!isset($_POST["submit"]))
  {
      $id = $_GET["id"];
      $sql = "SELECT * FROM news WHERE id=$id";
      $result = mysql_query($sql);       
      $myrow = mysql_fetch_array($result);
      ?>
 
      <form action="edit.php" method="post">
  <table>
<tr>
      <td>tittel:</td>
<td><input type="text" name="title" value="<?php echo $myrow["title"] ?>" size='40'></td>
</tr>
<tr>
<td>forfatter:</td>
<td><input type="text" name="author" value="<?php echo $myrow["author"] ?>" size='40'></td>
</tr>
<tr>
      <td>tekst:</td>
<td><textarea name="news" cols='30' rows='5'><? echo $myrow["news"] ?></textarea></td>
</tr>
<tr>
<td></td><br>
      <td><input type="hidden" name="cmd" value="edit"><input type="submit" name="submit" value="endre"></td>
</tr>
</table> 
      </form>

<? } ?>
<?
  if ($_POST["submit"])
  {
      $title = $_POST["title"];
  $news = $_POST["news"];
  $author = $_POST["author"];
 
  $sql = "UPDATE news SET title='$title',news='$news',author='$author' WHERE id=$id";

      $result = mysql_query($sql);
      echo "query finished.";
}
}
?>
[/code]

It works fine. But when I echo "query finished", I can see the "endre nyheter" and the link to the news item. I want query finished. to come on a own page. How would I manage to do that?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.