Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 132,685 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,261 people online right now. Registration is fast and FREE... Join Now!




PhP + MYSQL

 
Reply to this topicStart new topic

PhP + MYSQL, Compareing problem

Clearman
post 31 May, 2008 - 02:52 PM
Post #1


New D.I.C Head

*
Joined: 13 Dec, 2007
Posts: 48


My Contributions


The two values in my database match up with the ones i use.
But the out come is the same the vars wont compare and match up.
CODE


//index.html

<!-- Test login program
index.html
-->

<html>
<head>
</head>
<body>



<center>
<form method = "POST" action = "login.php">
<hr>
UserName:<br>
<input type = "text" name = "UserName"><br>
Password:<br>
<input type = "password" name ="Password"><br>
<hr>
<input type="submit">

<center>
<body>
</html>


CODE

<!--  Test login program
login.php
-->




<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("users_info", $con);



$UserName = $_POST["UserName"];
$Password = $_POST["Password"];

$name = mysql_query("SELECT $UserName FROM User_Name"); //$UserName in data base matches
$pass = mysql_query("SELECT $Password FROM Pass_Word");//$Password in data base matches
if ($name == $UserName)
{
echo "name ok<br>";
if ($pass == $Password )
{

echo "ok done<br>";
}
}
else { echo " Something went wrong";}

?>




It should say "name ok" and "ok done" but sadly "Something went wrong" pops up.

I Tested this

CODE

$name = mysql_query("SELECT $UserName FROM User_Name"); //$UserName in data base matches
$pass = mysql_query("SELECT $Password FROM Pass_Word");//$Password in data base matches
if ($name == $name)
{
echo "name ok<br>";
if ($pass == $pass  )
{

echo "ok done<br>";
}
}
else { echo " Something went wrong";}

?>


and got the result so i think the problem is in the mysql code.

Thanks for any help
User is offlineProfile CardPM

Go to the top of the page

akozlik
post 31 May, 2008 - 04:16 PM
Post #2


D.I.C Addict

Group Icon
Joined: 25 Feb, 2008
Posts: 596



Thanked 22 times

Dream Kudos: 750
My Contributions


Your SQL query is wrong. You need to do
CODE

<?php

$sql = "select * from users_info where User_Name = '$UserName' and Pass_Word = '$Password'";
mysql_query($sql);
?>


That's assuming your table name is users_info, and the field names are User_Name and Pass_Word. I wrote a tutorial on authentication systems in the PHP Tutorials section of DIC. You can read it here.

This post has been edited by akozlik: 31 May, 2008 - 04:17 PM
User is offlineProfile CardPM

Go to the top of the page

JBrace1990
post 31 May, 2008 - 04:26 PM
Post #3


D.I.C Regular

Group Icon
Joined: 9 Mar, 2008
Posts: 474



Thanked 21 times

Dream Kudos: 350
My Contributions


to get the data from the database, you need to atore it in an array...

lookhere to see how to do it.
php

$pass = mysql_fetch_array($pass);
$name = mysql_fetch_array($name);


in your first code, all you do is select the datam you don't store it anywhere...

in the second code, the variable $name and $pass are not set, so they equal the same thing (and always will (also, all variables will ALWAYS equal themselves))

EDIT: Actually akozlik, his query is correct if he has 2 tables (which I assume is what he has)... you do not need a variable in the mysql_query()...

I do agree though, you should read a few tutorials on how to make a better login system...

This post has been edited by JBrace1990: 31 May, 2008 - 04:27 PM
User is offlineProfile CardPM

Go to the top of the page

Martyr2
post 31 May, 2008 - 04:31 PM
Post #4


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 5,062



Thanked 177 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


Actually akozlik is right JBrace, he is not pointing out the fact that a variable should be in there or not, but that the Original Poster actually is using the user's username in the place of a column when it should be in the place of the where clause. If done the way the original poster has done, the table would have to have columns like "Martyr2", "JBrace1990" etc which I am sure it doesn't.

smile.gif

This post has been edited by Martyr2: 31 May, 2008 - 04:31 PM
User is offlineProfile CardPM

Go to the top of the page

