fufaso Posted December 14, 2010 Share Posted December 14, 2010 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> Quote Link to comment https://forums.phpfreaks.com/topic/221585-trying-to-insert-to-a-database-with-a-passed-value-and-a-form/ Share on other sites More sharing options...
MMDE Posted December 14, 2010 Share Posted December 14, 2010 $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... Quote Link to comment https://forums.phpfreaks.com/topic/221585-trying-to-insert-to-a-database-with-a-passed-value-and-a-form/#findComment-1147023 Share on other sites More sharing options...
BlueSkyIS Posted December 14, 2010 Share Posted December 14, 2010 $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. Quote Link to comment https://forums.phpfreaks.com/topic/221585-trying-to-insert-to-a-database-with-a-passed-value-and-a-form/#findComment-1147024 Share on other sites More sharing options...
fufaso Posted December 14, 2010 Author Share Posted December 14, 2010 $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 tried setting them all to GET and POST and still had the same issue.... ? Quote Link to comment https://forums.phpfreaks.com/topic/221585-trying-to-insert-to-a-database-with-a-passed-value-and-a-form/#findComment-1147025 Share on other sites More sharing options...
fufaso Posted December 14, 2010 Author Share Posted December 14, 2010 $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. Quote Link to comment https://forums.phpfreaks.com/topic/221585-trying-to-insert-to-a-database-with-a-passed-value-and-a-form/#findComment-1147027 Share on other sites More sharing options...
PFMaBiSmAd Posted December 14, 2010 Share Posted December 14, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/221585-trying-to-insert-to-a-database-with-a-passed-value-and-a-form/#findComment-1147028 Share on other sites More sharing options...
fufaso Posted December 14, 2010 Author Share Posted December 14, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/221585-trying-to-insert-to-a-database-with-a-passed-value-and-a-form/#findComment-1147030 Share on other sites More sharing options...
MMDE Posted December 14, 2010 Share Posted December 14, 2010 $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. Quote Link to comment https://forums.phpfreaks.com/topic/221585-trying-to-insert-to-a-database-with-a-passed-value-and-a-form/#findComment-1147031 Share on other sites More sharing options...
fufaso Posted December 14, 2010 Author Share Posted December 14, 2010 $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?? Quote Link to comment https://forums.phpfreaks.com/topic/221585-trying-to-insert-to-a-database-with-a-passed-value-and-a-form/#findComment-1147032 Share on other sites More sharing options...
MMDE Posted December 14, 2010 Share Posted December 14, 2010 $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). Quote Link to comment https://forums.phpfreaks.com/topic/221585-trying-to-insert-to-a-database-with-a-passed-value-and-a-form/#findComment-1147034 Share on other sites More sharing options...
fufaso Posted December 14, 2010 Author Share Posted December 14, 2010 $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! Quote Link to comment https://forums.phpfreaks.com/topic/221585-trying-to-insert-to-a-database-with-a-passed-value-and-a-form/#findComment-1147036 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.