Problems with code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
BlueFlame
Forum Commoner
Posts: 33
Joined: Wed Oct 12, 2005 5:21 am

Problems with code

Post by BlueFlame »

this code works pefectly fine on my localhost but when i upload it every thing seems to go wrong. It seems to have a lot of problems with this line of code "mysql_fetch_array ($result, MYSQL_NUM);". Can any one help?
And i have changed the database details.

Code: Select all

<?php 
session_start();
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Admin Login</title>
<link rel="stylesheet" type="text/css" href="style.css" media="all" title="StyleSheet"/>
</head>
<body>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="0" bordercolor="#999999">
  <tr>
    <td><table width="400" border="1" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td><div align="center"><img src="images/banner.jpg"></div></td>
      </tr>
      <tr>
        <td height="145">
		  <br><p align="center">
<?
if($_POST['submit']){
require_once('mysql_connect.php');
$username = $_POST['username'];
$password = md5($_POST['password']);
$query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$result = @mysql_query ($query);
$row = mysql_fetch_array ($result, MYSQL_NUM);
if ($row) {	
		session_register("username");
		session_register("user_id");
		$_SESSION['username'] = $row[1];
		$_SESSION['user_id'] = $row[0];
		
		echo "<font face=\"verdana\" size=\"1\" color=\"#000000\">Login Complete<META HTTP-EQUIV=\"refresh\" CONTENT=\"2; URL=menu.php\"></font>";
	} else {
	echo "<font face=\"verdana\" size=\"1\" color=\"#000000\">Login Failed<META HTTP-EQUIV=\"refresh\" CONTENT=\"2; URL=index.php\"></font>";
	}
} ?>
		  </p>
		  <br><form name="form1" method="post" action="index.php">
          <div align="center"><span class="style1">Username:</span><br>
                <input name="username" type="text" class="style1" id="username">
                <br>
                <span class="style1">Password:</span> <br>
            <input name="password" type="password" class="style1" id="password">
            <br>
            <br>
            <input name="submit" type="submit" class="style1" id="submit" value="Login">
            <br>
            <br>
          </div>
		</form></td>
      </tr>
	  <tr>
	  <td align="center"><?php include('copyright.php'); ?></td>
	  </tr>
    </table></td>
  </tr>
</table>
</body>
</html>
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

First of all, you need to use a function like mysql_real_escape_string() on your post data before putting it into a query


Second of all take the @ off the @mysql_query ($query);


Thirdly, add

Code: Select all

or die(mysql_error())
to the end of the query, post any error messages you are getting
BlueFlame
Forum Commoner
Posts: 33
Joined: Wed Oct 12, 2005 5:21 am

Post by BlueFlame »

I got this after I typed in the Correct username and password.
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/alan2011/public_html/phppro/admincp/index.php on line 27
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

Post lines 26, 27.
BlueFlame
Forum Commoner
Posts: 33
Joined: Wed Oct 12, 2005 5:21 am

Post by BlueFlame »

Code: Select all

$result = mysql_query ($query);
$row = mysql_fetch_array ($result, MYSQL_NUM);
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

a few issues:
  • relying on the submit button can bite you in the butt. Some browsers always send it, some only when the button is clicked. Look for a field that's always sent, or better yet, check $_SERVER['REQUEST_METHOD']
  • session_register() is not to be used when you are working with $_SESSION, ever.
  • The error you are getting is referring to the query string being bad in some way shape or form. If you follow what jshpro2 said about mysql_error() and the @ that will lead you to what the error actually is.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

With my host, if i do not use backticks around the query it WILL FAIL.

Code: Select all

select * from `tablename` where `blah`='blah'
note the difference between ` and '
Post Reply