Jump to content

code loox good, but not works!


dansk

Recommended Posts

okay, i come again for help from my phpfreaks friends

i have this code that i used from a tutorial

[code]<?

//used from maaking.com


$connect = mysql_connect("localhost", "user","pass") or die(mysql_error());
//mysql_select_db($announ) or die(mysql_error());
$result = mysql_query("SELECT * from Announ.login WHERE session_id = '" . $_COOKIE[id] . "'");
$row = mysql_fetch_array($result);

if ($row[session_id] == $_COOKIE[id] && $row[user_name] == "admin")
{

  if($submit)

  {//begin of if($submit).


      // Set global variables to easier names

    // and pervent sql injection and apostrophe to break the db.

      $post_title = mysql_real_escape_string($_POST['post_title']);

      $post = mysql_real_escape_string($_POST['post']);
     
      $post_status = mysql_real_escape_string($_POST['post_status']);
     
      $image = mysql_real_escape_string($_POST['image']);

      $image_link = mysql_real_escape_string($_POST['link']);



              //check if (title) field is empty then print error message.

              if(!$post_title){  //this means If the title is really empty.

                    echo "Error: News title is a required field. Please fill it.";

                    exit(); //exit the script and don't do anything else.

              }// end of if



        //run the query which adds the data gathered from the form into the database

        $result = mysql_query("INSERT INTO Announ.articles (Post_Title, Post_Date,Post, Post_Status, Images,Image_Link)

                      VALUES ('$post_title',NOW(),'$post','$post_status','$image','$image_link'");

          //print success message.

          echo "<b>Thank you! News added Successfully!<br>You'll be redirected to Home Page after (4) Seconds";

          echo "<meta http-equiv=Refresh content=4;url=Posts.php>";

  }//end of if($submit).





  // If the form has not been submitted, display it!

else

  {//begin of else



      ?>

      <br>

      <h3>::Add News</h3>



      <form method="post" action="news_add.php">



      Title: <input name="post_title" size="40" maxlength="255">

      <br>

      News: <textarea name="post"  rows="7" cols="30"></textarea>

      <br>

      Status: <input name="post_status" rows="7" cols="30">

      <br>

      image: <input name="image" rows="7" cols="30">

      <br>
     
      image_link: <input name="image_link" rows="7" cols="30">

      <br>
     
      <input type="submit" name="submit" value="Add News">

      </form>

      <?

  }//end of else


}
else
{
header("Location: login.php");
}







?>
      <title>Add News</title>

[/code]

the form displays correctly but for some reason, when i click add news, it goes back to the same page with nothing happening, just like this one http://www.maaking.com/demos/news_for_newbie/add_news.php

Link to comment
https://forums.phpfreaks.com/topic/32668-code-loox-good-but-not-works/
Share on other sites

The [color=red]//[/color]mysql_select_db($announ) or die(mysql_error()); statement is commented out.

With no database selected, the mysql_query(...) will fail and the mysql_fetch_array(...) will fail. The $row variable will be NULL and the if(...) test will be false and will skip down to the [b]else[/b] clause.

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.