Current location: Hot Scripts Forums » Programming Languages » PHP » Help with my script?


Help with my script?

Reply
  #1 (permalink)  
Old 12-19-04, 05:52 PM
culticka culticka is offline
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:

PHP Code:

<?

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 set name = '$name', email = '$email',
                                            title = '
$title', news = '$news', date = '$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
extract($r);//remove the $r so its just $variable

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>
?>
Yes, I know, it's sloppy and unorganized. But any help or feedback would be greatly appreciated .
Reply With Quote
  #2 (permalink)  
Old 12-19-04, 09:11 PM
bugalyzer bugalyzer is offline
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
Reply With Quote
  #3 (permalink)  
Old 12-19-04, 09:20 PM
culticka culticka is offline
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.
Reply With Quote
  #4 (permalink)  
Old 12-20-04, 01:43 AM
irfanbaba's Avatar
irfanbaba irfanbaba is offline
Newbie Coder
 
Join Date: Dec 2004
Location: Karachi, Pakistan
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Thumbs up

Hi
find following

PHP Code:

$sql mysql_query("insert into news set name = '$name', email = '$email',      title = '$title', news = '$news', date = '$date',"); 

replace with following
PHP Code:

$sql mysql_query("insert into news set name = '$name', email = '$email',      title = '$title', news = '$news', date = '$date'"); 

__________________
Web Programmer
web: http://www.netxs.com.pk
email: irfanmail@gmail.com
msn : irfanbaba@hotmail.com
Reply With Quote
  #5 (permalink)  
Old 12-20-04, 10:43 AM
Vulture Vulture is offline
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.
Reply With Quote
  #6 (permalink)  
Old 12-20-04, 06:28 PM
culticka culticka is offline
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...
Reply With Quote
  #7 (permalink)  
Old 12-21-04, 10:12 AM
Vulture Vulture is offline
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?
Reply With Quote
  #8 (permalink)  
Old 12-21-04, 01:45 PM
culticka culticka is offline
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>"
;
?>
Reply With Quote
  #9 (permalink)  
Old 12-21-04, 10:04 PM
culticka culticka is offline
Newbie Coder
 
Join Date: Oct 2004
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
so can anyone help?
Reply With Quote
  #10 (permalink)  
Old 12-21-04, 10:18 PM
Sabu Sabu is offline
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:
PHP Code:

while($r=mysql_fetch_array($getnews)){//while there are rows in the table 

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.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
web proxy script id10tn00b Script Requests 1 10-09-04 06:02 PM
security concerns new purchased script Ron_Long_Beach PHP 3 09-23-04 01:45 AM
Tournament Script bmfdx12 General Advertisements 1 08-23-04 09:46 AM
New Script bmfdx12 General Advertisements 0 08-22-04 11:07 PM
Is there any integrity of script rankings? webmaster@atmanager.com Hot Scripts Forum Questions, Suggestions and Feedback 17 08-06-04 12:12 AM


All times are GMT -5. The time now is 05:19 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.