Jump to content

Help with passing variable/query


thebigtuna

Recommended Posts

Alright new question. Right now I'm trying to get a form to work with some of the basic stuff I learned, but it just isn't happening. What I want it to do is display the users information, so they can make changes to it if they want(haven't gotten to that part yet). I want it to take the username(txtUserId) which I passed from the login page that directs here, use that to username to preform a query which brings up the rest of the corresponding information, and displays it in the form so they can change it. Its display nothing at which leads me to believe that its either not passing the username to the next page, the query is working like I want it to, or theres some other inane thing I don't understand yet.  Any help is appreciated :)

 

 

code for login:

 

<?php

 

session_start();

 

$errorMessage = '';

if (isset($_POST['txtUserId']) && isset($_POST['txtPassword'])) {

 

mysql_connect("localhost", "user", "pass") or die(mysql_error());

  mysql_select_db(tunasql) or die(mysql_error());

 

  $userId = $_POST['txtUserId'];

  $password = $_POST['txtPassword'];

 

  // check if the user id and password combination exist in database

  $sql = "SELECT username

          FROM test

          WHERE username = '$userId'

                AND password = '$password'";

 

  $result = mysql_query($sql)

            or die('Query failed. ' . mysql_error());

 

  if (mysql_num_rows($result) == 1) {

      // the user id and password match,

   

      $_SESSION['db_is_logged_in'] = true;

 

      exit;

  } else {

      $errorMessage = 'Sorry, wrong user id / password';

  }

  mysql_close();

  }

?>

 

<html>

<head>

<title>Login</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

 

 

 

</head>

 

<body>

<?php

if ($errorMessage != '') {

?>

<p align="center"><strong><font color="#990000"><?php echo $errorMessage; ?></font></strong></p>

<?php

}

?>

<form method="post" action ="main.php" name="login" id="login">

 

<fieldset>

<legend>Login</legend>

<table width="400" align="center" cellpadding="2" cellspacing="2">

<tr>

<td width="150">Username:</td>

<td><input name="txtUserId" type="text" id="txtUserId"></td>

</tr>

<tr>

<td width="150">Password:</td>

<td><input name="txtPassword" type="password" id="txtPassword"></td>

</tr>

<tr>

<td width="150"> </td>

<td><input type="submit" name="btnLogin" value="Login"></td>

</tr>

</table>

</fieldset>

 

 

 

</form>

</body>

</html>

 

 

 

 

 

 

 

 

Code for 2nd page:

 

<?php

 

session_start();

 

// is user logged in or not??

if (!isset($_SESSION['db_is_logged_in'])

    || $_SESSION['db_is_logged_in'] !== true) {

 

    // not logged in, move to login page

    header('Location: login.php');

    exit;

}

 

$_POST['txtUserId'] = $userid;

     

mysql_connect("localhost", "user", "pass") or die(mysql_error());

mysql_select_db(db) or die(mysql_error());

 

 

 

$sql = "SELECT username, email, name, zip, city, state, country, adress, phone

          FROM test

          WHERE username = 'userid'";

 

$result = mysql_query($sql)

            or die('Query failed. ' . mysql_error());

           

 

mysql_close();

 

?>

 

<!--  -->

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

"http://www.w3.org/TR/xhtml2/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

 

  <title>Form Demo</title>

 

  </head>

 

  <body>

 

 

}

?>

<form id="main"  method="post" action ="<? $_SERVER['PHP_SELF']?>">

 

 

<fieldset>

<legend>Update Information</legend>

<table width="100%" border="0">

<tr>

    <td>Your Email:</td>

    <td><input type="text" name="email" size="32" value="<?php echo $email;?>"

    /></td>

 

</tr>

<tr>

    <td>Your Full Name:</td>

    <td><input type="text" name="name" size="32" value="<?php echo $name;?>" /></td>

</tr>

 

<tr>

    <td>Your ZIP Code:</td>

    <td><input type="text" name="zip" size="5" value="<?php echo $zip;?>"/></td>

</tr>

<tr>

    <td>Your City:</td>

    <td><input type="text" name="city" size="32" value="<?php echo $city;?>" /></td>

</tr>

<tr>

    <td>State/Province:</td>

    <td><input type="text" name="state" size="32" value="<?php echo $state;?>" /></td>

</tr>

<tr>

    <td>Country:</td>

    <td><input type="text" name="country" size="32" value="<?php echo $country;?>" /></td>

</tr>

<tr>

    <td>Address:</td>

    <td><input type="text" name="address" size="32" value="<?php echo $address;?>" /></td>

</tr>

<tr>

    <td>Phone:</td>

    <td><input type="text" name="phone" size="32" value="<?php echo $phone;?>" /></td>

</tr>

 

<tr>

    <td><input type="submit" value="Update" name="updateb" /></td>

</tr>

</table>

</fieldset>

 

<a href="logout.php>logout</a>

 

</form>

 

 

</body>

</html>

 

 

Link to comment
https://forums.phpfreaks.com/topic/57652-help-with-passing-variablequery/
Share on other sites

Where did you define $userid?

 

I can see where you did..

 

<?php

$_POST['txtUserId'] = $userid;

?>

 

Which likely needs to be

 

<?php

$userid = $_POST['txtUserId'];

?>

 

There may be other reasons but you didn't wrap that in code tags so I am too lazy to look more closely.

Yeah, tried that, didnt work.

 

 

Also, I didnt see an option to edit, but in the query on the second page: I do have it as

WHERE username = '$userid'";

 

not:

WHERE username = 'userid'";

 

I was screwing around with things trying to get it to work and forgot to readd the $ before I copied and pasted it in here.

do it more simply use

session_start();

$_session['test'] = $_POST['txtUserId'];

 

then just use $_session['test'] where you need the data will hold it long as u want it to

remeber on other pages u need to initalise sessions by

session_start();

before you can return it value.

 

Waz

 

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.