form problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
irealms
Forum Contributor
Posts: 215
Joined: Mon Apr 28, 2003 7:10 am
Location: Leeds

form problem

Post by irealms »

i have this script:

Code: Select all

<?php

echo 'Alter the data below and submit to update the news article.';
$editquery = "SELECT * FROM blog where id = '$_GET[article]'";
$editresult = mysql_query($editquery, $db_conn) or die("query [$editquery] failed: ".mysql_error()); 
if (mysql_num_rows($editresult) >0)
{
	$row = mysql_fetch_assoc($editresult); 
	$date = $row['date'];
	$title = $row['title'];
	$entry = $row['entry'];
}
echo '<form method="post" action="index.php?page=artedit">';
echo 'Date:<br /><input type="text" name="date" value="'.$date.'"><br />';
echo '<br />';
echo 'Title:<br /><input type="text" name="title" value="'.$title.'"><br />';
echo '<br />';
echo 'News Entry:<br /><textarea name="entry" rows=10 cols=60 value="'.$entry.'"></textarea><br />';
echo '<input type="submit" value="Edit">';
echo '</form>';

?>
and the date/time are printed in the form but the entry variable does not show in the text area. Any ideas?
[]InTeR[]
Forum Regular
Posts: 416
Joined: Thu Apr 24, 2003 6:51 am
Location: The Netherlands

Post by []InTeR[] »

Code: Select all

<textarea name="entry" rows=10 cols=60 value="'.$entry.'"></textarea>
must be

Code: Select all

<textarea name="entry" rows=10 cols=60>".$entry."</textarea>
User avatar
irealms
Forum Contributor
Posts: 215
Joined: Mon Apr 28, 2003 7:10 am
Location: Leeds

thanks

Post by irealms »

yeah noticed that about 10mins after i posted, hehe.

I am having problems with the query atm

Code: Select all

<?php
echo 'Alter the data below and submit to update the news article.';
$editquery = "SELECT * FROM blog where id = '$_GET[article]'";
$editresult = mysql_query($editquery, $db_conn) or die("query [$editquery] failed: ".mysql_error()); 
if (mysql_num_rows($editresult) >0)
{
	$row = mysql_fetch_assoc($editresult);
	$artid = $row['id'];
	$date = $row['date'];
	$title = $row['title'];
	$entry = $row['entry'];
}
if (!isset($_POST['date']) || !isset($_POST['title']) || !isset($_POST['entry']))
{
echo '<form method="post" action="index.php?page=artedit">';
echo 'Date:<br /><input type="text" name="date" value="'.$date.'"><br />';
echo '<br />';
echo 'Title:<br /><input type="text" name="title" value="'.$title.'"><br />';
echo '<br />';
echo 'News Entry:<br /><textarea name="entry" rows=10 cols=60>'.$entry.'</textarea><br />';
echo '<input type="submit" value="Edit">';
echo '</form>';
}
else
{
$editedquery = "UPDATE blog set (date,title,entry) VALUES ('$_POST[date]','$_POST[title]','$_POST[entry]') where id = '$_GET[article]'";
$editedresult = mysql_query($editedquery, $db_conn) or die("query [$editedquery] failed: ".mysql_error()); 
if (mysql_num_rows($editedresult) >0)
	{
	echo 'You have updated article '.$artid.' "'.$title.'"';
	}
}

?>
problem is the update query won't work.
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

GO, GO, GO, STOP

Post by phpScott »

GO, GO, GO, STOP
first of all I have never had much success with using $_POST['varName'] in my queries but what ever works.

but in this line

Code: Select all

<?php
$editedquery = "UPDATE blog set (date,title,entry) VALUES ('$_POST[date]','$_POST[title]','$_POST[entry]') where id = '$_GET[article]'"; 
?>
you go $_POST $_POST $_POST $_GET
I don't think you mix methods like that. If you forming is submiting via the Post method you can only use POST.

phpScott
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Re: thanks

Post by twigletmac »

irealms wrote:problem is the update query won't work.
Could you elaborate a bit further.

Mac
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Post by liljester »

i dont think you can have post and get data either... unless you post the data like so: action="page.php?article=1". ive never tried it tho..
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

It is entirely possible to do that - as you can when you post a form send a bunch of information in the URL as well as sending infomation by POST.

Mac
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

ohhhh

Post by phpScott »

thanks twig for clearing that up. I can see a few applications where that would be useful :D

phpScott
User avatar
irealms
Forum Contributor
Posts: 215
Joined: Mon Apr 28, 2003 7:10 am
Location: Leeds

Post by irealms »

sure twig the error i get is:

query [UPDATE blog SET date='10/06/03 , title='Forum error fixed' , entry='Fixed the error that was popping up in the forums.' where id=] failed:
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

echo '<form method="post" action="index.php?page=artedit">';
where id = '$_GET[article]'"
I might be wrong but I can't see a GET-parameter article, only $_GET['page']=='artedit'
User avatar
irealms
Forum Contributor
Posts: 215
Joined: Mon Apr 28, 2003 7:10 am
Location: Leeds

Post by irealms »

ok go to http://www.irealms.co.uk/crimson and logon as test/test

go to site announcements and click edit on one of the articles. You will see the get i'm using is sent by the link when you click on the article edit. :)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

http://www.irealms.co.uk\crimson\index.php?page=siteann : page not found

edit: ah, because of the backslashes. Must be http://www.irealms.co.uk/crimson/index.php?page=siteann
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

you do not pass the article id from the edit-form to the update-script
<form method="post" action="index.php?page=artedit">Date:<br /><input type="text" name="date" value="04/06/03" style="font-size:10px;border:solid 1px;"><br /><br />Title:<br /><input type="text" name="title" value="Smilies <img src="http://irealms.co.uk/crimson/forums/smi ... iley15.gif border="0" />" style="font-size:10px;border:solid 1px;"><br /><br />News Entry:<br /><textarea name="entry" rows=10 cols=60 style="font-size:12px;border:solid 1px;"><p>
I have installed a smilie mod to the forum though at present they don't seem to show in the posts, this is being looked into. The text formatting seems to work though! Also started work on a site roster, at present it just shows username, main character and rank. This feature may not be completed until nearer the games release as at present we don't know exact details regarding how many characters your allowed and other character related areas.
</p></textarea><br /><input type="submit" value="Edit" ></form>
User avatar
irealms
Forum Contributor
Posts: 215
Joined: Mon Apr 28, 2003 7:10 am
Location: Leeds

ok

Post by irealms »

ok changed it a bit and i'm now getting:

query [UPDATE blog set (date,title,entry) VALUES ('10/06/03','Forum error fixed','Fixed the error that was popping up in the forums.') where id = '8'] failed:

so it is using the right id

code is:

Code: Select all

<?php
echo 'Alter the data below and submit to update the news article.';
$editquery = "SELECT * FROM blog where id = '$_GET[article]'";
$editresult = mysql_query($editquery, $db_conn) or die("query [$editquery] failed: ".mysql_error()); 
if (mysql_num_rows($editresult) >0)
{
	$row = mysql_fetch_assoc($editresult);
	$artid = $row['id'];
	$date = $row['date'];
	$title = $row['title'];
	$entry = $row['entry'];
}
if (!isset($_POST['date']) || !isset($_POST['title']) || !isset($_POST['entry']))
{
echo '<form method="post" action="index.php?page=artedit">';
echo '<input type="hidden" name="id" value="'.$artid.'">';
echo 'Date:<br /><input type="text" name="date" value="'.$date.'" style="font-size:10px;border:solid 1px;"><br />';
echo '<br />';
echo 'Title:<br /><input type="text" name="title" value="'.$title.'" style="font-size:10px;border:solid 1px;"><br />';
echo '<br />';
echo 'News Entry:<br /><textarea name="entry" rows=10 cols=60 style="font-size:12px;border:solid 1px;">'.$entry.'</textarea><br />';
echo '<input type="submit" value="Edit" >';
echo '</form>';
}
else
{
$editedquery = "UPDATE blog set (date,title,entry) VALUES ('$_POST[date]','$_POST[title]','$_POST[entry]') where id = '$_POST[id]'"; 
$editedresult = mysql_query($editedquery, $db_conn) or die("query [$editedquery] failed: ".mysql_error()); 
if (mysql_num_rows($editedresult) >0)
	{
	echo 'You have updated article '.$artid.' "'.$title.'"';
	}
}

?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

ok, now it's time to read the manual entry for UPDATE ;)
You used the syntax for INSERT INTO

and also take a look at http://php.net/mysql_escape_string
Post Reply