Page 1 of 1

dynamic news code script error

Posted: Thu Nov 10, 2005 8:07 am
by merlin89
feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


Hi,

Iam working on a dynamic news script  provided by a magazine tutorial but gives me the following error when I try to enter a test news item into the database it reports

Code: Select all

The query is:
INSERT into dynamic_news values ('0','','','','')
The news story was successfully added to the database.
Add another news story

the code for the addnewsform.htm file is

Code: Select all

<HTML>
<HEAD>
<TITLE>Add news form</TITLE>
</HEAD>
<BODY>
<FORM ACTION="addnews.php" METHOD=POST>
<TABLE border=0 cellspacing=0 cellpadding=0>
<TR><TD>Date</TD>
<TD><INPUT NAME="Array[Date]" TYPE=TEXT id="Array[Date]"SIZE=8></TD>
</TR><BR><TR><TD>Headline</TD><TD><INPUT TYPE=TEXT NAME="Array[Headline]"SIZE=50><TR><TD><BR>
<TR><TD>Story</TD><TD><TEXTAREA NAME="Array[Story]"ROWS=8 COLS=80></TEXTAREA></TD></TR><BR><BR>
</TABLE><INPUT TYPE=SUBMIT NAME="SUBMIT" VALUE="Save news story to database">
</FORM>
</BODY>
</HTML>

The code for the addnews.php file is:

Code: Select all

<html>
<head>
<title>Add news story to database</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php
//match the form fields to the variables
$Array[Date]=trim($Array[Date]);
$Array[Headline]=trim($Array[Headline]);
$Array[Story]=trim($Array[Story]);
//assign variables to your database details
$DBName="maccms1";
$TableName="dynamic_news";
$mysql_link=mysql_connect("localhost","username","password");
//insert array variables into database
$Query="INSERT into $TableName values ('0','$Array[Date]','$Array[Headline]','$Array[Extracopy]','$Array[Topic1]')";
//print on screen the record added
print("The query is:<BR>$Query<P>\n");
//Tell me if the record has been added
if(mysql_db_query($DBName,$Query, $mysql_link)){
print("The news story was successfully added to the database.<BR>
<A HREF=\"addnewsform.htm\">Add another news story</A>\n");
}else{
print("Sorry - we could not add your story<BR>\n");
}
//close the connection to the database
mysql_close($mysql_link);
?>
</body>
</html>

I reckon its not passing the inputs from the form correctly to the arrays but unsure how this is done does anybody have any advice. Try it for yourself at maccms.co.uk/addnewsform.htm.

Many thanks in advance


feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Thu Nov 10, 2005 8:25 am
by feyd
  1. Quote your named (string) array indices:

    Code: Select all

    $Array[Date]
    should be

    Code: Select all

    $Array['Date']
  2. your code assumes register_globals is on when it is not. Use $_POST['Array']['Date'] etc etc

thank feyd

Posted: Thu Nov 10, 2005 11:23 am
by merlin89
many thanks feyd will give it a try tonight ;sorry about the unformatted posting to the forum also perhaps should read the advice 1st

cheers
Ian

inser query still not working my code still incorrect

Posted: Fri Nov 11, 2005 2:10 am
by merlin89

Code: Select all

Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in maccms.co.uk\addnews.php on line 18
as you can see I tried to change the $Query but I dont think I have got it right

Code: Select all

<html>
<head>
<title>Add news story to database</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php
//match the form fields to the variables
$Array['Date']=trim($Array['Date']);
$Array['Headline']=trim($Array['Headline']);
$Array['Story']=trim($Array['Story']);
//assign variables to your database details
$DBName="maccms1";
$TableName="dynamic_news";
$mysql_link=mysql_connect("localhost","user","password");
//insert array variables into database
$Query="INSERT into $TableName values ('0','$_POST['Array']['Date']','$_POST['Array']['Headline']','$_POST['Array']['Extracopy']','$_POST['Array']['Topic1']')";
//print on screen the record added
print("The query is:<BR>$Query<P>\n");
//Tell me if the record has been added
if(mysql_db_query($DBName,$Query, $mysql_link)){
print("The news story was successfully added to the database.<BR>
<A HREF=\"addnewsform.htm\">Add another news story</A>\n");
}else{
print("Sorry - we could not add your story<BR>\n");
}
//close the connection to the database
mysql_close($mysql_link);
?>
</body>
</html>
Many Thanks, your help is very much appreciated

Posted: Fri Nov 11, 2005 2:17 am
by n00b Saibot

Code: Select all

$Query="INSERT into $TableName values ('0','$_POST['Array']['Date']','$_POST['Array']['Headline']','$_POST['Array']['Extracopy']','$_POST['Array']['Topic1']')";
should be

Code: Select all

$Query="INSERT into {$TableName} values ('0','{$_POST['Array']['Date']}','{$_POST['Array']['Headline']}','{$_POST['Array']['Extracopy']}','{$_POST['Array']['Topic1']}')";
always use {} when printing variable inside a string :wink: