Jump to content

Combine these scripts?


Cory94bailly

Recommended Posts

List.php:

 

<head>
<title>Edit/Delete News</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>

<body>

<br />

<div id="menu">
<? include('menu.html'); ?>
</div>
<div id="content">
<?php

require 'config.php';

// Make a MySQL Connection
mysql_connect($path, $username, $password) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());

$query1 = "SELECT * FROM news ORDER BY sticky, id ASC";
$result1 = mysql_query($query1) or die(mysql_error());

function printNews($result)
{
    if ( mysql_num_rows($result) < 1 )
    {
        echo "<h1>No news!</h1>";
    }
    else
    {
        echo "<table cellspacing=\"2\" cellpadding=\"5\">\n";

        while($news = mysql_fetch_assoc($result))
        {
         $sticky = ($news['sticky'] == 'y') ? "<font color='red'><u>Announcement:</u></font> " : '';
            echo <<<HTML
  <tr>
    <th align="left">{$sticky}<b>{$news['title']}</b></th>
    <td rowspan="2" valign="bottom" align="left">
      <a href="news_edit.php?id={$news['id']}"><img border="0" src="../images/edit.png" alt="Edit" /></a><br />
      <a href="news_delete.php?id={$news['id']}"><img border="0" src="../images/delete.png" alt="Edit" /></a>
    </td>
  </tr>
  <tr>
    <td>{$news['news']}</td>
  </tr>
HTML;
        }

        echo "\n</table><br />";
    }
}

printNews($result1);

?>

 

 

 

News_Edit.php:

 

<head>
<title>Add/Delete News</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>

<body>

<br />

<div id="menu">
<? include('menu.html'); ?>
</div>
<div id="content">
<?php
ob_start();
require 'config.php';

// Make a MySQL Connection
mysql_connect($path, $username, $password) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());

//Starts if you click the submit button.
if(isset($_POST['submit']))
{
//Finds out the ID
    if(is_numeric($_POST['id']))
    {
    //Set the variables!
        $id      = $_POST['id'];
        $title   = mysql_real_escape_string($_POST['title']);
        $news  = mysql_real_escape_string($_POST['news']);

        //Update it!
        $query = "UPDATE news SET title = '$title', news = '$news' WHERE id = '$id'";
        mysql_query($query) or die(mysql_error());
        echo "<meta http-equiv='refresh' content='1;url=index.php'> ID $id Edited!";
    }
    else
    {
    //Error message.
        die('invalid action');
    }
}
elseif(isset($_GET['id']) && is_numeric($_GET['id']))
{
    $id = $_GET['id'];

    $query = "SELECT title, news FROM news WHERE id='$id'";
    $result = mysql_query($query);

    list($news_title, $news_text) = mysql_fetch_row($result);
    //The form.
?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
  <p>Title:<br>
     <input type="text" name="title" value="<?php echo $news_title ?>" />
  </p>
  <p>Text:<br>
     <textarea rows="10" cols="50" name="news"><?php echo $news_text ?></textarea>
  </p>

  <input type="hidden" name="id" value="<?php echo $id; ?>">
  <input type="submit" name="submit" value="Submit">
</form>
<?php
//End it!!!
}
ob_end_flush();
?>

 

 

News_delete.php:

 

<head>
<title>Add/Delete News</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>

<body>

<br />

<div id="menu">
<? include('menu.html'); ?>
</div>
<div id="content">
<?php
ob_start();
require 'config.php';

// Make a MySQL Connection
mysql_connect($path, $username, $password) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());

//Starts if you click the submit button.
if(isset($_POST['submit']))
{
//Finds out the ID
    if(is_numeric($_POST['id']))
    {
    //Set the variables!
        $id      = $_POST['id'];
        $title   = mysql_real_escape_string($_POST['title']);
        $news  = mysql_real_escape_string($_POST['news']);

        //Update it!
        $query = "DELETE FROM news where id=$id";
        mysql_query($query) or die(mysql_error());
        echo "<meta http-equiv='refresh' content='1;url=index.php'> ID $id Deleted!";
    }
    else
    {
    //Error message.
        die('invalid action');
    }
}
elseif(isset($_GET['id']) && is_numeric($_GET['id']))
{
    $id = $_GET['id'];

    $query = "DELETE FROM news where id=$id";
    $result = mysql_query($query);
}
//End it!!!
echo "<meta http-equiv='refresh' content='1;url=index.php'> ID $id Deleted!";
ob_end_flush();
?>

 

 

 

Now how can I make it so if I go to list.php, it will list the news and everything regularly, but when I click edit or delete, it will stay on the same page but go to like http://mysite.com/file.php?action=delete&id=4 it will delete id 4 or edit it if the action is edit..

 

Please help me..

Link to comment
https://forums.phpfreaks.com/topic/107231-combine-these-scripts/
Share on other sites

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.