Page 1 of 1

Cannot edit record

Posted: Fri Feb 11, 2005 12:31 pm
by bfsog
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)&#123;$id=1;&#125;
else &#123;$id=$_GET&#1111;'id'];&#125;

$sql="SELECT * FROM content where id='$id'";

$res=mysql_query($sql) or die("Error: ".mysql_error());
$row=mysql_fetch_array($res);

if($submit=="Update")
&#123;
 $sql="UPDATE content SET comment='.$change.' where id='.$id.'";
 mysql_query($sql) or die("Query failed: ".mysql_error());
&#125;

?>
<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&#1111;'name']))
&#123;
include ("links.inc");
?>

 <form name="alter" method="post" action="<?php echo $PHP_SELF ?>">
  <p>
<textarea name="change" cols="100" rows="20">
<?php echo $row&#1111;comment]; ?></textarea>
</p>
  <p>
    <input type="submit" name="submit" value="Update">
</p>
</form>

<?php

&#125;

else &#123;

echo 'You have to sign in first ';
echo '<a href="login.php" title="Login">Login</a>';

&#125;

?>
Thanks in advance, it means alot

Re: Cannot edit record

Posted: Fri Feb 11, 2005 12:37 pm
by timvw
First: read how to enable error_reporting and how to display those messages...
bfsog wrote:

Code: Select all

if(!$id)&#123;$id=1;&#125;
else &#123;$id=$_GET&#1111;'id'];&#125;
This will always assign 1 to $id (if register_globals = OFF)

bfsog wrote:

Code: Select all

if($submit=="Update")
This assumes register_globals is ON, a bad thing (tm)



Might want to tell us what exactly is not working.

Posted: Fri Feb 11, 2005 12:37 pm
by djot
-
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
-

Posted: Fri Feb 11, 2005 12:38 pm
by feyd
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 :P

Posted: Fri Feb 11, 2005 12:47 pm
by djot
-
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.
-

thanks, but still not functioning

Posted: Fri Feb 11, 2005 2:46 pm
by bfsog
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&#1111;'id'])) &#123;
$id = $_GET&#1111;'id'];
&#125;
else &#123;$id=1;&#125;

$sql="SELECT * FROM content where id='$id'";

$res=mysql_query($sql) or die("Error: ".mysql_error());
$row=mysql_fetch_array($res);

if($submit=="Update")
&#123;
 $sql="UPDATE content SET comment='.$change.' where id='.$id.' ";
 mysql_query($sql) or die("Query failed: ".mysql_error());
 header( 'Location: editpost.php?id='.$id.'');
&#125;

?>
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

Posted: Fri Feb 11, 2005 2:50 pm
by feyd
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.

Posted: Fri Feb 11, 2005 5:30 pm
by bfsog
I have a file, called edit.php, which is where the user enters the id of the record to edit.

Posted: Fri Feb 11, 2005 5:35 pm
by feyd
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?

Posted: Fri Feb 11, 2005 5:44 pm
by bfsog
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.

Posted: Fri Feb 11, 2005 5:47 pm
by feyd
when edit.php calls this script, does the url have '?id=10' in it?

Posted: Fri Feb 11, 2005 5:52 pm
by bfsog
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

Posted: Fri Feb 11, 2005 5:54 pm
by feyd
okay, now, your original code shows

Code: Select all

if(!$id)&#123;$id=1;&#125;
else &#123;$id=$_GET&#1111;'id'];&#125;
since $id doesn't exist, likely, I would expect it to always be 1.. unless you turned on register_globals.

Posted: Fri Feb 11, 2005 5:56 pm
by bfsog
is there a solution? i dont know how to turn them on...

Posted: Fri Feb 11, 2005 5:57 pm
by feyd
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&#1111;'id'])) &#123;
$id = $_GET&#1111;'id'];
&#125;
else &#123;$id=1;&#125;