Jump to content

Archived

This topic is now archived and is closed to further replies.

localhost

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

Recommended Posts

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!

[code=php:0]
<?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>

[/code]

Share this post


Link to post
Share on other sites
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:
[code]
<input type="hidden" name="fid" value="<?=((isset($_REQUEST['fid']))?($_REQUEST['fid']):(''))?>" />[/code]

Share this post


Link to post
Share on other sites
$FID = $_REQUEST['fid']; checks the url for ?fid=x and that makes $FID x. no your method doesnt work.

Share this post


Link to post
Share on other sites
try replacing $_request['fid'] with $_GET['fid']

Share this post


Link to post
Share on other sites
try
global $fid; instead of your request

that might get it

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
[code]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());[/code]

Share this post


Link to post
Share on other sites
Let's go back to your original code:
[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>
[/code]

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 [code]<?php $FID = $_REQUEST['fid']; ?>[/code] 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
[list]
[*]Put the id from the URL on the action URL of the form:
[/list][code] <form action="newthread.php?fid=<?php echo $_GET['fid'] ?>" method="POST">[/code]
[*]Or make it into a hidden field in the form
[code]<form action="newthread.php" method="POST">
<input type="hidden" name="fid" value="<?php echo $_GET['id'] ?>">
[/code][list]
[/list]
Either method will pass the value from the link used to invoke the script back to the script to process the form.

Ken

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.