Jump to content

[SOLVED] Problem with php connecting to mysql?


tom.hill

Recommended Posts

Hi everyone!

 

As im sure you will be able to tell reading this post im very new to web development and have fallen at the first hurdle! Im trying to develop a website in php/html with a mysql db (so I guess im in the right place to ask for help!) I have tried to start by creating a login page although for some reason I cannot get it to work, I think the problem lies with my code connecting to my db but hopefully one of you guys will be able to point out where im going wrong?

 

Here’s what I have so far: (im developing and hosting locally)

 

login.html

 

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

<table border="0" width="225" align="center">

    <tr>

        <td width="219" bgcolor="#999999">

            <p align="center"><font color="white"><span style="font-size:12pt;"><b>Login</b></span></font></p>

        </td>

    </tr>

    <tr>

        <td width="219">

            <table border="0" width="220" align="center">

                <tr>

                    <td width="71"><span style="font-size:10pt;">Username:</span></td>

                    <td width="139"><input type="text" name="username"></td>

                </tr>

                <tr>

                    <td width="71"><span style="font-size:10pt;">Password:</span></td>

                    <td width="139"><input type="password" name="password"></td>

                </tr>

                <tr>

                    <td width="71"> </td>

                        <td width="139">

                            <p align="right"><input type="submit" name="submit" value="Submit"></p>

                        </td>

                </tr>

            </table>

        </td>

    </tr>

    <tr>

        <td width="219" bgcolor="#999999"><font color="white">Not Registered? </font><a href="register.html" target="_self"><font color="white">Register</font></a><font color="white"> </font><b><i><font color="white">Now!</font></i></b></td>

    </tr>

</table>

</form>

 

login.php

 

<?php

 

//Database Information

 

$dbhost = "localhost";

$dbname = "store";

$dbuser = "root";

$dbpass = "password";

 

//Connect to database

 

mysql_connect ( $dbhost, $dbuser, $dbpass) or die("Could not connect: ".mysql_error());

mysql_select_db($dbname) or die(mysql_error());

 

session_start();

$username = $_POST[‘username’];

$password = $_POST[‘password’];

 

$query = “select * from users where username=’$username’ and password=’$password’”;

 

$result = mysql_query($query);

 

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

$error = “Bad Login”;

    include “login.html”;

 

} else {

    $_SESSION[‘username’] = “$username”;

    include “memberspage.php”;

}

 

?>

 

memberspage.php

 

<html>

<body>

 

<?php

echo "hello";

?>

 

</body>

</html>

 

 

When I enter credentials in to login.html and click submit ie7 just shows a blank page with login.php in the address bar. This behavior is the same whether I enter credentials that have been manually entered into the users table of the store db or not.

 

Can anyone help??

 

# root and password are correct credentials for my db, I can login to myadmin using these

 

Thanks in advance.

Tom

 

Link to comment
Share on other sites

chane login.php to

 

<?php

//Database Information

$dbhost = "localhost";
$dbname = "store";
$dbuser = "root";
$dbpass = "password";

//Connect to database

mysql_connect ( $dbhost, $dbuser, $dbpass) or die("Could not connect: ".mysql_error());
mysql_select_db($dbname) or die(mysql_error());

session_start();
$username = $_POST[‘username’];
$password = $_POST[‘password’];

$query = "select * from `users` where `username`=$username and `password`=$password";

$result = mysql_query($query);

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

header('Location: login.html');

} else {
   $_SESSION[‘username’] = “$username”;
   header('Location: memberspage.php');
}

?>

 

edited the code

Link to comment
Share on other sites

No change im afraid, I wonder if this helps?

 

-- phpMyAdmin SQL Dump

-- version 2.11.3

-- http://www.phpmyadmin.net

--

-- Host: localhost

-- Generation Time: Dec 18, 2007 at 02:16 PM

-- Server version: 5.0.45

-- PHP Version: 5.2.1

 

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

 

--

-- Database: `store`

--

 

-- --------------------------------------------------------

 

--

-- Table structure for table `users`

--

 

CREATE TABLE IF NOT EXISTS `users` (

  `userid` int(11) NOT NULL auto_increment,

  `username` varchar(25) NOT NULL,

  `password` char(32) NOT NULL,

  `isdeleted` tinyint(1) NOT NULL default '0',

  PRIMARY KEY  (`userid`),

  UNIQUE KEY `username` (`username`)

) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

 

--

-- Dumping data for table `users`

--

 

INSERT INTO `users` (`userid`, `username`, `password`, `isdeleted`) VALUES

(1, 'tom', 'password', 0);

 

Link to comment
Share on other sites

Blank php pages are usually the result of fatal parse or fatal runtime errors.

 

Always check your web server log for errors. In this case you would have found -

 

Parse error: syntax error, unexpected T_STRING in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\login.php on line 19

 

When learning php, developing php code, or debugging php code, always turn on full error reporting in php.ini or a .htaccess file to get php to help you. Turning on full php error reporting in your code won't help with fatal parse errors because the code never runs to turn the error reporting on.

Link to comment
Share on other sites

Hey guys I started again and managed to get it working.

 

The problem seemed to be around this line

 

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

 

expanding this worked to:

 

$numrows = mysql_num_rows($result);

 

if ($numrows != 1){

 

worked! Bizarre but anyway working now, thanks again! im sure i'll be back! :)

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.