Jump to content


Photo

INSERTING AS 0 WHEN NOT SUPPOSED TO BE!!!!!


  • Please log in to reply
11 replies to this topic

#1 localhost

localhost
  • Members
  • PipPipPip
  • Advanced Member
  • 152 posts

Posted 12 July 2006 - 10:44 PM

Ahh! I have been trying to figure this out but just can't get it, this file is called newthread.php. So when I go to newthread.php?fid=1 it needs to insert into the database, fid as $FID which is equal to $_REQUEST['fid'];

Below is the code, it seems to be inserting it as 0 no matter what! Please if anyone can help It will be much appreciated!

<?php
session_start();
include('inc/connect.php');

/* ****** IF SUBMIT IS HIT AND REQUIRED FIELDS ARE NOT EMPTY ****** */
if(isset($_POST['submit']) && !empty($_POST['title']) && !empty($_POST['post']))
{

$FID = $_REQUEST['fid'];

$title = $_POST['title'];
$post = $_POST['post'];

$date = date('m-d-Y');

$query = "INSERT INTO topics (`id`, `fid`, `title`, `post`, `poster`, `date`) VALUES ('', '$FID', '$title', '$post', 'Guest', '$date')";
$result = mysql_query($query) or die(mysql_error());

if($result){
echo "<script>window.location=\"index.php\"</script>";
} 
}
?>
<form action="newthread.php" method="POST">
Thread title:
<br />
<input type="text" name="title" />
<br />
Post:
<Br />
<textarea name="post" rows="20" cols="50"></textarea>
<br />
<input type="submit" name="submit" value="Post" />
</form>



#2 ShogunWarrior

ShogunWarrior
  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 12 July 2006 - 11:00 PM

But if you are posting the form, then where will the fid be coming from?

PHP won't remember the FID that was in the URL when you accessed the page, try putting this into the form, under the <FORM> tag:
<input type="hidden" name="fid" value="<?=((isset($_REQUEST['fid']))?($_REQUEST['fid']):(''))?>" />

<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

#3 localhost

localhost
  • Members
  • PipPipPip
  • Advanced Member
  • 152 posts

Posted 12 July 2006 - 11:14 PM

$FID = $_REQUEST['fid']; checks the url for ?fid=x and that makes $FID x. no your method doesnt work.

#4 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 12 July 2006 - 11:51 PM

try replacing $_request['fid'] with $_GET['fid']
regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#5 localhost

localhost
  • Members
  • PipPipPip
  • Advanced Member
  • 152 posts

Posted 13 July 2006 - 12:18 AM

get and request do the same thing, it doesnt work with either :-./

#6 localhost

localhost
  • Members
  • PipPipPip
  • Advanced Member
  • 152 posts

Posted 13 July 2006 - 01:14 AM

anybody else? this is the first spot ive gotten stuck at..

#7 cmgmyr

cmgmyr
  • Members
  • PipPipPip
  • Advanced Member
  • 1,278 posts
  • LocationUSA

Posted 13 July 2006 - 01:17 AM

try
global $fid; instead of your request

that might get it

#8 localhost

localhost
  • Members
  • PipPipPip
  • Advanced Member
  • 152 posts

Posted 13 July 2006 - 01:24 AM

like...

if(isset($_POST['submit']) && !empty($_POST['title']) && !empty($_POST['post']))
{

$FID = global $fid;

$title = $_POST['title'];
$post = $_POST['post'];

$date = date('m-d-Y');

$query = "INSERT INTO topics (`id`, `fid`, `title`, `post`, `poster`, `date`) VALUES ('', '$FID', '$title', '$post', 'Guest', '$date')";
$result = mysql_query($query) or die(mysql_error());

i get this error with that:
Parse error: parse error, unexpected T_GLOBAL in C:\wamp\www\newthread.php on line 9

#9 cmgmyr

cmgmyr
  • Members
  • PipPipPip
  • Advanced Member
  • 1,278 posts
  • LocationUSA

Posted 13 July 2006 - 01:29 AM

if(isset($_POST['submit']) && !empty($_POST['title']) && !empty($_POST['post']))
{

global $fid; 

$title = $_POST['title'];
$post = $_POST['post'];

$date = date('m-d-Y');

$query = "INSERT INTO topics (`id`, `fid`, `title`, `post`, `poster`, `date`) VALUES ('', '$FID', '$title', '$post', 'Guest', '$date')";
$result = mysql_query($query) or die(mysql_error());


#10 localhost

localhost
  • Members
  • PipPipPip
  • Advanced Member
  • 152 posts

Posted 13 July 2006 - 01:40 AM

still inserts fid as 0 when the link is newthread.php?id=1

#11 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 13 July 2006 - 03:04 AM

Let's go back to your original code:
<?php
session_start();
include('inc/connect.php');

/* ****** IF SUBMIT IS HIT AND REQUIRED FIELDS ARE NOT EMPTY ****** */
if(isset($_POST['submit']) && !empty($_POST['title']) && !empty($_POST['post']))
{

$FID = $_REQUEST['fid'];

$title = $_POST['title'];
$post = $_POST['post'];

$date = date('m-d-Y');

$query = "INSERT INTO topics (`id`, `fid`, `title`, `post`, `poster`, `date`) VALUES ('', '$FID', '$title', '$post', 'Guest', '$date')";
$result = mysql_query($query) or die(mysql_error());

if($result){
echo "<script>window.location=\"index.php\"</script>";
} 
}
?>
<form action="newthread.php" method="POST">
Thread title:
<br />
<input type="text" name="title" />
<br />
Post:
<Br />
<textarea name="post" rows="20" cols="50"></textarea>
<br />
<input type="submit" name="submit" value="Post" />
</form>

You are only doing the update when the form has been submitted. In the code for the form, nowhere do you pass the value of the fid back to the processing script. You keep on insisting that
<?php $FID = $_REQUEST['fid']; ?>
will give it to you.  It won't if you don't have it either on the URL or in the form.  If you invoke this script with the link "newthread.php?id=1", your script will not set anything to that value, since you don't reference either $_GET['id'] or $_REQUEST['id']. ShofgunWarrior was on the rught track in reply #1.

What you need to do is either
  • Put the id from the URL on the action URL of the form:
<form action="newthread.php?fid=<?php echo $_GET['fid'] ?>" method="POST">
[*]Or make it into a hidden field in the form
<form action="newthread.php" method="POST">
<input type="hidden" name="fid" value="<?php echo $_GET['id'] ?>">
Either method will pass the value from the link used to invoke the script back to the script to process the form.

Ken

#12 localhost

localhost
  • Members
  • PipPipPip
  • Advanced Member
  • 152 posts

Posted 13 July 2006 - 03:17 AM

THANK YOU VERY MUCH! I have been looking for this solution for a while, now it's working well. If there was a rep system then I would give you some.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users