Jump to content

[SOLVED] Help me about php output ( Practicing MySql)


Recommended Posts

 guys i need ur help, i have jst practicing my php about w/ mySql..

 Now my codes have no errors but i dnt understand my output..

 

config.php

<?php

 

//Change root to your database account's username

$dbusername = "root";

 

//Add your account's password in between the quotations

$password = " ";

 

//Add the name of the database you are using in between the quotations

$database = " ";

 

//This usually does not need to be changed.

$server = "localhost";

 

//Do not edit coding below this line.

mysql_connect($server, $dbusername, $password) or die(mysql_error());

 

mysql_select_db($database) or die(mysql_error());

 

?>

 

add.php

<HTML>

<form action="<? $_SERVER['PHP_SELF']; ?>" method="POST">

Author: <input type='text' size='30' name='author'><br><br>

Subject: <input type='text' size='30' name='subject'><br><br>

Message: <textarea name='message' rows='5' cols='30'></textarea><br><br>

Date: <input type='text' size='30' name='date'><br>br>

<input type='submit' name='submit' value='Add News'></form>

 

<?php

//Include the database file

require_once('config.php');

 

//If the submit button is pressed

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

 

 

//Turn the inputs from the form into variables for insertion

$author = $_POST['author'];

$subject = $_POST['subject'];

$message = $_POST['message'];

$date = $_POST['date'];

 

//Begin the database query

$insert = mysql_query("INSERT INTO news(newsAuthor,newsSubject,newsMessage,newsDate)

values('$author', '$subject', '$message', '$date')");

 

//Echo a message if the news was added

echo "The news was successfully inserted into the database.";

 

}

 

?>

 

news.php

</php

require_once('config.php');

 

//The following bit of code will select the news and order the articles by their id, so if you post an article, the same article will show up at the top of the list.

$query = mysql_query("SELECT * FROM news order by newsID desc");

 

while($n=mysql_fetch_array($query)){

 

$author=$n["newsAuthor"];

$subject=$n["newsSubject"];

$message=$n["message"];

$date=$n["date"];

 

echo "<b>$subject<br>$message<br>Posted by $author on $date";

 

}

 

?>

 

Now i already created my database using phpmyadmin, but whats the problem?

it appears on this after i submit

 

Is there anything that i mess?

Forbidden

 

You don't have permission to access /Practice/< on this server.

   Pls. help me i really want to know..

In add.php, line 2:

 

<form action="<? $_SERVER['PHP_SELF']; ?>" method="POST">

 

The use of short form of PHP opening tags is deprecated, and has been removed entirely from the latest PHP versions IIRC.

 

Try:

 

<form action="<?php print $_SERVER['PHP_SELF']; ?>" method="POST">

 

  • 2 weeks later...

  Guys now im done in saving into the database and now my problem is, i want to display it the data

  save it from the db. Is there something missing in my code at news.php?

 

<?php

require_once('config.php');

 

//The following bit of code will select the news and order the articles by their id, so if you post an article, the same article will show up at the top of the list.

$query = mysql_query("SELECT * FROM news order by newsID desc");

 

while($n=mysql_fetch_array($query)){

 

$author=$n["newsAuthor"];

$subject=$n["newsSubject"];

$message=$n["message"];

$date=$n["date"];

 

echo "<b>$subject<br>$message<br>Posted by $author on $date";

 

}

 

?>

 

theres other ways of doing it but i usually prefer using the heredoc syntax, you can find out more about it on the php site.

change this line

echo "<b>$subject<br>$message<br>Posted by $author on $date";

to this line

echo <<<EOT
<b[color=red]r[/color]>$subject<br>$message<br>Posted by $author on $date
EOT;

 

that might mean multiple things either a missing } or ;. but it also could be coming from the heredoc syntax. you cant have anything other than echo <<<EOT on one line (no spaces), the stuff has to be on the next line, and similarly with EOT; it has to be on a seperate line with nothing else on that line. Check for any extra spaces.

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.