Jump to content

[SOLVED] Confusing me..!


Cory94bailly

Recommended Posts

<?php
require('includes/config.php');
   // Make a MySQL Connection
   mysql_connect("$path", "$username", "$password") or die(mysql_error());
   mysql_select_db("$database") or die(mysql_error());
if(isset($_POST['submit']))
{
//Set the variables!
$id = basename($_SERVER['PHP_SELF']);
$title = mysql_real_escape_string($_POST['title']);
$news = mysql_real_escape_string($_POST['news']);
$dbtitle = mysql_query("SELECT * FROM news");
$dbtext = "";
$query1 = "UPDATE news SET title = '$title' WHERE id = '$id'";
$query2 = "UPDATE news SET news = '$news' WHERE id = '$id'";
//Update the title and text!
mysql_query($query1) or die(mysql_error());
mysql_query($query2) or die(mysql_error());
}
?>

   <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
      <p>Title:<br>
      <textarea rows="1" cols="20" name="title"><? $dbtitle ?></textarea></p>
      <p>Text:<br>
      <textarea rows="10" cols="50" name="news"><? $dbtext ?></textarea></p>
      <input type="submit" name="submit" value="Submit">
   </form>
   
   <?
   echo "ID = $id";
   echo "<br>";
   echo "TITLE = $title";
   echo "<br>";
   echo "NEWS = $news";
   echo "<br>";
   echo "DBTITLE = $dbtitle";
   echo "<br>";
   echo "DBTEXT = $dbtext";
   ?>

 

 

 

Well there's my WHOLE code..

 

I am having lots of problems..

 

1.) I don't know how to get it to show "ID = " as something else.. Let me explain.

 

If I am at http://xxx.com/page.php?id=4

I want it to only show the number 4 as ID = ...

 

 

 

 

2.) I don't know how to word it but maybe looking at my code will help.

Link to comment
Share on other sites

Just looking at your code why use two queries to update the news title/news content in two queries:

$query1 = "UPDATE news SET title = '$title' WHERE id = '$id'";
$query2 = "UPDATE news SET news = '$news' WHERE id = '$id'";
//Update the title and text!
mysql_query($query1) or die(mysql_error());
mysql_query($query2) or die(mysql_error());

Use just one:

//Update the title and text!
$query = "UPDATE news SET title = '$title', news = '$news' WHERE id = '$id'";
mysql_query($query) or die(mysql_error());

 

To get the id from the url use $_GET['id']

Link to comment
Share on other sites

That is because it'll only work if you submit the form. YOu'll need to change:

if(isset($_POST['submit']))
{
//Set the variables!
$id = basename($_SERVER['PHP_SELF']);

to:

$id = (isset($_GET['id']) && is_numeric($_GET['id'])) ? $_GET['id'] : null;

if(isset($_POST['submit']))
{

Link to comment
Share on other sites

<?php
require('includes/config.php');
   // Make a MySQL Connection
   mysql_connect("$path", "$username", "$password") or die(mysql_error());
   mysql_select_db("$database") or die(mysql_error());
$id = (isset($_GET['id']) && is_numeric($_GET['id'])) ? $_GET['id'] : null;

if(isset($_POST['submit']))
{
$title = mysql_real_escape_string($_POST['title']);
$news = mysql_real_escape_string($_POST['news']);
$dbtitle = mysql_query("SELECT * FROM news");
$dbtext = "";
//Update it!
$query = "UPDATE news SET title = '$title', news = '$news' WHERE id = '$id'";
mysql_query($query) or die(mysql_error());
}
?>

   <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
      <p>Title:<br>
      <textarea rows="1" cols="20" name="title"><? $dbtitle ?></textarea></p>
      <p>Text:<br>
      <textarea rows="10" cols="50" name="news"><? $dbtext ?></textarea></p>
      <input type="submit" name="submit" value="Submit">
   </form>
   
   <?
   echo "ID = $id";
   echo "<br>";
   echo "TITLE = $title";
   echo "<br>";
   echo "NEWS = $news";
   echo "<br>";
   echo "DBTITLE = $dbtitle";
   echo "<br>";
   echo "DBTEXT = $dbtext";
   ?>

 

There's my code...

 

How do I get it to show the title and text of a specific post? If I could know how to go to http://myside.com/file.php?id=63 and it would show the mysql's 63rd id field so I could freely edit it..

 

If someone could show me how to do that stuff, I would be pretty much done...

Link to comment
Share on other sites

<?php
require 'includes/config.php';

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

if(isset($_POST['submit']))
{
    if(is_numeric($_POST['id']))
    {
        $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());
    }
    else
    {
        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);
?>
<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
}
?>

Link to comment
Share on other sites

Yayyyyyy!!!

 

Wow, that was a smaller code than I thought it would have to be...

 

Now, how can I get it so if I go to http://mysite.com/file.php, it will display all news but with a button next to them saying something like "Delete" and if I click it, it goes to http://mysite.com/file.php?id=IDHERE automatically?

Link to comment
Share on other sites

Yayyyyyy!!!

 

Wow, that was a smaller code than I thought it would have to be...

 

Now, how can I get it so if I go to http://mysite.com/file.php, it will display all news but with a button next to them saying something like "Delete" and if I click it, it goes to http://mysite.com/file.php?id=IDHERE automatically?

 

Well here's what I have now to display news..

 

<?php
require('includes/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>\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>
  </tr>
  <tr>
    <td>{$news['news']}</td>
  </tr>
HTML;
        }

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

printNews($result1); // Non-sticky News

?>

 

I don't know how to get it to show a edit button next to it..

 

Also, I want to know how to (if possibly) how to put a delete button near each...

Link to comment
Share on other sites

try:

<?php

require 'includes/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']}">Edit</a><br />
      <a href="news_delete.php?id={$news['id']}">Delete</a>
    </td>
  </tr>
  <tr>
    <td>{$news['news']}</td>
  </tr>
HTML;
        }

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

printNews($result1);

?>

Note:

make sure you change news_edit.php and news_delete.php to the correct page which edits/deletes the news.

Link to comment
Share on other sites

Here's my code:

 

<?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());

//Finds out the ID
    if(is_numeric($_POST['id']))
    {
    //Set the variable!
        $id      = $_POST['id'];

        //Delete 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');
    }
    //End it!
    ob_end_flush();
    ?>

 

And I am just getting "Invalid action."

Link to comment
Share on other sites

How is the id coming to the script? If its via the url then you'd use $_GET['id'] rather than $_POST['id']

 

$_POST is used when getting data from a form. $_GET returns data in the url.

 

Never mind :P

 

I fixed it..

 

 

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.