Jump to content

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


localhost

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.