Jump to content

editing records with form


AlexGM16

Recommended Posts

i have a page called managenews.php, which displays all the news articles I have written, with a 'delete' and 'edit' link next to them.

Well the delete page works perfectly fine, but when I click edit I get the default Internal server error.

The query works just fine when i insert some dummy info into it via phpmyadmin.

[b]editnews.php[/b]
[code]<?php
if($_POST['submit']) {
$title = $_POST['title'];
$newstext = $_POST['newstext'];
$db=mysql_connect("localhost", "user", "pass") or die("Could not connect to localhost");
mysql_select_db("alex", $db) or die("Could not find database alex");
$querySQL = mysql_query("UPDATE news SET postdate='CURDATE()', title='".$title."', newstext='".$newstext."' WHERE id='".$id."'") or die(mysql_error());
header("Location: managenews.php");
} else {
$id = $_GET['id'];
$db=mysql_connect("localhost", "user", "pass") or die("Could not connect to localhost");
mysql_select_db("alex", $db) or die("Could not find database alex");
$query = mysql_query("SELECT * FROM news WHERE id='".$id."'");

while($results = mysql_fetch_assoc($query)) {
?>
<form name="editNews" method="post"
action="<? $_SERVER['PHP_SELF']; ?>">
<?
echo "<input type=\"text\" name=\"title\" size=\"20\" value=\"{$results['title']}\" /><br />";
echo "<input type=\"text\" name=\"newstext\" size=\"20\" value=\"{$results['newstext']}\" />";
}
?>
<input type="submit" name="submit" value="submit" />
</form>
<?
}
?> [/code]
Link to comment
https://forums.phpfreaks.com/topic/28247-editing-records-with-form/
Share on other sites

sry guys:
[b]managenews.php[/b]
[code]<?php
include "displayNewsEdit.inc";
/* user config variables */
$max_items = 5; /* max number of news items to show */

$db = mysql_connect ('localhost','user','pass');
mysql_select_db ('alex',$db);

echo "<center>\n";
displayNews();
echo"</center>\n";
?>

[/code]

[b]displayNewsEdit.inc[/b]
[code]<?php
function displayNews($all = 0) {

    global $db, $max_items;
   
    if ($all == 0) {
 
        $query = "SELECT id,title,newstext," .
                "DATE_FORMAT(postdate, '%M %D, %Y') as date " .
                "FROM news ORDER BY postdate DESC LIMIT $max_items";
    } else {
     
        $query = "SELECT id,title,newstext," .
                "DATE_FORMAT(postdate, '%M %D, %Y') as date " .
                "FROM news ORDER BY postdate DESC";
    }
    $result = mysql_query ($query);
    while ($row = mysql_fetch_assoc ($result)) {
     
        echo "<TABLE border=\"1\" width=\"320\">\n";
        $date = $row['date'];       
        $title = htmlentities ($row['title']);
        $news = nl2br (strip_tags ($row['newstext'], '<a><b><i><u>'));
       
echo "<TR><TD><b>$title</b> posted on $date
<a href=\"delete.php?id={$row['id']}\">Delete</a>&nbsp;|&nbsp;<a href=\"edit.php?id={$row['id']}\">Edit</a></TD></TR>\n";
echo "<TR><TD>$news</TD></TR>\n"; 
echo "</TABLE>\n";
echo "<BR>\n";
}
   
    /* if we aren't displaying all news,
    * then give a link to do so */
  /*  taking out the view all news link for now******************************
  if ($all == 0) {
        echo "<a href=\"{$_SERVER['PHP_SELF']}" .
            "?action=all\">View all news</a>\n";
    }      *********************************/
}
?>[/code]

[b]delete.php[/b]
[code]<?php
$db=mysql_connect("localhost", "user", "pass") or die("Could not connect to localhost.");
mysql_select_db("alex", $db) or die("Could not find database alex");

/* get id from url */
$id = $_GET['id'];

/* delete record */
$sql = "DELETE FROM news WHERE id=".$id;
$result = mysql_query( $sql ) or die( mysql_error() );

/* redirect user back to edit news page */
header("Location: managenews.php");

?> [/code]

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.