Passing variables between pages

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
skaber
Forum Newbie
Posts: 2
Joined: Fri Feb 06, 2004 4:53 am

Passing variables between pages

Post by skaber »

hello !
I created an admin.php page wich is used by any admin to addusers to a mysql database. The admin.php itself seems to be nice but i have no clue how to get the fields added into the database. The first part of the page shows the LOGIN table and the second part is a form to add users.
What i do here is i refer the form action to test.php that adds a new row to the database.

My problem is i dunno how to get the "name" and "password" fields passed throught test.php so $name and $password will be defined on test.php

Is this the right way to do it or could i do something shorter without test.php ?

Thanks,
Francis
-----admin.php-----
<?php require_once('Connections/PretimeDB.php'); ?>
<?php
mysql_select_db($database_PretimeDB, $PretimeDB);
$query_pretime = "SELECT * FROM logins";
$pretime = mysql_query($query_pretime, $PretimeDB) or die(mysql_error());
$row_pretime = mysql_fetch_assoc($pretime);
$totalRows_pretime = mysql_num_rows($pretime);
?>

<body>
<?php do { ?>
<tr>
<td><div align="center"><font face="Verdana, Arial, Helvetica, sans-serif"><?php echo $row_pretime['username']; ?></font></div></td>
<td><div align="center"><font face="Verdana, Arial, Helvetica, sans-serif"><?php echo $row_pretime['password']; ?></font></div></td>
</tr>
<?php } while ($row_pretime = mysql_fetch_assoc($pretime)); ?>
</table>
<p>&nbsp;</p>
<form name="form1" method="post" action="test.php">
<table width="75%" border="0" align="center">
<tr>
<td colspan="2"><div align="center"><img src="img_03.gif" width="400" height="20"></div></td>
</tr>
<tr>
<td><div align="center"><font color="#0000A0" face="Verdana, Arial, Helvetica, sans-serif">USERNAME</font></div></td>
<td><div align="center"><font color="#0000A0" face="Verdana, Arial, Helvetica, sans-serif">PASSWORD</font></div></td>
</tr>
<tr>
<td><div align="center">
<input name="name" type="text" id="name">
</div></td>
<td><div align="center">
<input name="password" type="text" id="password">
</div></td>
</tr>
</table>

<div align="center">
<input type="submit" name="submit" value="submit">
<br>
</div>
</form>
<p>&nbsp; </p>
</body>
</html>
<?php
mysql_free_result($pretime);
?>



-----test.php-----
<?php require_once('Connections/PretimeDB.php'); ?>
<?php
if ( isset( $_GET['name'] ) )
if ( isset( $_GET['password'] ) )
?>
<?php
mysql_select_db($database_PretimeDB, $PretimeDB);
$query = "INSERT INTO logins (username,password) VALUES ('$name','$password')";
$result = mysql_query("INSERT INTO logins (username,password) VALUES ('$name','$password')") or die("Query failed : " . mysql_error());
?>
Straterra
Forum Regular
Posts: 527
Joined: Mon Nov 24, 2003 8:46 am
Location: Indianapolis, Indiana
Contact:

Re: Passing variables between pages

Post by Straterra »

skaber wrote:hello !
I created an admin.php page wich is used by any admin to addusers to a mysql database. The admin.php itself seems to be nice but i have no clue how to get the fields added into the database. The first part of the page shows the LOGIN table and the second part is a form to add users.
What i do here is i refer the form action to test.php that adds a new row to the database.

My problem is i dunno how to get the "name" and "password" fields passed throught test.php so $name and $password will be defined on test.php

Is this the right way to do it or could i do something shorter without test.php ?

Thanks,
Francis
-----admin.php-----

Code: Select all

<?php require_once('Connections/PretimeDB.php'); ?>
<?php
mysql_select_db($database_PretimeDB, $PretimeDB);
$query_pretime = "SELECT * FROM logins";
$pretime = mysql_query($query_pretime, $PretimeDB) or die(mysql_error());
$row_pretime = mysql_fetch_assoc($pretime);
$totalRows_pretime = mysql_num_rows($pretime);
?>

<body>
  <?php do { ?>
  <tr> 
    <td><div align="center"><font face="Verdana, Arial, Helvetica, sans-serif"><?php echo $row_pretime['username']; ?></font></div></td>
    <td><div align="center"><font face="Verdana, Arial, Helvetica, sans-serif"><?php echo $row_pretime['password']; ?></font></div></td>
  </tr>
  <?php } while ($row_pretime = mysql_fetch_assoc($pretime)); ?>
</table>
<p>&nbsp;</p>
<form name="form1" method="post" action="test.php">
  <table width="75%" border="0" align="center">
    <tr> 
      <td colspan="2"><div align="center"><img src="img_03.gif" width="400" height="20"></div></td>
    </tr>
    <tr> 
      <td><div align="center"><font color="#0000A0" face="Verdana, Arial, Helvetica, sans-serif">USERNAME</font></div></td>
      <td><div align="center"><font color="#0000A0" face="Verdana, Arial, Helvetica, sans-serif">PASSWORD</font></div></td>
    </tr>
    <tr> 
      <td><div align="center"> 
          <input name="name" type="text" id="name">
        </div></td>
      <td><div align="center"> 
          <input name="password" type="text" id="password">
        </div></td>
    </tr>
  </table>

  <div align="center">
    <input type="submit" name="submit" value="submit">
    <br>
  </div>