JBrace1990
post 31 May, 2008 - 04:33 PM
Post #5


D.I.C Regular

Group Icon
Joined: 9 Mar, 2008
Posts: 474



Thanked 21 times

Dream Kudos: 350
My Contributions


QUOTE(Martyr2 @ 31 May, 2008 - 05:31 PM) *

Actually akozlik is right JBrace, he is not pointing out the fact that a variable should be in there or not, but that the Original Poster actually is using the user's username in the place of a column when it should be in the place of the where clause. If done the way the original poster has done, the table would have to have columns like "Martyr2", "JBrace1990" etc which I am sure it doesn't.

smile.gif


by looking at his login script, I wouldn't be so sure it's not.....
User is offlineProfile CardPM

Go to the top of the page

akozlik
post 31 May, 2008 - 04:36 PM
Post #6


D.I.C Addict

Group Icon
Joined: 25 Feb, 2008
Posts: 596



Thanked 22 times

Dream Kudos: 750
My Contributions


JBrace: It doesn't look like he has two tables. He only selects his database with

CODE

mysql_select_db("users_info", $con);


That's definitely an odd way of doing a login system. Check out the tutorials and ask us if you need any more guidance. You should have one table that holds the user information, including usernames and password. I would definitely think about naming that one column password and not Pass_Word, but that's a matter of style.

Martyr2: Thanks for clearing that up


QUOTE(Martyr2 @ 31 May, 2008 - 05:31 PM) *

If done the way the original poster has done, the table would have to have columns like "Martyr2", "JBrace1990" etc which I am sure it doesn't.

smile.gif


How crazy would that be if he just randomly named his columns that and then you guys replied. That'd be a bit odd.
User is offlineProfile CardPM

Go to the top of the page

Clearman
post 31 May, 2008 - 05:14 PM
Post #7


New D.I.C Head

*
Joined: 13 Dec, 2007
Posts: 48


My Contributions


Thanks for your replies and i got the general Message .

This "hopefully" explains my database structure:
CODE

-- phpMyAdmin SQL Dump
-- version 2.11.6
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Jun 01, 2008 at 02:58 AM
-- Server version: 5.0.51
-- PHP Version: 5.2.6

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

--
-- Database: `users`
--

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

--
-- Table structure for table `users_info`
--

CREATE TABLE `users_info` (
  `id` int(30) NOT NULL auto_increment,
  `User_Name` char(15) NOT NULL,
  `Pass_Word` char(20) NOT NULL,
  `Age` int(2) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=49;

--
-- Dumping data for table `users_info`
--

INSERT INTO `users_info` (`id`, `User_Name`, `Pass_Word`, `Age`) VALUES
-- //I cut some out


(30, 'ciachyou', 'PassworHere', 192),
(31, 'ciachyou', 'PassworHere', 192),
(32, 'ciachyou', 'PassworHere', 192),
(33, 'ciachyou', 'PassworHere', 192),
(34, 'ciachyou', 'PassworHere', 192),
(35, 'ciachyou', 'PassworHere', 192),
(36, 'ciachyou', 'PassworHere', 192),
(37, 'ciachyou', 'PassworHere', 192),
(38, 'ciachyou', 'PassworHere', 192),
(39, 'ciachyou', 'PassworHere', 192),
(40, 'ciachyou', 'PassworHere', 192),
(41, 'ciachyou', 'PassworHere', 192),
(42, 'ciachyou', 'PassworHere', 192),
(43, 'ciachyou', 'PassworHere', 192),
(44, 'ciachyou', 'PassworHere', 192),
(45, 'ciachyou', 'PassworHere', 192),
(46, 'ciachyou', 'PassworHere', 192),
(47, 'ciachyou', 'PassworHere', 192),
(48, 'ciachyou', 'PassworHere', 192);


I used "Pass_word" because i read an introduction on MYSQL injections and changed that one piece of code or just to annoy other people it might have been 50/50 who knows ph34r.gif

At 2am i would love to see the names of your variables. wink2.gif

EDIT: Thanks for the links

This post has been edited by Clearman: 31 May, 2008 - 05:15 PM
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 07:11AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month