Jump to content

Can't insert Session Username into database


msebar

Recommended Posts

Came a long way with the code since Friday and have another issue.

I can echo the session username on my pages but not into the insert command to the database. I need this so when a user logs in only their data will be seen. Here is the code pages.

 

Here is the page code this does echo the username

Logged in as <?php echo "$username"; ?>

<?php
// Start session

session_start() ;

$username = $_SESSION['username'];

// Include required functions file

require_once('include/db/functions.inc.php') ;

// Check login status ... if not logged in, redirect to login screen

if (check_login_status() == false) {

		redirect('login.php') ;
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Add A New Tank(WAD)</title>
<style type="text/css"></style>
</head>

<body>
<p align="center"><a href="../test/index.php">Home</a> | <a href="../test/register.php">Register</a> | <a href="../test/login.php">Login</a> | <a href="../test/tank.php">Add Tank</a> | <a href="../test/fish.php">Add Fish</a> | <a href="../test/plants.php">Add Plants</a> | <a href="../test/water-test.php">Add Water Test</a> | <a href="../test/include/login/logout.inc.php">Logout</a></p></p>
<p>Logged in as  <?php 
   
   echo "$username";
   ?> </p>

<table width="810" border="2" align="center">
    <tr>
    <td>
  
      
  <table width="800" border="0" align="center" cellpadding="0" cellspacing="0">
    <tr>
      <td align="center" bgcolor="#FFFFFF" scope="col"><h2><b>Your Tanks(WAD)</b></h2></td>
    </tr>
          
    <form action="/test/include/tank/tank.inc.php" method="post" name="tank" id="tank">
          
      <table border="2" align="center" cellpadding="0">
        <tr>
          <td><div align="left"><b>Tank Name: </b> </div></td>
          <td><div align="left">
              <input type="text" name="tankname" size="25" />
          </div></td>
        </tr>
        <tr>
          <td><div align="left"><b>Date Filled With Water: </b> </div></td>
          <td><div align="left">
              <input type="text" name="date" size="25" />
          </div></td>
        </tr>
        <tr>
          <td><div align="left"><b>Length: </b> </div></td>
          <td><div align="left">
              <input type="text" name="length" size="25" />
          </div></td>
        </tr>
        <tr>
          <td><div align="left"><b>Depth: </b> </div></td>
          <td><div align="left">
              <input type="text" name="depth" size="25" />
          </div></td>
        </tr>
        <tr>
          <td><div align="left"><b>Height: </b> </div></td>
          <td><div align="left">
              <input type="text" name="height" size="25" />
          </div></td>
        </tr>
        <tr>
          <td><div align="left"><b>Volume: </b> </div></td>
          <td><div align="left">
              <input type="text" name="volume" size="25" />
          </div></td>
        </tr>
        <tr>
          <td><div align="left"><b>Type of Tank: </b> </div></td>
          <td><div align="left">
              <input type="text" name="type" size="25" />
          </div></td>
        </tr>
          <tr>
          <td></div></td></tr>
         
        
        <tr>
          <td><div align="left"><b>Notes: </b> </div></td>
          <td><div align="left">
              <p>
                <textarea name="notes" cols="50" rows="10"></textarea>
              </p>
            </div></td>
        </tr>
        <tr>
          <th colspan="2"><p>
              <input type="submit" value="Add New Tank" />
            </p></th>
        </tr>
      </table>
    </form>
      
    
    <tr>
      <td align="center" valign="top" bgcolor="#FFFFFF"><div align="center"><font size="2"> © 2014 <a href="http://www.pctechtime.com">PC TECH TIME</a> </font> </div></td>
    </tr>
  </table>
    <tr>
    <td></td></td></tr>
  
    <tr>
    <td></tr></td></tr>
  
</table>
</body>
</html>

This is tank.inc.php which works except for the username being inserted into the database. I did try removing the mysqli_close($con); but that didn't help. I did have it working when I removed the mysqli_close($con); but then I logged out and then back in and it stopped?

<?php
 
include_once "../../../test/include/db/db.inc.php";


  // escape data and set variables
 	$tankname = mysqli_real_escape_string($con, $_POST['tankname']);
	$date = mysqli_real_escape_string($con, $_POST['date']);
	$length = mysqli_real_escape_string($con, $_POST['length']);			  
	$depth = mysqli_real_escape_string($con, $_POST['depth']);
  	$height = mysqli_real_escape_string($con, $_POST['height']);
	$volume = mysqli_real_escape_string($con, $_POST['volume']);
	$type = mysqli_real_escape_string($con, $_POST['type']);
	$notes = mysqli_real_escape_string($con, $_POST['notes']);	
	
	
	$username = $_SESSION['username'];
	
 
  //  # setup SQL statement
$sql="INSERT INTO tank (tankname, username, date, length, depth, height, volume, type, notes)
VALUES ('$tankname', '$username', '$date', '$length', '$depth', '$height', '$volume', '$type', '$notes')"; 		
      
        
if (!mysqli_query($con,$sql)) {
  die('Error: ' . mysqli_error($con));
}
echo 'New Tank Added ';
mysqli_close($con);
?>

add the session_start() on top of the second page, telling php that a session is used, also turn php error on.

<?php

ini_set('display_startup_errors', 1);

ini_set('display_errors', 1);

error_reporting(-1);

session_start();

include_once "../../../test/include/db/db.inc.php";

// rest of your script

Wow I think I need a break. Be learning PHP on and off for the last 5 years and decided to dig in deep this time.  I think I will take the rest of the night off and start tomorrow or Monday. Just trying to absorb as much as I can in short time. BTW your correct it worked. I spent 2 hours on this as I didn't want to ask the question. Had no choice started to get frustrated. Thanks again you guys are always there for me.

I am getting so sensitive seeing somebody to use a directory path(s) something like yours "../../../"  You should really avoid them, instead use an absolute one.

PS: Do all values you want to insert to database are strings? 

Archived

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

×
×
  • 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.