PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Tue Dec 10, 2019 2:49 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 21 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: News php code
PostPosted: Sun Mar 25, 2012 6:47 am 
Offline
Forum Newbie

Joined: Sun Mar 25, 2012 6:22 am
Posts: 12
Hi,

I'm a fair new in PHP and currently seeking help for my news code (php). :offtopic:

Syntax: [ Download ] [ Hide ]
<html>
<head>
<title>Add News</title>
<meta http-equiv="Content-Type" content="text/html; charset="iso"-8859-1">
</head>

<body>
<?
if(isset($add_n)){
   $link = @mysql_connect("", "", "");
   if(!$link){
      echo('Error connecting to the database: ' . $mysql_error());
      exit();
   }
   $db = @mysql_selectdb('');
   if(!$db){
      echo('Error selecting database: ' . $mysql_error());
      exit();
   }
   $query = "INSERT INTO news(name, email, headline, story, timestamp)VALUES('$name', '$email', '$headline', '$story', NOW())";
   $result = @mysql_query($query);
   if(!$result){
      echo('Error adding news: ' . $mysql_error());
      exit();
   }else{
   mysql_close($link);
   echo('Success!<br><a href="add.php">Click here</a> to add more news.<br><a href="edit.php">Click here</a> to edit news.<br><a href="../index.php">Click here</a> to return to the main page.');
   }
}else{
?>
<form name="form1" method="post" action="<? echo $PHP_SELF; ?>">
  <table width="50%" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td width="50%">Name</td>
      <td><input name="name" type="text" id="name"></td>
    </tr>
    <tr>
      <td>Email</td>
      <td><input name="email" type="text" id="email"></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>Headline</td>
      <td><input name="headline" type="text" id="headline"></td>
    </tr>
    <tr>
      <td>News Story</td>
      <td><textarea name="story" id="story"></textarea></td>
    </tr>
    <tr>
      <td colspan="2"><div align="center">
          <input name="hiddenField" type="hidden" value="add_n">
          <input name="add" type="submit" id="add" value="Submit">
        </div></td>
    </tr>
  </table>
  </form>
<? } ?>
</body>
</html>

Thats the code I used but when I filled the input field and hit the submit button, it does not write anything in the databse. it just do what the else stateement Why? thnx much


Top
 Profile  
 
 Post subject: Re: News php code
PostPosted: Sun Mar 25, 2012 6:59 am 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6617
Location: WA, USA
There a few things wrong...

1. Variables like $add_n (which should actually be $hiddenField=="add_n") and $name don't exist unless you specifically create them. It may have happened automatically in previous versions of PHP with different setups than you have now, but they were bad and you shouldn't try to use it. Instead,
Syntax: [ Download ] [ Hide ]
$name = $_POST["name"];
// etc.

For the $hiddenField you have to isset() on what's in $_POST - you can't try to assign $hiddenField and do an isset() on that.
Syntax: [ Download ] [ Hide ]
if (isset($_POST["hiddenField"]) /* still need that add_n thing though so */ && $_POST["hiddenField"] == "add_n") {



2. You should never put values from a form right into your SQL queries. If you can't use PDO/mysqli/prepared statements then use at the last minute:
Syntax: [ Download ] [ Hide ]
"VALUES ('" . mysql_real_escape_string($name) . "', '" . mysql_real_escape_string($email) . "' and so on"



3 in two parts. The first part is that $PHP_SELF isn't around either (for the same reason $name and $email and all those aren't) so you have to grab that from $_SERVER. The second part is that PHP_SELF isn't safe for using as form actions. The safer alternative is SCRIPT_NAME ($_SERVER["SCRIPT_NAME"]) but it isn't exactly the same as PHP_SELF. If you notice that and need it like it was before then say something.


4.
- mysql_selectdb() is deprecated; use (just add an underscore)
- mysql_error() is a function, not a variable. Remove the $s.


Top
 Profile  
 
 Post subject: Re: News php code
PostPosted: Sun Mar 25, 2012 9:12 am 
Offline
Forum Newbie

Joined: Sun Mar 25, 2012 6:22 am
Posts: 12
Thanks!

It seems to have numerous probs in my code. :!:

I have one more similar code... The problem is getting the news from database..

Syntax: [ Download ] [ Hide ]
<?php

$query = "SELECT *," .
"DATE_FORMAT(postdate, '%Y-%m-%d') as date " .
"FROM news ORDER BY id DESC LIMIT $news_limit"; // 1.
$result = mysql_query($query);

while($r=mysql_fetch_array($result)) // 2.
{
echo "<br><table width='100%'><tr bgcolor='$title_cell_color'><td>
<img src='$bullet'><b>$title</b> posted on $date</td></tr>
<tr bgcolor='$news_cell_color'><td>$content</td></tr>
</table><br>"
;

}

?>

im fairly beginner in php.. pls help me.

if it does not work,
Can you suggest me a news page which is similar??


Top
 Profile  
 
 Post subject: Re: News php code
PostPosted: Sun Mar 25, 2012 4:51 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6617
Location: WA, USA
It doesn't work... how?


Top
 Profile  
 
 Post subject: Re: News php code
PostPosted: Sun Mar 25, 2012 11:28 pm 
Offline
Forum Newbie

Joined: Sun Mar 25, 2012 6:22 am
Posts: 12


Top
 Profile  
 
 Post subject: Re: News php code
PostPosted: Mon Mar 26, 2012 5:35 am 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6617
Location: WA, USA


Top
 Profile  
 
 Post subject: Re: News php code
PostPosted: Mon Mar 26, 2012 5:57 am 
Offline
Forum Newbie

Joined: Sun Mar 25, 2012 6:22 am
Posts: 12
Can you help me? I really do not know it.... pls.....

Thnx much!


Top
 Profile  
 
 Post subject: Re: News php code
PostPosted: Mon Mar 26, 2012 6:04 am 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6617
Location: WA, USA
I did. Scroll up and (re)read my posts.


Top
 Profile  
 
 Post subject: Re: News php code
PostPosted: Mon Mar 26, 2012 7:28 am 
Offline
Forum Newbie

Joined: Sun Mar 25, 2012 6:22 am
Posts: 12
Thanks!
But there's another error. ;)

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /srv/disk4/741839/www/catapultphpmysql.eu.pn/testwebsite/news/add.php on line 20
pls help me fix this error. ;)


Top
 Profile  
 
 Post subject: Re: News php code
PostPosted: Mon Mar 26, 2012 10:15 am 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6617
Location: WA, USA
Well that's not the error from before, and I don't see anything that would cause it in the latest code you've posted, so...

What's your code now?


Top
 Profile  
 
 Post subject: Re: News php code
PostPosted: Mon Mar 26, 2012 11:16 pm 
Offline
Forum Newbie

Joined: Sun Mar 25, 2012 6:22 am
Posts: 12


Top
 Profile  
 
 Post subject: Re: News php code
PostPosted: Tue Mar 27, 2012 7:30 am 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6617
Location: WA, USA
There's a typo in what I posted. Try to find it, it's not that hard.


Top
 Profile  
 
 Post subject: Re: News php code
PostPosted: Tue Mar 27, 2012 9:35 am 
Offline
Forum Newbie

Joined: Sun Mar 25, 2012 6:22 am
Posts: 12


Top
 Profile  
 
 Post subject: Re: News php code
PostPosted: Tue Mar 27, 2012 9:53 am 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6617
Location: WA, USA
You're missing a closing ) and an opening {.

You found the typo. Good. Proves you can find little things like it. So try to find these other little things too, okay?


Top
 Profile  
 
 Post subject: Re: News php code
PostPosted: Tue Mar 27, 2012 11:57 pm 
Offline
Forum Newbie

Joined: Sun Mar 25, 2012 6:22 am
Posts: 12


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 21 posts ]  Go to page 1, 2  Next

All times are UTC - 5 hours


Who is online

Users browsing this forum: Google [Bot] and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group