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

Join 105,772 PHP Programmers for FREE! Ask your question and get quick answers from experts. There are 1,272 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Data insertion into empty column

2 Pages V  1 2 >  
Reply to this topicStart new topic

Data insertion into empty column, login screen

msvp4u
post 23 Jul, 2008 - 07:29 AM
Post #1


New D.I.C Head

*
Joined: 15 Jul, 2008
Posts: 30


My Contributions


my table looks like this


mysql> select * from loginuser;
+-------+---------------+----------+---------+-----------+-----------+
| usrid | fname | lname | usrname | pass | confpass |
+-------+---------------+----------+---------+-----------+-----------+
| 122 | vijay | ganesan | | vganesan | vganesan |
| 125 | selva | kumar | | skumar | skumar |
| 126 | rangith | fernando | | rfernando | rfernando |
| 119 | madhusudhanan | kannan | | mkannan | mkannan |
+-------+---------------+----------+---------+-----------+-----------+
4 rows in set (0.00 sec)


i want to give username as the following query
CODE

$sql="select usrid, concat(left(fname,1),lname) as username from loginuser order by fname;";
$result=mysql_query($sql)or die(mysql_error());


and i want to use the above command and use the password column of pass to login
how could i do it?
User is offlineProfile CardPM

Go to the top of the page


MitkOK
post 23 Jul, 2008 - 07:56 AM
Post #2


D.I.C Regular

Group Icon
Joined: 9 Aug, 2007
Posts: 267



Thanked 9 times

Dream Kudos: 225
My Contributions


Basically you want to get the password for every user with the query which gets uid and the name ?
User is offlineProfile CardPM

Go to the top of the page

msvp4u
post 23 Jul, 2008 - 08:00 AM
Post #3


New D.I.C Head

*
Joined: 15 Jul, 2008
Posts: 30


My Contributions


the login name should be mkannan
password thats not a problem. the problem is with writing the query for username

User is offlineProfile CardPM

Go to the top of the page

msvp4u
post 23 Jul, 2008 - 08:07 AM
Post #4


New D.I.C Head

*
Joined: 15 Jul, 2008
Posts: 30


My Contributions


QUOTE(msvp4u @ 23 Jul, 2008 - 08:00 AM) *

the login name should be mkannan
password thats not a problem. the problem is with writing the query for username


i want the query to look something like this

i want to validate the username and also the password

CODE

$sql="SELECT * FROM $tbl_name WHERE usrname ='$username' and pass='$pass'";
$result=mysql_query($sql)or die(mysql_error());
User is offlineProfile CardPM

Go to the top of the page

msvp4u
post 23 Jul, 2008 - 08:43 AM
Post #5


New D.I.C Head

*
Joined: 15 Jul, 2008
Posts: 30


My Contributions


the php part looks like this
i need the sql part which troubles me
CODE

<?php
$host="localhost"; // Host name
$username=""; // Mysql username
$userpassword=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="loginuser"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$userpassword")or die("Cannot connect to the Data Base");
mysql_select_db("$db_name")or die("Cannot select DB");

// username and password sent from form
$loginname=$_POST['loginname'];
$password=$_POST['password'];

// To protect MySQL injection
$loginname = stripslashes($loginname);
$password = stripslashes($password);
$loginname = mysql_real_escape_string($loginname);
$password = mysql_real_escape_string($password);
//echo "$loginname";
//echo "$password";
$sql="SELECT * FROM $tbl_name WHERE usrname ='$username' and pass='$pass'";
$result=mysql_query($sql)or die(mysql_error());
//echo "$result";
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
//echo "$count";
// If result matched $loginname and $password, table row must be 1 row

