Jump to content

Dummies Problem - Session Variable Insert


mdclermont
Go to solution Solved by mdclermont,

Recommended Posts

Hello,

 

I am working the PHP & MySQL for Dummies book. A lot of the material has gone fine or I've figured out what the problem was. However, I'm trying to insert a row into the MySQL table Login each time the user logs in. The login itself goes fine, but no row is added to that table.

 

  • I recently updated my Dreamhost php version to 5.3.
  • The Login table has two fields: loginName loginTime
  • I receive no errors of any kind.
  • I am able to add a row to the table in phpMyAdmin using INSERT INTO with the user's name and NOW()
  • Line 33: I've tried some syntax things like adding periods at each end of $_SESSION or making it '$_SESSION['logname']' etc. I also tried substituting the post variable.

This is what I believe to be the applicable part of the file (line 30 and following I think):

session_start();                                          #8
switch (@$_POST['Button'])                                #9
{
  case "Log in":                                         #11
    include("xxxxx");                                 #12
    $cxn=mysqli_connect($host,$user,$password,$dbname)
             or die("Query died: connect".mysqli_error ($cxn));             #14
    $sql="SELECT loginName FROM Member
              WHERE loginName='$_POST[fusername]'";
    $result=mysqli_query($cxn,$sql)
                or die("Query died: fusername: ".mysqli_error($cxn));
    $num=mysqli_num_rows($result);                     #19
    if($num > 0)  //login name was found                 #20

    {
	  $sql="SELECT loginName FROM Member
              WHERE loginName='$_POST[fusername]'
              AND password=('$_POST[fpassword]')";//took out md5 before (
      $result2=mysqli_query($cxn,$sql)
                   or die("Query died: fpassword");      #26
      $num2=mysqli_num_rows($result2);                 #27
      if($num2 > 0)  //password matches                  #28
      {$_SESSION['auth']="yes";                         #30
        $_SESSION['logname']=$_POST['fusername'];      #31
        $sql="INSERT INTO Login (loginName,loginTime)
                VALUES ('$_SESSION[logname]',NOW())"; #changed $_SESSION[logname] to $_POST[fusername]
        $result=mysqli_query($cxn,$sql)
                   or die("Query died: insert");         #35
        header("Location: SecretPage.php");              #36
      }
      else  // password does not match                   #38

Thanks,

Matt

Link to comment
Share on other sites

I was worried that maybe I wasn't properly storing the variable, but I think I was able to confirm it was stored by echoing. It outputs the login name on the next page when I use the following:

<?php
  session_start();
echo "Secret Page<br>";
echo "try 1.3<br>";
echo $_SESSION['logname'];
?>

Thanks,

Matt

Link to comment
Share on other sites

  • Solution

Got it...two issues. One was I had jacked up the syntax trying different things. The biggest issue, and hopefully one I will remember to check earlier next time, was that the database name in the *.inc file was wrong. :-\  Thanks for those that considered the issue.

 

Just in case it could be helpful for someone else in the future, here is what I ended up with:

 if($num2 > 0)  //password matches                  #28
      {$_SESSION['auth']="yes";                         #30
        $_SESSION['logname']=$_POST['fusername'];      #31
        $sql="INSERT INTO Login (loginName,loginTime)
                VALUES('$_SESSION[logname]',NOW())"; #changed $_SESSION[logname] to $_POST[fusername]
        $result=mysqli_query($cxn,$sql)
                   or die("Query died: insert");         #35
        header("Location: SecretPage.php");              #36
      }
      else  // password does not match                   #38

Matt

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.