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
bfsog
Forum Newbie
Posts: 8 Joined: Thu Feb 10, 2005 6:14 am
Location: UK
Post
by bfsog » Fri Feb 11, 2005 12:31 pm
Im completing a blog script, and actually this wasnt a known bug until 5 minutes ago. I try and edit a post, by putting in the variables stored in a database table.
The code is kinda sloppy, not very good for its purpose, but im new..
Code: Select all
<?php
session_start();
header("Cache-control: private");
?>
<?php
include ("connect.php");
if(!$id){$id=1;}
else {$id=$_GETї'id'];}
$sql="SELECT * FROM content where id='$id'";
$res=mysql_query($sql) or die("Error: ".mysql_error());
$row=mysql_fetch_array($res);
if($submit=="Update")
{
$sql="UPDATE content SET comment='.$change.' where id='.$id.'";
mysql_query($sql) or die("Query failed: ".mysql_error());
}
?>
<html>
<head>
<?php include ("constants.inc"); ?>
<?php echo"<title>$SiteTitle</title>"?>
<?php echo"<link rel="stylesheet" href="$cssfile" type="text/css">"?>
</head>
<body>
<h1 style="font-family: verdana;"><?php echo "$SiteTitle"; ?></h1><br><br>
<?php
if (isset($_SESSIONї'name']))
{
include ("links.inc");
?>
<form name="alter" method="post" action="<?php echo $PHP_SELF ?>">
<p>
<textarea name="change" cols="100" rows="20">
<?php echo $rowїcomment]; ?></textarea>
</p>
<p>
<input type="submit" name="submit" value="Update">
</p>
</form>
<?php
}
else {
echo 'You have to sign in first ';
echo '<a href="login.php" title="Login">Login</a>';
}
?>
Thanks in advance, it means alot
timvw
DevNet Master
Posts: 4897 Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium
Post
by timvw » Fri Feb 11, 2005 12:37 pm
First: read how to enable error_reporting and how to display those messages...
bfsog wrote: Code: Select all
if(!$id){$id=1;}
else {$id=$_GETї'id'];}
This will always assign 1 to $id (if register_globals = OFF)
This assumes register_globals is ON, a bad thing (tm)
Might want to tell us what exactly is not working.
djot
Forum Contributor
Posts: 313 Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:
Post
by djot » Fri Feb 11, 2005 12:37 pm
-
if(!$id){$id=1;}
else {$id=$_GET['id'];}
why not this way?
if (isset($_GET['id'])) {
$id = $_GET['id'];
}
else {$id=1;}
$sql="UPDATE content SET comment='.$change.' where id='.$id.'";
Your single quotes are strings inside the sql-statement, use double quotes instead:
$sql="UPDATE content SET comment=".$change." where id=".$id." ";
djot
-
Last edited by
djot on Fri Feb 11, 2005 12:40 pm, edited 2 times in total.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Fri Feb 11, 2005 12:38 pm
any errors that show? are register_globals on? error_reporting obviously isn't set to E_ALL..
woo clash of posts.. and I'm the last
nice fella's
djot
Forum Contributor
Posts: 313 Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:
Post
by djot » Fri Feb 11, 2005 12:47 pm
-
woo clash of posts.. and I'm the last nice fella's
Yeah great
I "won" vs feyd, although I did type quite more this time, but "lost" vs timvw again...
Anyway, I don't see this as a competition, it's just funny.
Perhaps I should stick to what jason said:
*quoted from jason viewtopic.php?t=9704&start=0 I tend not to answer questions because when I go to answer them, someone else has already done that.
djot
PS: timVW, please rename to timBMW, I don't like Volkswagen.
-
bfsog
Forum Newbie
Posts: 8 Joined: Thu Feb 10, 2005 6:14 am
Location: UK
Post
by bfsog » Fri Feb 11, 2005 2:46 pm
My code still does not update the record. I have stripped all the useless stuff, but all the php remains.
Code: Select all
<?php
session_start();
header("Cache-control: private");
?>
<?php
include ("connect.php");
if (isset($_GETї'id'])) {
$id = $_GETї'id'];
}
else {$id=1;}
$sql="SELECT * FROM content where id='$id'";
$res=mysql_query($sql) or die("Error: ".mysql_error());
$row=mysql_fetch_array($res);
if($submit=="Update")
{
$sql="UPDATE content SET comment='.$change.' where id='.$id.' ";
mysql_query($sql) or die("Query failed: ".mysql_error());
header( 'Location: editpost.php?id='.$id.'');
}
?>
I dont get an error, after submitting the form, it reloads to editpost.php?id=1 always, instead id=whatever post id i chose to edit
Then I go to my viewing page where I see all posts, and the $row[comment] is still the same as it was before i "supposedly" edited it.
I appreciate your help
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Fri Feb 11, 2005 2:50 pm
where/when is the page called with page.php?id=10
it's not in this code.
Your code still assumes register_globals being on.
bfsog
Forum Newbie
Posts: 8 Joined: Thu Feb 10, 2005 6:14 am
Location: UK
Post
by bfsog » Fri Feb 11, 2005 5:30 pm
I have a file, called edit.php, which is where the user enters the id of the record to edit.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Fri Feb 11, 2005 5:35 pm
well, your code is set up to set $id to 1 if id wasn't present in the url.. are you sure everything is coming across correctly?
bfsog
Forum Newbie
Posts: 8 Joined: Thu Feb 10, 2005 6:14 am
Location: UK
Post
by bfsog » Fri Feb 11, 2005 5:44 pm
Not sure what you mean. Sorry I am new to this.
All I want is to be able to edit records.
Im sure I had this working, but it will not edit now
Thanks again for taking time to help me.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Fri Feb 11, 2005 5:47 pm
when edit.php calls this script, does the url have '?id=10' in it?
bfsog
Forum Newbie
Posts: 8 Joined: Thu Feb 10, 2005 6:14 am
Location: UK
Post
by bfsog » Fri Feb 11, 2005 5:52 pm
yes. edit.php forms action is editpost.php, using get method
when i goto editpost.php, from edit.php, in the url it is ?id=10 or whatever
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Fri Feb 11, 2005 5:54 pm
okay, now, your original code shows
Code: Select all
if(!$id){$id=1;}
else {$id=$_GETї'id'];}since $id doesn't exist, likely, I would expect it to always be 1.. unless you turned on register_globals.
bfsog
Forum Newbie
Posts: 8 Joined: Thu Feb 10, 2005 6:14 am
Location: UK
Post
by bfsog » Fri Feb 11, 2005 5:56 pm
is there a solution? i dont know how to turn them on...
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Fri Feb 11, 2005 5:57 pm
do not turn on register_globals.. the code you need to use is in edit.php it looks like:
Code: Select all
if (isset($_GETї'id'])) {
$id = $_GETї'id'];
}
else {$id=1;}