</form>
<p>&nbsp; </p>
</body>
</html>
<?php
mysql_free_result($pretime);
?>

-----test.php-----

Code: Select all

<?php require_once('Connections/PretimeDB.php'); ?>
<?php 
if ( isset( $_GET['name'] ) )
if ( isset( $_GET['password'] ) ) 
?>
<?php
mysql_select_db($database_PretimeDB, $PretimeDB);
$query = "INSERT INTO logins (username,password) VALUES ('$name','$password')";
$result = mysql_query("INSERT INTO logins (username,password) VALUES ('$name','$password')") or die("Query failed : " . mysql_error());
?>
Just put it all in PHP syntax...
Straterra
Forum Regular
Posts: 527
Joined: Mon Nov 24, 2003 8:46 am
Location: Indianapolis, Indiana
Contact:

Post by Straterra »

admin.php

Code: Select all

<?php
require_once('Connections/PretimeDB.php'); 
mysql_select_db($database_PretimeDB, $PretimeDB); 
$query_pretime = "SELECT * FROM logins"; 
$pretime = mysql_query($query_pretime, $PretimeDB) or die(mysql_error()); 
$row_pretime = mysql_fetch_assoc($pretime); 
$totalRows_pretime = mysql_num_rows($pretime); 
?> 

<body>  
  <tr> 
    <td><div align="center"><font face="Verdana, Arial, Helvetica, sans-serif"><?php echo $row_pretime['username']; ?></font></div></td> 
    <td><div align="center"><font face="Verdana, Arial, Helvetica, sans-serif"><?php echo $row_pretime['password']; ?></font></div></td> 
  </tr> 
  <?php while ($row_pretime = mysql_fetch_assoc($pretime)) { ?> 
</table> 
<p> </p> 
<form name="form1" method="post" action="test.php"> 
  <table width="75%" border="0" align="center"> 
    <tr> 
      <td colspan="2"><div align="center"><img src="img_03.gif" width="400" height="20"></div></td> 
    </tr> 
    <tr> 
      <td><div align="center"><font color="#0000A0" face="Verdana, Arial, Helvetica, sans-serif">USERNAME</font></div></td> 
      <td><div align="center"><font color="#0000A0" face="Verdana, Arial, Helvetica, sans-serif">PASSWORD</font></div></td> 
    </tr> 
    <tr> 
      <td><div align="center"> 
          <input name="name" type="text" id="name"> 
        </div></td> 
      <td><div align="center"> 
          <input name="password" type="text" id="password"> 
        </div></td> 
    </tr> 
  </table> 

  <div align="center"> 
    <input type="submit" name="submit" value="submit"> 
    <br> 
  </div> 
</form> 
<p>  </p> 
</body> 
</html> 
<?php
} 
mysql_free_result($pretime); 
?>
This above code was such badly written, that I have NO CLUE what you are wanting to do with the while statement..Now, onto the test.php

test.php

Code: Select all

<?php
require_once('Connections/PretimeDB.php'); 
if ( isset($_GET['name']) == true and isset($_GET['password']) == true ) {
mysql_select_db($database_PretimeDB, $PretimeDB); 
$query = "INSERT INTO logins (username,password) VALUES ('$name','$password')"; 
$result = mysql_query("INSERT INTO logins (username,password) VALUES ('$name','$password')") or die("Query failed : " . mysql_error()); 
?>
And why are you adding the exact same things twice??
I would STRONGLY recommend you rewrite the entire script.
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

he is not adding the same thing twice. He has it the same thing typed out twice, but he isn't adding it twice...

Code: Select all

<?php 
require_once('Connections/PretimeDB.php'); 
if ( isset($_GET['name']) == true and isset($_GET['password']) == true ) { 
mysql_select_db($database_PretimeDB, $PretimeDB); 
$query = "INSERT INTO logins (username,password) VALUES ('$name','$password')"; 
$result = mysql_query("INSERT INTO logins (username,password) VALUES ('$name','$password')") or die("Query failed : " . mysql_error()); 
?>
could just be:

Code: Select all

<?php 
require_once('Connections/PretimeDB.php'); 
if ( isset($_GET['name']) == true and isset($_GET['password']) == true ) { 
mysql_select_db($database_PretimeDB, $PretimeDB); 
$query = "INSERT INTO logins (username,password) VALUES ('$name','$password')"; 
$result = mysql_query($query) or die("Query failed : " . mysql_error()); 
?>
skaber
Forum Newbie
Posts: 2
Joined: Fri Feb 06, 2004 4:53 am

still no good

Post by skaber »

yea, you were right, i had typed in twice the same command. i modifed the admin.php to set <form action="test.php"> and i changed the test.php page has fallow:

<?php
require_once('Connections/PretimeDB.php');
if ( isset($_GET['name']) == true and isset($_GET['password']) == true ) {
mysql_select_db($database_PretimeDB, $PretimeDB);
$query = "INSERT INTO logins (username,password) VALUES ('$name','$password')";
$result = mysql_query($query) or die("Query failed : " . mysql_error());
echo "$name $password";
}
?>

i added the echo "$name $password" to see if the command would be executed. Seems like all what is between the brackets isnt getting executed.
any idea ?
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

$_POST?
Post Reply