if($count==1){

// Register $loginname, $password and redirect to file "login_success.php"
session_register("txtLoginName");
session_register("txtPassWord");
header("location:./ff.php");
User is offlineProfile CardPM

Go to the top of the page

msvp4u
post 23 Jul, 2008 - 01:37 PM
Post #6


New D.I.C Head

*
Joined: 15 Jul, 2008
Posts: 30


My Contributions


in the above table i want to update the usrname field with the following query

CODE

$sql="select concat(left(fname,1),lname) as username from loginuser;";


and then
use that usrname part as username for logging in

User is offlineProfile CardPM

Go to the top of the page

JBrace1990
post 23 Jul, 2008 - 01:41 PM
Post #7


D.I.C Regular

Group Icon
Joined: 9 Mar, 2008
Posts: 293



Thanked 10 times

Dream Kudos: 350
My Contributions


php

$sql = mysql_query("SELECT COUNT(*) AS total,username FROM loginuser WHERE username = '$username' AND password = '$password' GROUP BY username,id ORDER BY id, LIMIT 1")or die(mysql_error());
$row = mysql_fetch_array($sql);
if($row['total'] == 1){
//login
}
else{
echo "not logged in";
}
User is offlineProfile CardPM

Go to the top of the page

msvp4u
post 23 Jul, 2008 - 01:50 PM
Post #8


New D.I.C Head

*
Joined: 15 Jul, 2008
Posts: 30


My Contributions


There is an error message displayed


You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 1' at line 1
User is offlineProfile CardPM

Go to the top of the page

msvp4u
post 23 Jul, 2008 - 02:01 PM
Post #9


New D.I.C Head

*
Joined: 15 Jul, 2008
Posts: 30


My Contributions


the below is my db table
CODE

mysql> select * from loginuser;+-------+---------------+----------+---------+-----------+-----------+
| usrid | fname                   | lname        | usrname      | pass      | confpass  |
+-------+-------------------------+----------+-------------+-----------+-----------+
|   122 | vijay                          | ganesan  |                | vganesan  | vganesan  |
|   127 | sathish                      | durai        |                | sdurai        | sdurai    |
|   125 | selva                         | kumar      |                | skumar     | skumar    |
|   126 | rangith                       | fernando  |                | rfernando  | rfernando |
|   119 | madhusudhanan       | kannan   |                | mkannan    | mkannan   |
+-------+---------------+----------+---------+-----------+-----------+


and my php code looks like this

CODE

<?php
$host="localhost"; // Host name
$username=""; // Mysql username
$userpassword=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="userlogin"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$userpassword")or die("Cannot connect to the Data Base");
mysql_select_db("$db_name")or die("Cannot select DB");

// username and password sent from form
$loginname=$_POST['txtLoginName'];
$password=$_POST['txtPassWord'];

// To protect MySQL injection
$loginname = stripslashes($loginname);
$password = stripslashes($password);
$enpassword = stripslashes($enpassword);
$loginname = mysql_real_escape_string($loginname);
$password = mysql_real_escape_string($password);
$enpassword = mysql_real_escape_string($enpassword);

$sql = mysql_query("SELECT COUNT(*) AS total,username FROM loginuser WHERE username = '$username' AND password = '$password' GROUP BY username,id ORDER BY id, LIMIT 1")or die(mysql_error());  
$row = mysql_fetch_array($sql);  
if($row['total'] == 1){  

//login  
header("location:./login_success.php");
}  
else{  
echo "Wrong Username or Password";  
}  
?>

User is offlineProfile CardPM

Go to the top of the page

mocker
post 23 Jul, 2008 - 02:21 PM
Post #10


D.I.C Head

**
Joined: 14 Oct, 2007
Posts: 160



Thanked 4 times
My Contributions


If you use JBrace's example, the code is :
$sql = mysql_query("SELECT COUNT(*) AS total,username FROM loginuser WHERE username = '$username' AND password = '$password' GROUP BY username")or die(mysql_error());

The rest of that sql query was extraneous and gives errors.

Personally I would use:
$sql = mysql_query("SELECT usrid FROM loginuser WHERE username = '$username' AND password = '$password'")or die(mysql_error());
if(mysql_num_rows($sql) == 1){

GROUP BY's tend to start making queries slow, though in this case it probably wouldn't really be noticeable.
User is offlineProfile CardPM

Go to the top of the page

msvp4u
post 23 Jul, 2008 - 02:44 PM
Post #11


New D.I.C Head

*
Joined: 15 Jul, 2008
Posts: 30


My Contributions


sorry mocker
the query u said isnt working
User is offlineProfile CardPM

Go to the top of the page

JBrace1990
post 23 Jul, 2008 - 03:02 PM
Post #12


D.I.C Regular

Group Icon
Joined: 9 Mar, 2008
Posts: 293



Thanked 10 times

Dream Kudos: 350
My Contributions


QUOTE(mocker @ 23 Jul, 2008 - 02:21 PM) *

If you use JBrace's example, the code is :
$sql = mysql_query("SELECT COUNT(*) AS total,username FROM loginuser WHERE username = '$username' AND password = '$password' GROUP BY username")or die(mysql_error());

The rest of that sql query was extraneous and gives errors.

Personally I would use:
$sql = mysql_query("SELECT usrid FROM loginuser WHERE username = '$username' AND password = '$password'")or die(mysql_error());
if(mysql_num_rows($sql) == 1){

GROUP BY's tend to start making queries slow, though in this case it probably wouldn't really be noticeable.


COUNT is faster then num_rows almost always
User is offlineProfile CardPM

Go to the top of the page

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 8/21/08 04:10PM

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