Jump to content

Non case-sensitive SQL query


EffakT

Recommended Posts

Hey, I am currently working on a website for school and I am doing the Log in system. I need the 'Name' to be non-case sensitive.

How would I go about doing this?

The code I have so far is:

<?php
session_start();
//error_reporting(0);
if (isset($_SESSION['user']))
{
	die(header("Location: index.php"));
}
else
{
	//echo "Session = ".$_SESSION['user']."<br/>";
	if (isset($_POST['submit']))
	{
		$username = $_POST['username'];
		$password = $_POST['password'];
		
		mysql_connect("localhost","root","");
		mysql_select_db("3.46");
		$sql = "SELECT * FROM members WHERE Name = '$username'";
		//echo $sql;
		//echo "Searching for: $username<br/>";
		$members = mysql_query($sql);
		$count = mysql_num_rows($members);
		
		if ($count == 0)
		{
			echo "<b><p style='color: red'>Unable to find member: $username</p></b>";
		}
		else
		{
			while($row=mysql_fetch_array($members))
			{
				$dbpass = $row['Md5'];
				$dbuser = $row['Name'];
				//echo "<br/>dbuser = ".$dbuser;
				//echo "<br/>username = ".$username;
			}
			if ($username != $dbuser || sha1($password) != $dbpass)
			{
				if (sha1($password) != $dbpass)
				{
					echo "Incorrect password<br/>";
				}
			}
			else
			{	
				$_SESSION['user'] = $dbuser;
				//echo "<br/>Session: ".$_SESSION['user']."<br/>";
				//header("Location: index.php");
			}
		}
	}
}
?>

What do I need to do the make the 'Name' non case-sensitive?

Link to comment
https://forums.phpfreaks.com/topic/277169-non-case-sensitive-sql-query/
Share on other sites

CREATE TABLE `members` (
 `ID` int(11) NOT NULL AUTO_INCREMENT,
 `Name` varchar(80) NOT NULL,
 `Md5` text NOT NULL,
 `Email` varchar(80) NOT NULL,
 `Admin` int(1) NOT NULL DEFAULT '0',
 PRIMARY KEY (`ID`),
 UNIQUE KEY `ID` (`ID`),
 UNIQUE KEY `Name` (`Name`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1

Arrgh simple fix but I didn't even realize

 

To fix it, I changed the statement that checks if user or pass is not the same as db to:

if (strtoupper($username) != strtoupper($dbuser) || sha1($password) != $dbpass)
			{
				if (sha1($password) != $dbpass)
				{
					echo "Incorrect password<br/>";
				}
				if (strtoupper($username) != strtoupper($dbuser))
				{
					echo "Incorrect Username";
				}
			}

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.