Jump to content

need help calling specific data from db


icedragoniii

Recommended Posts

Goal: When user logs in set a cookie that is his personal id from the database

 

How I am going about doing it: the user needs to input his username and I am trying to compare the inputted username with that of the one in the db and then take information just from that row (i.e. the personal id number assigned) and then save it as a cookie.

 

How am I failing?: I don't know that much PHP coding therefore I am quite limited in what i can do. Having issues assigning the cookie (because it needs to be a string and I don't know how to do that).

 

I am using Dream Weaver CS4 (and using some a lot of the code that it offers) and the code with // by it is what I added and am having issues with. I know it is really weird looking but I tried a ton of stuff and would be very grateful if you would help me. I do not mind if you wish for me to change my approach of how to go about doing my goal. I am still new with PHP.

 

<?php require_once('../Connections/users.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_users, $users);
$query_login = "SELECT * FROM people";
$login = mysql_query($query_login, $users) or die(mysql_error());
$row_login = mysql_fetch_assoc($login);
$totalRows_login = mysql_num_rows($login);
?>
<?php
// *** Validate request to login to this site.
if (!isset($_SESSION)) {
  session_start();
}

$loginFormAction = $_SERVER['PHP_SELF'];
if (isset($_GET['accesscheck'])) {
  $_SESSION['PrevUrl'] = $_GET['accesscheck'];
}

if (isset($_POST['username'])) {
  $loginUsername=$_POST['username'];
  $password=$_POST['password'];
  		//
  		//$post={echo $result};
  		//
  		// $userid = "SELECT username FROM people";
  		//$result2 = mysql_query($userid);
  $MM_fldUserAuthorization = "personid";
  $MM_redirectLoginSuccess = "../index.php";
  $MM_redirectLoginFailed = "login.php";
  $MM_redirecttoReferrer = true;
  mysql_select_db($database_users, $users);
       //	while($loginUsername = mysql_fetch_assoc($result2))
   //  $personid = "SELECT personid FROM people";
	   //  $result = mysql_query($personid);
       //$showcookie=print_r($result);
   //$showcookie=echo $_POST[$result];
   //setrawcookie("whouser",print_r($result));

  $LoginRS__query=sprintf("SELECT username, password, personid FROM people WHERE username=%s AND password=%s",
					  
  GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text")); 
  $LoginRS = mysql_query($LoginRS__query, $users) or die(mysql_error());
  $loginFoundUser = mysql_num_rows($LoginRS);
  if ($loginFoundUser) {
    
    $loginStrGroup  = mysql_result($LoginRS,0,'personid');
    
    //declare two session variables and assign them
    $_SESSION['MM_Username'] = $loginUsername;
    $_SESSION['MM_UserGroup'] = $loginStrGroup;	      

    if (isset($_SESSION['PrevUrl']) && true) {
      $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];	
    }
    header("Location: " . $MM_redirectLoginSuccess );
  }
  else {
    header("Location: ". $MM_redirectLoginFailed );
  }
}
?>



<form ACTION="<?php echo $loginFormAction; ?>" id="form1" name="form1" method="POST">
                      <ul>
							<p><center>                          		
								Username: <br />
					    <span id="sprytextfield6">
									<input type="text" name="username" class="text" />
									<span class="textfieldRequiredMsg">Required.</span></span>
                                                <br />
                                    Password: <br />
                  <span id="sprypassword1">
                                                  <input type="password" name="password" id="password" />
                                                  <span class="passwordRequiredMsg">Required.</span></span>
                                     </center>
       				      </ul>
						</p>
                                       <br /><center>
<span id="sprycheckbox1">
              						   	    <input type="checkbox" name="tac" value="agree" />
										<span class="checkboxRequiredMsg">Please Agree</span></span>By entering this website I agree that I have read and understand the <a href="tac2.html">Terms and Conditions</a> of using this website
			<br />
                                            <input type="submit" value="Submit" />
							    </center>
                            </p>
                          </form>

 

excuse some of the ugly layout, but it kinda works for me

Link to comment
Share on other sites

<?php

    $sql = "SELECT ID FROM table WHERE USERNAME='".$_REQUEST['username']."'";
    $result=mysql_query($sql);
    if(!$result){die(mysql_error());}
    $id = mysql_fetch_array($result);
    $value = "cookie value...";
    setcookie($id, $value);
?>

 

I forgot a semicolon on the first one. woops

Link to comment
Share on other sites

It says

WARNING: setcookie() expects parameter 2 to be a string.

 

I changed the first parameter because I want to be able to call it on other parts of the web page and then display the persons unique id. When I used it default it gave me the same error but with parameter 1 instead of 2

 

    $sql = "SELECT personid FROM people WHERE username='".$_REQUEST['username']."'";
    $result=mysql_query($sql);
    if(!$result){die(mysql_error());}
    $id = mysql_fetch_array($result);
    $value = "mycookie";
    setcookie($value, $id);

 

if i keep both of them strings then I can call them on other parts of the website. [setcookie ($value, "testing");] tested that.

 

my original goal was backwards... so your code will work perfectly if I just switch $id and $value however it still wants both of them to be strings...

 

GOAL:I want to call the cookie on other parts of the website and then use the unique id that i associate to the user name to do stuff.

Link to comment
Share on other sites

It says

WARNING: setcookie() expects parameter 2 to be a string.

 

I changed the first parameter because I want to be able to call it on other parts of the web page and then display the persons unique id. When I used it default it gave me the same error but with parameter 1 instead of 2

 

    $sql = "SELECT personid FROM people WHERE username='".$_REQUEST['username']."'";
    $result=mysql_query($sql);
    if(!$result){die(mysql_error());}
    $id = mysql_fetch_array($result);
    $value = "mycookie";
    setcookie($value, $id);

 

if i keep both of them strings then I can call them on other parts of the website. [setcookie ($value, "testing");] tested that.

 

my original goal was backwards... so your code will work perfectly if I just switch $id and $value however it still wants both of them to be strings...

 

GOAL:I want to call the cookie on other parts of the website and then use the unique id that i associate to the user name to do stuff.

 

$sql = "SELECT personid FROM people WHERE username='".$_REQUEST['username']."'";
    $result=mysql_query($sql);
    if(!$result){die(mysql_error());}
    $id = strval(mysql_fetch_array($result)); //strval() converts it to string
    $value = "mycookie";
    setcookie($value, $id);

Link to comment
Share on other sites

I added the strval() and it keeps outputting the same thing for multiple users which is the word "Array". I went step by step through the process

and $sql = "SELECT...

  outputs

        SELECT personid FROM people WHERE username= \'admin\'

 

username is admin as I inputted (this does change to whatever the user name is that was inputted)

 

next step $result=mysql... outputs

    Resource id #4

Which is the same for multiple accounts...then

$id outputs "Array"

 

those are all the string outputs

 

the way the table is set up, it has 4 columns and multiple rows

personid | username | password | access

    1            admin          admin        1

 

it isn't calling the personid correctly

Link to comment
Share on other sites

Try This:

$sql = "SELECT personid FROM people WHERE username='".$_REQUEST['username']."'";
    $result=mysql_query($sql);
    if(!$result){die(mysql_error());}
        $id = mysql_fetch_array($result);
        $id = "$id";
        $value = "mycookie";
        setcookie($value, $id);

Link to comment
Share on other sites

I was messing around with the code that Dream Weaver gave me and came out with this code

 

if (isset($_POST['username'])) {
  $loginUsername=$_POST['username'];
  $password=$_POST['password'];
  $MM_fldUserAuthorization = "access";
  $MM_redirectLoginSuccess = "../index.php";
  $MM_redirectLoginFailed = "../404.shtml";
  $MM_redirecttoReferrer = true;
  mysql_select_db($database_users, $users);
  	
  $LoginRS__query=sprintf("SELECT username, password, access, personid FROM people WHERE username=%s AND password=%s",// needed to add personid here to make sure it gets requested right here
  GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text")); 
   
  $LoginRS = mysql_query($LoginRS__query, $users) or die(mysql_error());
  $loginFoundUser = mysql_num_rows($LoginRS);

  if ($loginFoundUser) {
      
    $loginStrGroup  = mysql_result($LoginRS,0,'access');
   	$loginUsrId  = mysql_result($LoginRS,0,'personid');// This right here calls the personid out of the already requested row same as the previous one did for getting access
    //declare two session variables and assign them
    $_SESSION['MM_Username'] = $loginUsername;
    $_SESSION['MM_UserGroup'] = $loginStrGroup;	      

        $id = strval($loginUsrId);//made it into a string
        $value = "mycookie";//name of the cookie
        setcookie($value, $id);//set it and then success!

 

Since it was already requesting the access and was getting it correct on all the different accounts i just replicated it but used the person id instead. it sets the correct id for the user and i am able to call it on the rest of the website.

 

Thanks a lot The Letter E your code helped me quite a lot! without your help i probably will still be stuck making it a string and not knowing what a good majority of the code did.

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.