Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

Should be easy but where's the bug here?

Codes here !

Moderators: macek, egami, gesf

Should be easy but where's the bug here?

Postby infomorelos » Mon Jan 27, 2003 12:20 am

I have built a table benutzer with three columns: 'usuario', 'password' and 'redirect', all are 'varchar' and 'usuario' is PRIMARY KEY. Then I try to redirect users depending on the username/pasword-combination they supply.
However, after typing in the combination ad clicking on 'submit', I get this error message:

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/ahcehc/paginas/navega1.php on line 21

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/ahcehc/paginas/navega1.php on line 22

Warning: Cannot add header information - headers already sent by (output started at /home/ahcehc/paginas/navega1.php:21) in /home/ahcehc/paginas/navega1.php on line 23


See the actual code:
line 1 <?php
line 2 $db = 'ahcehc';
line 3 $dbuser = 'user';
line 4 $dbpass = 'myPassword';
line 5 $dbhost = 'localhost';
line 6 mysql_connect($dbhost,$dbuser,$dbpass) or die("could not connect");
line 7 mysql_select_db("$db") or die("could not open database");
line 8 if (isset($_POST["usuario"]))
line 9 {
line 10 $usario = $_POST["usuario"];
line 11 }
line 12
line 13 if (isset($_POST["password"]))
line 14 {
line 15 $usario = $_POST["password"];
line 16 }
line 17
line 18 if ( isset($_POST["Submit"]) )
line 19 {
line 20 $sql = "SELECT * FROM benutzer WHERE usuario='$usuario' AND password='$password'";
line 21 $result = mysql_query($sql, $connect);
line 22 $row = mysql_fetch_array($result);
line 23 header("Location: ".$row[redirect]);
line 24 exit;
line 25 }
line 26 else
line 27 {
line 28 ?>
line 29 <form name="form1" method="post" action="navega1.php">
<table width="100%" border="0" cellspacing="0" cellpadding="1">
<tr>
<td>
<input type="text" name="usuario">
</td>
</tr>
<tr>
<td><font size="2">Nombre de Usuario</font></td>
</tr>
<tr>
<td>
<input type="text" name="password">
</td>
</tr>
<tr>
<td><font size="2">Contraseña</font></td>
</tr>
<tr>
<td>
<input type="submit" name="Submit" value="Log-in">
</td>
</tr>
</table>
</form>
<?php
}
?>

Any ideas about what's wrong here?
infomorelos
New php-forum User
New php-forum User
 
Posts: 14
Joined: Mon Jan 27, 2003 12:10 am
Location: Cuernavaca, Mexico

Postby Joan Garnet » Mon Jan 27, 2003 4:29 am

Code: Select all
line 21 $result = mysql_query($sql, $connect);

No $connect variable was initialized yet :)

Code: Select all
line 23 header("Location: ".$row[redirect]);

Headers must be added on the very top of the page, so they are read before anything else.

That will be enough I guess.
Saludos!
;)
User avatar
Joan Garnet
Moderator
Moderator
 
Posts: 387
Joined: Sat Aug 03, 2002 2:56 am
Location: Mars

Page doesn't show up any more

Postby infomorelos » Mon Jan 27, 2003 2:10 pm

Thank you for your tip, it changed thing a little bit: The page doesn't show up any more, I'n getting redirected to the index file immediately (before the 'navega1.php' page shows up.
I don't know what to do with line 21, where and how do I initializa the $connect variable?
I have changed line 23 to the top now and the code reads:

line 1 <?php
line 2 header("Location: ".$row[redirect]);
line 3 $db = 'ahcehc';
line 4 $dbuser = 'user';
line 5 $dbpass = 'myPassword';
line 6 $dbhost = 'localhost';
line 7 mysql_connect($dbhost,$dbuser,$dbpass) or die("could not connect");
line 8 mysql_select_db("$db") or die("could not open database");
line 9 if (isset($_POST["usuario"]))
line 10 {
line 11 $usario = $_POST["usuario"];
line 12 }
line 13
line 14 if (isset($_POST["password"]))
line 15 {
line 16 $usario = $_POST["password"];
line 17 }
line 18
line 19 if ( isset($_POST["Submit"]) )
line 20 {
line 21 $sql = "SELECT * FROM benutzer WHERE usuario='$usuario' AND password='$password'";
line 22 $result = mysql_query($sql, $connect);
line 23 $row = mysql_fetch_array($result);
line 24 exit;
line 25 }
line 26 else
line 27 {
line 28 ?>
line 29 <form name="form1" method="post" action="navega1.php">
<table width="100%" border="0" cellspacing="0" cellpadding="1">
<tr>
<td>
<input type="text" name="usuario">
</td>
</tr>
<tr>
<td><font size="2">Nombre de Usuario</font></td>
</tr>
<tr>
<td>
<input type="text" name="password">
</td>
</tr>
<tr>
<td><font size="2">Contraseña</font></td>
</tr>
<tr>
<td>
<input type="submit" name="Submit" value="Log-in">
</td>
</tr>
</table>
</form>
<?php
}
?>

Maybe the problem is in the tables: all fields are 'NOT NULL'.
infomorelos
New php-forum User
New php-forum User
 
Posts: 14
Joined: Mon Jan 27, 2003 12:10 am
Location: Cuernavaca, Mexico

Postby Joan Garnet » Mon Jan 27, 2003 4:35 pm

Code: Select all
line 7 $conection = mysql_connect($dbhost,$dbuser,$dbpass) or die("could not connect");

About the redirection, what do you want exactly?? you can use javascript.
User avatar
Joan Garnet
Moderator
Moderator
 
Posts: 387
Joined: Sat Aug 03, 2002 2:56 am
Location: Mars

latest news!

Postby infomorelos » Tue Jan 28, 2003 12:58 am

Hi Joan,

I had written earlier in this thread that I need to redirect users from my index page to thei user-directory-index, depending on the username. I wouldn't know how to do that in Javascript.

In the meanwhile, I've solved the problem:
It was line 21 $result = mysql_query($sql, $connect);

since there is no connect variable defined, the line must be:
line 21 $result = mysql_query($sql);

Now it really rocks...

Thank you for your help!
infomorelos
New php-forum User
New php-forum User
 
Posts: 14
Joined: Mon Jan 27, 2003 12:10 am
Location: Cuernavaca, Mexico

Postby Joan Garnet » Tue Jan 28, 2003 2:53 am

using javascript is easy.
just echo the function caller.

Code: Select all
<body onload="
<?php
if(blah blah blah){
    echo "user_redirect()";
}else{
    echo "another_user_redirect()";
}
?>
">

This is an example... there many ways to achieve that.
User avatar
Joan Garnet
Moderator
Moderator
 
Posts: 387
Joined: Sat Aug 03, 2002 2:56 am
Location: Mars


Return to mySQL & php coding

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron