12-19-04, 05:52 PM
Newbie Coder
Join Date: Oct 2004
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Help with my script?
Hi. I'm pretty new to PHP, and need some help with this code:
Yes, I know, it's sloppy and unorganized. But any help or feedback would be greatly appreciated
.
12-19-04, 09:11 PM
Newbie Coder
Join Date: Apr 2004
Posts: 86
Thanks: 0
Thanked 0 Times in 0 Posts
whats your question? are you seeing an error when you call the page? what is the error?
Not sure if this is the problem - but I usually don't put spaces between the '=
' signs and i escape double quotes when defining the sql statement - ie.:
$sql=mysql_query("insert into news set name=\"$name\", email=\"$email\", title=\"$title\", news=\"$news\", date=\"$date\"");
I also noticed an extra ',' at the end of your string - that will cause a problem. .. the best way for you to troubleshoot is to put an echo statement under the $sql definintion .. and then you can cut & paste the statement directly into your sql interface to see if it has the right syntax, ie:
echo $sql
http://buildacom.com
12-19-04, 09:20 PM
Newbie Coder
Join Date: Oct 2004
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
thanks for replyin so quick. i get this error:
Quote:
Parse error: parse error, unexpected $ in /home/culticka/public_html/5d/dark/news.php on line 70
i tried the modified code and still got the error.
12-20-04, 01:43 AM
Newbie Coder
Join Date: Dec 2004
Location: Karachi, Pakistan
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Hi
find following
replace with following
__________________
Web Programmer
web: http://www.netxs.com.pk
email: irfanmail@gmail.com
msn : irfanbaba@hotmail.com
12-20-04, 10:43 AM
Newbie Coder
Join Date: Dec 2004
Location: Scotland, UK
Posts: 50
Thanks: 0
Thanked 0 Times in 0 Posts
Using single quotes will not evaluate the variable in the query. The INSERT query doesn't support "set", so that'll need restructured. You also haven't ended the string to be outputted. I would use this:
PHP Code:
<?php
mysql_connect ( "localhost" , "user" , "pass" )or die( mysql_error ());
//select the database to use
mysql_select_db ( "db" )or die( mysql_error ());
$action = $_REQUEST [ 'action' ];
if( $action == "add" ){
$date = date ( "l M d, Y" );
$name = $_POST [ 'name' ];
$email = $_POST [ 'email' ];
$title = $_POST [ 'title' ];
$news = $_POST [ 'news' ];
$sql = mysql_query ( "INSERT INTO `news` (`name`, `email`, `title`, `news`, `date`) VALUES ('" . $name . "', '" . $email . "', '" . $title . "', '" . $news . "', '" . $date . "')" );
$query = mysql_query ( $sql ) or die( "Cannot query the database (cannot add news).<br>" . mysql_error ());
echo "News Added." ;
$getnews = mysql_query ( "SELECT * FROM news ORDER BY id DESC" ); //query the database for all of the news
while( $r = mysql_fetch_array ( $getnews )){ //while there are rows in the table
echo "<table border='0' width='315' cellpadding='0' cellspacing='0'>
<tr>
<td width='315' align='left' valign='top'><b>" . $title . "</b> | " . $date . "</td>
</tr>
<tr>
<td width='315' align='center' valign='top'>
" . $news . "
</td>
</tr>
<tr>
<td width='315' align='right' valign='top'>
<a href='mailto:" . $email . "'>" . $name . "</a>
</td>
</tr>
</table>" ;
?>
Should work
Last edited by Vulture; 12-20-04 at 10:47 AM .
12-20-04, 06:28 PM
Newbie Coder
Join Date: Oct 2004
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
sorry i was gone but am back. thanks for the help, ill go try it...
Parse error: parse error, unexpected $ in /home/culticka/public_html/5d/dark/news.php on line 51
uh oh
. didnt work...
12-21-04, 10:12 AM
Newbie Coder
Join Date: Dec 2004
Location: Scotland, UK
Posts: 50
Thanks: 0
Thanked 0 Times in 0 Posts
That code is only 46 lines long
Are you including the code anywhere?
12-21-04, 01:45 PM
Newbie Coder
Join Date: Oct 2004
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
no but the form is on a different page:
addnews.php:
<form name="addnews" method="post" action="news.php?action=add">
<input type="text" size="12" name="name" value="name"><br>
<input type="text" size="12" name="email" value="email"><br>
<input type="text" size="12" name="title" value="title of news"><br>
<textarea name="news" cols="18" rows="12" value="news"></textarea>
<input type="submit" value="add news">
</form>
news.php:
PHP Code:
<style type="text/css" href="shoutbox/shoutstyle.css" rel="stylesheet">
<br>
<?php
mysql_connect ( "localhost" , "user" , "pass" )or die( mysql_error ());
//select the database to use
mysql_select_db ( "db" )or die( mysql_error ());
$action = $_REQUEST [ 'action' ];
if( $action == "add" ){
$date = date ( "l M d, Y" );
$name = $_POST [ 'name' ];
$email = $_POST [ 'email' ];
$title = $_POST [ 'title' ];
$news = $_POST [ 'news' ];
$sql = mysql_query ( "INSERT INTO `news` (`name`, `email`, `title`, `news`, `date`) VALUES ('" . $name . "', '" . $email . "', '" . $title . "', '" . $news . "', '" . $date . "')" );
$query = mysql_query ( $sql ) or die( "Cannot query the database (cannot add news).<br>" . mysql_error ());
echo "News Added." ;
$getnews = mysql_query ( "SELECT * FROM news ORDER BY id DESC" ); //query the database for all of the news
while( $r = mysql_fetch_array ( $getnews )){ //while there are rows in the table
echo "<table border='0' width='315' cellpadding='0' cellspacing='0'>
<tr>
<td width='315' align='left' valign='top'><b>" . $title . "</b> | " . $date . "</td>
</tr>
<tr>
<td width='315' align='center' valign='top'>
" . $news . "
</td>
</tr>
<tr>
<td width='315' align='right' valign='top'>
<a href='mailto:" . $email . "'>" . $name . "</a>
</td>
</tr>
</table>" ;
?>
12-21-04, 10:04 PM
Newbie Coder
Join Date: Oct 2004
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
12-21-04, 10:18 PM
Junior Code Guru
Join Date: Sep 2004
Posts: 458
Thanks: 0
Thanked 0 Times in 0 Posts
Well, the error "unexpected $ at [end of file] is fairly infuriating, until I eventually found out that $ means 'end of string'.
In other words, you might have opened an IF check without properly closing it.
After looking at your code:
Notice this line:
Where exactly is the closing curly bracket? It can't loop properly because you never told it where to stop, so naturally there was an unexpected end of file.
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off