Jump to content

Trying to insert to a database with a passed value and a form


Recommended Posts

Hey all,

 

I'm very new to php and am having an issue writing to my database with a passed value and info taking from a form and am lost as to what to do.

The $comm_id is the value I am getting from another php page that I am passing in the link from the other page. I need to insert that value, the session variable, and the $cmt value, which is taken from the user input from the form. As it stands now, when I run this, the $comm_id and $u_id insert and the $cmt is blank, BUT $cmt is echoing on the screen with the proper value.

 

Any tips or direction would be great as i've been trying to sort this out for days now and i'm stumped.

 

<?php 

$host='localhost';
$user='username';
$password='password';
$dbname='db_a';
session_start();
$id_link=mysql_connect($host,$user,$password);
mysql_select_db($dbname, $id_link);

$comm_id = $_GET['commId'];
$u_id = $_SESSION['userId']; 
echo $comm_id;
echo $u_id;
    

$cmt = $_POST["comm"];
mysql_query("insert into Comments values($u_id, $comm_id,'$cmt');");
echo $cmt;

?>
   
<FORM ACTION="test.php" METHOD="POST">
<input type="text" name="comm">
<input type="SUBMIT">
</FORM> 

$query="insert into Comments values($u_id, $comm_id,'$cmt')";
echo $query;
mysql_query($query) or die(mysql_error());

Correct me if I'm wrong, but you never choose what columns to insert those values on...

Still, if you use what I wrote above, you may get some idea of what is wrong...

$query="insert into Comments values($u_id, $comm_id,'$cmt')";
echo $query;
mysql_query($query) or die(mysql_error());

Correct me if I'm wrong, but you never choose what columns to insert those values on...

Still, if you use what I wrote above, you may get some idea of what is wrong...

 

there are only 3 columns in the db, so there is no need. the first 2 values are inserting fine, just the last one isn't. If I change $cmt to a hard coded value, it all works.

Your form processing code is being executed unconditionally every time the page is requested, so of course a blank $cmt is inserted when you first browse to the file.

yeah I've noticed that, but even when I hit the submit button with a value in the text area, the first 2 values are inserted, but the $cmt is not. Is there some way to get all 3 values inserting?

$comm_id is set from GET, $cmt is set from POST. you'll probably get one or the other, GET or POST, but probably not both.

I'm not too sure if it is wrong to get them from two different places, but the thing is, the action of the form will never send the GET data, so he would need to:

echo '<form action="test.php?commID='.$_GET['commId'].'" method="post">';

this is of course only if the page already has a commID get data in the url.

$comm_id is set from GET, $cmt is set from POST. you'll probably get one or the other, GET or POST, but probably not both.

I'm not too sure if it is wrong to get them from two different places, but the thing is, the action of the form will never send the GET data, so he would need to:

echo '<form action="test.php?commID='.$_GET['commId'].'" method="post">';

this is of course only if the page already has a commID get data in the url.

yeah the url does have the commID data in it.

 

does that piece of code go within the <?php    ?>  tags? and if so, does that mean I remove the form tag from outside the php tags?

 

so is it even possible to insert a value that you GET from the url into a database with a value you get from a form??

$comm_id is set from GET, $cmt is set from POST. you'll probably get one or the other, GET or POST, but probably not both.

I'm not too sure if it is wrong to get them from two different places, but the thing is, the action of the form will never send the GET data, so he would need to:

echo '<form action="test.php?commID='.$_GET['commId'].'" method="post">';

this is of course only if the page already has a commID get data in the url.

yeah the url does have the commID data in it.

 

does that piece of code go within the <?php    ?>  tags? and if so, does that mean I remove the form tag from outside the php tags?

 

so is it even possible to insert a value that you GET from the url into a database with a value you get from a form??

 

yes, it contains some php, and must therefore be inside: <?php and ?>

the action attribute is like a link about where to send the form data. What we do here is to make sure the url is correct, (including the get data).

$comm_id is set from GET, $cmt is set from POST. you'll probably get one or the other, GET or POST, but probably not both.

I'm not too sure if it is wrong to get them from two different places, but the thing is, the action of the form will never send the GET data, so he would need to:

echo '<form action="test.php?commID='.$_GET['commId'].'" method="post">';

this is of course only if the page already has a commID get data in the url.

yeah the url does have the commID data in it.

 

does that piece of code go within the <?php    ?>  tags? and if so, does that mean I remove the form tag from outside the php tags?

 

so is it even possible to insert a value that you GET from the url into a database with a value you get from a form??

 

yes, it contains some php, and must therefore be inside: <?php and ?>

the action attribute is like a link about where to send the form data. What we do here is to make sure the url is correct, (including the get data).

 

THANK YOU SOOOOOOOOOOOOOOOOOOOOOO MUCH!!!

 

that got it going... wow, that's been driving me nuts for 3 days now!

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.