Inserting Session into the database

This is a discussion on "Inserting Session into the database" within the PHP Forum section. This forum, and the thread "Inserting Session into the database are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Jun 16th, 2007, 14:14
Junior Member
Join Date: Jun 2006
Location: usa
Age: 29
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Inserting Session into the database

Ok, I am very confuse on this one,

I have a page where users can send a message into the database which eventually the business can log in and he/she can see, his/her messages. that had been posted to his business,

i am doing it with a session which, i will call later to display the messages sent to that business

my problem is that the $_SESSION['BusinessName']; is not bein taken to page2. I did some testing and it takes other values to page2 but not BusinessName value

here is my code for page1 please help

PHP: Select all


<?php 
session_start
(); 
$_SESSION['BusinessName'] ='$BusinessName'
$_SESSION['test']='test 1'// this is the test line

?> 


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

if(isset($_GET['BusinessName'])){ 


$query = "SELECT * FROM business_info WHERE `BusinessName`= '$BusinessName' "; 
$result = mysql_query($query) or die (mysql_error()); 
$row = mysql_fetch_assoc($result); 

$BusinessName= ($row['BusinessName']); 
$Keyword =($row['Keyword']); 
$Picture1 =  ($row['Picture1']); 
$Headline = ($row['Headline']); 
$Slogan2 = ($row['Slogan2']); 
$Description1 =($row['Description1']); 
$Description2 = ($row['Description2']); 
$Description3= ($row['Description3']); 
$Contact2 =  ($row['Contact2']); 
$Picture2 = ($row['Picture2']); 
$Picture3 = ($row['Picture3']); 
$Business_Address=($row['Business_Address']); 
$make=($row['make']); 
$type=($row['type']); 
$Tel=($row['Tel']); 
$Website=($row['Website']); 

?> 


<center>

<?php 
 
$_SESSION
['BusinessName']; 
?> 

<pre>_SESSION:<?php print_r($_SESSION); ?></pre>


<table width="200" border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
  <tr>
    <td valign="top">
    <?php include("includefiles/banner.php"); ?>
    <table width="778" border="0" cellpadding="0" cellspacing="0">
      <tr>



this // <pre>_SESSION:<?php print_r($_SESSION); ?></pre>

// on page1 display _SESSION:Array
(
    [BusinessName] => NEW ENGLAND SEAFOODS
    [test] => test 1
)
PHP: Select all


AND THIS IS MY CODE FOR page2

<?php
session_start
();


?>

<html>
<head>


<title>Send Message</title>

<?
  $BusinessName 
addslashes($_POST['BusinessName']);

  
$from addslashes($_POST['from']);
  
$statusaddslashes($_POST['status']);
  
$subjectaddslashes($_POST['subject']);
  
$messageaddslashes($_POST['message']);
 
$BusinessName$_SESSION['BusinessName'];
if(isset(
$_SESSION['BusinessName'])){
$query "INSERT INTO  `messages` (`BusinessName`,`date`,`from`,`status`,`subject`,`message`)
VALUES ('"
.$_SESSION['BusinessName']."','".$date."','".$from."', '".$status."','".$subject."',
'"
.$message."')";

$result mysql_query($query);
echo 
mysql_error();

if(
$result)
         {
echo 
mysql_affected_rows()." .Your Message have been sent. We will get back to you. <br>";
          }
}


?>


<center>





<pre>_SESSION:<?php print_r($_SESSION); ?></pre> This on page2 display[b] SESSION:Array
(
    [BusinessName] => 
    [test] => test 1
)[/b]

<table width="200" border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
  <tr>
    <td valign="top">
    <?php include("includefiles/banner.php"); ?>
<td width="282"><? echo'<input name="from"  type="text" size="60">'?></td>
                            </tr>
                            <tr>
                              <td bgcolor="#CCCCCC"><strong>Subject </strong></td>
                              <td><? echo'<input name="subject" type="subject"  size="60">'?></td>
                            </tr>
                            <tr>
                              <td bgcolor="#CCCCCC"><strong>Body</strong></td>
                              <td><p>
                                <? echo'<textarea name="message" cols="75" rows="10"></textarea>'?>
                              </p>
                                <p>&nbsp;</p></td>
                            </tr>
                            <tr>
I am able to insert all the info exept for the BusinessName wich actually is not on page2 that is why is not going into the database
Reply With Quote

  #2 (permalink)  
Old Jun 16th, 2007, 15:55
SuperMember

SuperMember
Join Date: Jun 2007
Location: uk
Posts: 459
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Inserting Session into the database

Hi I am quite new to php but looking at your code on page1 shouldnt you have something like this.

PHP: Select all

<?php 
session_start
(); 
$_SESSION['BusinessName'] ='$BusinessName'
$_SESSION['test']='test 1'// this is the test line
 
if(isset($_GET['BusinessName'])){ 
 
$query "SELECT * FROM business_info WHERE `BusinessName`= '$BusinessName' "
$result mysql_query($query) or die (mysql_error()); 
$row mysql_fetch_assoc($result); 
$BusinessName= ($row['BusinessName']); 
$Keyword =($row['Keyword']); 
$Picture1 =  ($row['Picture1']); 
$Headline = ($row['Headline']); 
$Slogan2 = ($row['Slogan2']); 
$Description1 =($row['Description1']); 
$Description2 = ($row['Description2']); 
$Description3= ($row['Description3']); 
$Contact2 =  ($row['Contact2']); 
$Picture2 = ($row['Picture2']); 
$Picture3 = ($row['Picture3']); 
$Business_Address=($row['Business_Address']); 
$make=($row['make']); 
$type=($row['type']); 
$Tel=($row['Tel']); 
$Website=($row['Website']); 

?> 
 
<center>
<?php 
 
$_SESSION
['BusinessName']; 
?> 
<pre>_SESSION:<?php print_r($_SESSION); ?></pre>
 
<table width="200" border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
  <tr>
    <td valign="top">
    <?php include("includefiles/banner.php"); ?>
    <table width="778" border="0" cellpadding="0" cellspacing="0">
      <tr>
 
this // <pre>_SESSION:<?php print_r($_SESSION); ?></pre>
// on page1 display _SESSION:Array
(
    [BusinessName] => NEW ENGLAND SEAFOODS
    [test] => test 1
)
Reply With Quote
  #3 (permalink)  
Old Jun 16th, 2007, 15:58
Junior Member
Join Date: Jun 2006
Location: usa
Age: 29
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Inserting Session into the database

ok, that is what i have
Reply With Quote
  #4 (permalink)  
Old Jun 16th, 2007, 16:03
SuperMember

SuperMember
Join Date: Jun 2007
Location: uk
Posts: 459
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Inserting Session into the database

this is the code you posted.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

if(isset($_GET['BusinessName'])){


$query = "SELECT * FROM business_info WHERE `BusinessName`= '$BusinessName' ";
$result = mysql_query($query) or die (mysql_error());
$row = mysql_fetch_assoc($result);

$BusinessName= ($row['BusinessName']);
$Keyword =($row['Keyword']);
$Picture1 = ($row['Picture1']);
$Headline = ($row['Headline']);
$Slogan2 = ($row['Slogan2']);
$Description1 =($row['Description1']);
$Description2 = ($row['Description2']);
$Description3= ($row['Description3']);
$Contact2 = ($row['Contact2']);
$Picture2 = ($row['Picture2']);
$Picture3 = ($row['Picture3']);
$Business_Address=($row['Business_Address']);
$make=($row['make']);
$type=($row['type']);
$Tel=($row['Tel']);
$Website=($row['Website']);
}
?>



There is no <? between </head> and

if(isset($_GET['BusinessName'])){
Reply With Quote
  #5 (permalink)  
Old Jun 16th, 2007, 16:10
Junior Member
Join Date: Jun 2007
Location: Los Angeles
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Inserting Session into the database

In your first script:
$_SESSION['BusinessName'] ='$BusinessName';

should be
$_SESSION['BusinessName'] = $BusinessName;

(test var is ok with the quotes since its a string).

In your page 2 script:

$BusinessName = addslashes($_POST['BusinessName']);
is used ok here but later in your code you reload the same var again without the slashes so you're over writing your BussinessName var!

Rather than use [SESSION] so much (which you can do) I declare my php vars needed as globals as globals - that way I can access them from any script. I also use this naming convention so I know a global php var from a local php var:

$php_g_var = 'test'; // global var

$php_var is a local var.

Being new to php you may not know that the form that does the post method call will pass all of it's html tag objects to the called php script ONLY! Therfore, you have to use or assign to global vars those values from the calling form. Yolu can't use those html object values on any other php script other than the one called in the method post.

For example I can do this in the called php script:

$php_g_var = $chk_include_reports;

Assigns checkbox true or false to a php global var. You cannot do this from your page 2 script - it won't work. So you have to store the value in a global php var.

I hope this helps.

RalphF
GoldRushWebHosting.com
Reply With Quote
  #6 (permalink)  
Old Jun 16th, 2007, 18:51
Junior Member
Join Date: Jun 2006
Location: usa
Age: 29
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Exclamation Re: Inserting Session into the database

Ok, it is making some sense to me now, So basicly i am destroying BusinessName value on page2

ok Here is my question where i place

BusinessName = addslashes($_POST['BusinessName']);
because i added it at the top of the page where i open the session and i was still getting the same error


i guess, i will nee a little bit of help
here
Reply With Quote
  #7 (permalink)  
Old Jun 16th, 2007, 21:00
Junior Member
Join Date: Jun 2007
Location: Los Angeles
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Inserting Session into the database

This line:

BusinessName = addslashes($_POST['BusinessName']);

should be like this:

$BusinessName = $_POST['BusinessName'];

You don't need the addslashes() function and you forgot the $ char on the php var.

Try that - if that doesn't work, show me your code thus far.

RalphF
GoldRushWebHosting.com
Reply With Quote
  #8 (permalink)  
Old Jun 16th, 2007, 22:12
Junior Member
Join Date: Jun 2006
Location: usa
Age: 29
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Inserting Session into the database

Well that didnt work

here is my full code for page2

PHP: Select all

 
<?php
session_start
();
$BusinessName $_POST['BusinessName'];
 
?>
<html>
<head>
 
<title>Send Message</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
 
 <body  bgcolor="#E5E5E5">
<?php
$host 
"localhost";
$username "townsfin_localho";
$password "abc123";
$database "townsfin_contacts";
 
$db mysql_connect($host$username$password) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());
 
  
$from addslashes($_POST['from']);
  
$statusaddslashes($_POST['status']);
  
$subjectaddslashes($_POST['subject']);
  
$messageaddslashes($_POST['message']);
  
$BusinessName$_SESSION['BusinessName'];
if(isset(
$_SESSION['BusinessName'])){
$query "INSERT INTO  `messages` (`BusinessName`,`date`,`from`,`status`,`subject`,`message`)
VALUES ('"
.$_SESSION['BusinessName']."','".$date."','".$from."', '".$status."','".$subject."',
'"
.$message."')";
$result mysql_query($query);
echo 
mysql_error();
if(
$result)
         {
echo 
mysql_affected_rows()." .Your Message have been sent. We will get back to you. <br>";
          }
}
 
?>
 
<center>
 
 
<pre>_SESSION:<?php print_r($_SESSION); ?></pre>
<table width="200" border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
  <tr>
    <td valign="top">
 <?php include("includefiles/banner.php"); ?>
 <table width="778" border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td width="778" valign="top">
  <table width="778" border="0" cellpadding="0" cellspacing="0">
 
          <tr>
            <td valign="top" bgcolor="#f90102">   <?php
       
echo "<h1><font color='ffffff'><marquee>$BusinessName </marquee></font></h1>"?>
 
 
 
</td>
          </tr>
        </table></td>
Reply With Quote
  #9 (permalink)  
Old Jun 16th, 2007, 22:25
Junior Member
Join Date: Jun 2007
Location: Los Angeles
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Inserting Session into the database

Your still pulling BusinessName from a POST[] - is this a php var or it is a form html object tag? See why it's important to develop a naming convention for your php vars? Your code isn't easily maintainable and when you come back to this code in 3 months you won't recall either (I'm guessing, you might!).

Since this is page 2 I'm guessing it's being called from your php script that was called from your main form post? Is this correct? In other words, your page 2 script was called from your 'dispatcher' php script correct?

If page 2 is called from the 'dispatcher' script then you need to make sure you are sending BusinessName as an argument otherwise you won't be able to pick up its value. This is the big reason I first mentioned to use global php vars - this wouldn't happen if you had set BusinessName value in the post method called script. Once you set it in that script you can use it anywhere - but let's continue on where you are for now.

I know this may seem a bit complex but it's not really. You just need to understand the structure of a php form page and how php grabs the html object tag values (checkboxes, text boxes, etc.).

How about using a little debugging like this (below your first line) $BusinessName = $_POST['BusinessName'];


print 'BusinessName '.$BusinessName."<br>";
exit;

Run your code and see what the value is if anything - if its blank then your not grabbing the post data and you'll want to look at how your passing it from the calling script which I think is the key question at this point.

Don't give up!!! We'll get it.

RalphF
GoldRushWebHosting.com

Last edited by rfresh; Jun 16th, 2007 at 22:28.
Reply With Quote
  #10 (permalink)  
Old Jun 16th, 2007, 22:35
Junior Member
Join Date: Jun 2006
Location: usa
Age: 29
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Inserting Session into the database

I guess, i had not post all my code really,

Well, this is what i display with the print BusinessName on page2 because really, i am not selectin anything on page2, i am inserting values into the table name message. i just need the BusinessName value that come from page1 through a session, But then it is not giving me any value

i will post my full value for page2
PHP: Select all

 
<?php
session_start
();
$_Session['BusinessName']; 
 
?>
<html>
<head>
 
<title>Send Message</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
 
 <body  bgcolor="#E5E5E5">
<?php
$host 
"localhost";
$username "_localho";
$password "a";
$database "t";
 
$db mysql_connect($host$username$password) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());
 
  
$from addslashes($_POST['from']);
  
$statusaddslashes($_POST['status']);
  
$subjectaddslashes($_POST['subject']);
  
$messageaddslashes($_POST['message']);
  
$BusinessName$_SESSION['BusinessName'];
if(isset(
$_SESSION['BusinessName'])){
$query "INSERT INTO  `messages` (`BusinessName`,`date`,`from`,`status`,`subject`,`message`)
VALUES ('"
.$_SESSION['BusinessName']."','".$date."','".$from."', '".$status."','".$subject."',
'"
.$message."')";
$result mysql_query($query);
echo 
mysql_error();
if(
$result)
         {
echo 
mysql_affected_rows()." .Your Message have been sent. We will get back to you. <br>";
          }
}
 
?>
 
<center>
 
 
<pre>_SESSION:<?php print_r($_SESSION); ?></pre>
<table width="200" border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
  <tr>
    <td valign="top">
 <?php include("includefiles/banner.php"); ?>
 <table width="778" border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td width="778" valign="top">
  <table width="778" border="0" cellpadding="0" cellspacing="0">
 
          <tr>
            <td valign="top" bgcolor="#f90102">   <?php
       
echo "<h1><font color='ffffff'><marquee>$BusinessName </marquee></font></h1>"?>
 
 
 
</td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td>
  <? echo'<form action="'$_SERVER['PHP_SELF'].'" method="post" >'?>
 
  <table width="744" border="0" cellpadding="0" cellspacing="0">
          <tr>
            <td width="534" valign="top"><table width="542" border="0" cellpadding="0" cellspacing="0">
              <tr>
                <td width="542" valign="top">                    <table width="542" border="1" cellpadding="0" cellspacing="0" bordercolor="#E5E5E5" bgcolor="#0033CC">
                          <tr>
                            <td width="60" height="40" ><div align="center"><font size="-2" face="Verdana, Arial, Helvetica, sans-serif"><font color="#FFFFFF">Buy Online </font></font></div></td>
                            <td width="47" ><div align="center"><font size="-2" face="Verdana, Arial, Helvetica, sans-serif"><font color="#FFFFFF">Photos</font></font></div></td>
                            <td width="31"><div align="center"><font size="-2" face="Verdana, Arial, Helvetica, sans-serif"><font color="#FFFFFF">Video</font></font></div></td>
                            <td width="39"><div align="center"><font size="-2" face="Verdana, Arial, Helvetica, sans-serif"><font color="#FFFFFF">Sound</font></font></div></td>
                            <td width="79"><font color="#FFFFFF" size="-2" face="Verdana, Arial, Helvetica, sans-serif">Send to friend</font></td>
                            <td width="84"><font color="#FFFFFF" size="-2" face="Verdana, Arial, Helvetica, sans-serif">Add to Favorite </font></td>
                            <td width="86"><font color="#FFFFFF" size="-2" face="Verdana, Arial, Helvetica, sans-serif">Join Mailing List </font></td>
                            <td width="92"><font color="#FFFFFF" size="-2" face="Verdana, Arial, Helvetica, sans-serif">Contact US </font></td>
                          </tr>
                  </table></td></tr>
              <tr>
                <td height="269" valign="top"><table width="534" border="0" cellpadding="0" cellspacing="0">
                  <tr>
                    <td width="425" valign="top"><table width="516" border="0" bgcolor="#FFFFFF">
                      <tr>
                        <td width="510" valign="top"> <p><font color="#970000">
                          </font>
                              </p><?
print 'BusinessName '.$BusinessName."<br>";
exit;
?>
                          <table width="390" border="1" bordercolor="#FFFFFF">
                            <tr>
                              <td width="92" bgcolor="#CCCCCC"><strong>From</strong></td>
                              <td width="282"><? echo'<input name="from"  type="text" size="60">'?></td>
                            </tr>
                            <tr>
                              <td bgcolor="#CCCCCC"><strong>Subject </strong></td>
                              <td><? echo'<input name="subject" type="subject"  size="60">'?></td>
                            </tr>
                            <tr>
                              <td bgcolor="#CCCCCC"><strong>Body</strong></td>
                              <td><p>
                                <? echo'<textarea name="message" cols="75" rows="10"></textarea>'?>
                              </p>
                                <p>&nbsp;</p></td>
                            </tr>
                            <tr>
                              <td>&nbsp;</td>
                              <td bgcolor="#EFEFEF"><div align="right">
                                <table width="200" border="0" bgcolor="#EFEFEF">
                                    <tr>
                                      <td></td>
                                      <td><? echo'<input type="submit" name="Submit" >'?></td>
                                    </tr>
                                                              </table>
                              </div></td>
                            </tr>
                            <tr>
                              <td>&nbsp;</td>
                              <td>&nbsp;</td>
                            </tr>
                          </table>
                          <p>
                            <div align="center">
                          </p>                          <p>&nbsp;</p>
                          <p>&nbsp;</p>
                          <p>&nbsp;                            </p></td>
              </tr>
                    </table>                      </td>
        </tr>
                </table>   
 
      </td>
              </tr>
</table>              
              </td>
            <td width="210" valign="top"><table width="210" border="1" cellpadding="0" cellspacing="0">
              <tr>
                <td width="200" valign="top">
 
</td>
              </tr>
            </table></td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td valign="top">
 
  <?php include("includefiles/banner.php"); ?>
  </td>
      </tr>
      <tr>
        <td></td>
      </tr>
    </table></td>
  </tr>
</table>
</center>
</body>
</html>
Reply With Quote
  #11 (permalink)  
Old Jun 16th, 2007, 23:27
Junior Member
Join Date: Jun 2006
Location: usa
Age: 29
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Inserting Session into the database

Well, i did something and it is working now, honestly, i dont know what i did but it works

thank u
Reply With Quote
  #12 (permalink)  
Old Jun 17th, 2007, 00:16
Junior Member
Join Date: Jun 2007
Location: Los Angeles
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts