473,466 Members | 1,394 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Quick MySQL insert quesiton

I have the following code:

$username_sqlsafe = addslashes($data['username']);
$email_sqlsafe = addslashes($data['email']);
$name_sqlsafe = addslashes($data['name']);
$location_sqlsafe = addslashes($data['location']);
$password_md5 = md5($data['password']);
$remoteaddr = $_SERVER['REMOTE_ADDR'];

// do insert
$sql = "INSERT INTO users (username,password,email,registered,
name,location,last_ip)
VALUES ('$username_sqlsafe','$password_md5','$email_sqlsa fe',NOW(),
'$name_sqlsafe','$location_sqlsafe','$remoteaddr') ";

$query = mysql_query($sql,$_db);
// end

The username field has a UNIQUE index.

Problem: when the username exists in the database $query comes out false.
But the PHP documentation says this shouldn't be the case, it should return
true and then mysql_affected_rows() will return 0. And yes, this code works
when the username isn't in the database. I'm just confused, I doubt the
documentation is wrong, but I don't see what I could be doing if it works
fine when the username isn't in the db.

Thanks.
Jul 16 '05 #1
3 5004
Xizor wrote:
I have the following code:

$username_sqlsafe = addslashes($data['username']);
$email_sqlsafe = addslashes($data['email']);
$name_sqlsafe = addslashes($data['name']);
$location_sqlsafe = addslashes($data['location']);
$password_md5 = md5($data['password']);
$remoteaddr = $_SERVER['REMOTE_ADDR'];

// do insert
$sql = "INSERT INTO users (username,password,email,registered,
name,location,last_ip)
VALUES ('$username_sqlsafe','$password_md5','$email_sqlsa fe',NOW(),
'$name_sqlsafe','$location_sqlsafe','$remoteaddr') ";

$query = mysql_query($sql,$_db);
// end

The username field has a UNIQUE index.

Problem: when the username exists in the database $query comes out false.
But the PHP documentation says this shouldn't be the case, it should
return true and then mysql_affected_rows() will return 0. And yes, this
code works when the username isn't in the database. I'm just confused, I
doubt the documentation is wrong, but I don't see what I could be doing if
it works fine when the username isn't in the db.

Thanks.


If the username exists, then the query will *fail*.

Anyway, you should be checking if the user exists first...
Jul 16 '05 #2
On Sat, 09 Aug 2003 20:28:13 GMT, "Xizor" <no**@nope.com> wrote:
I have the following code:

$username_sqlsafe = addslashes($data['username']);
$email_sqlsafe = addslashes($data['email']);
$name_sqlsafe = addslashes($data['name']);
$location_sqlsafe = addslashes($data['location']);
$password_md5 = md5($data['password']);
$remoteaddr = $_SERVER['REMOTE_ADDR'];

// do insert
$sql = "INSERT INTO users (username,password,email,registered,
name,location,last_ip)
VALUES ('$username_sqlsafe','$password_md5','$email_sqlsa fe',NOW(),
'$name_sqlsafe','$location_sqlsafe','$remoteaddr' )";

$query = mysql_query($sql,$_db);
// end

The username field has a UNIQUE index.

Problem: when the username exists in the database $query comes out false.
This is correct. The query will fail with a key violation. Look at
mysql_error() and mysql_errno(). Find the error code for unique key violation,
then when you do your error checking:

if ($!query) { // an error occurred
if (mysql_errno() == the unique key violation code) {
// handle 'user already exists' condition
}
else
{
// handle UNEXPECTED error
}
}
But the PHP documentation says this shouldn't be the case, it should return
true and then mysql_affected_rows() will return 0.


No, that's for UPDATEs.

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 16 '05 #3
On Sat, 09 Aug 2003 21:32:09 +0000, matty <ma*******@askmenoquestions.co.uk>
wrote:
The username field has a UNIQUE index.


If the username exists, then the query will *fail*.

Anyway, you should be checking if the user exists first...


No you shouldn't, you should let the database do that for you, else you have a
race condition.

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Lenz Grimmer | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, MySQL 4.0.14, a new version of the popular Open Source/Free Software Database, has been released. It is now available in source and binary...
4
by: Ian | last post by:
Hi there, Maybe this is a strange quesiton but maybe someone can advise. I have created a database and it is getting larger and larger, I wonder if there any tools when you can actually...
1
by: Saqib Ali | last post by:
I have created 2 tables in my MySQL database. A_TAB and B_TAB. They have auto-incrementing integer primary keys respectively named A_ID & B_ID. When I created B_TAB, I declared a field named A_ID...
34
by: Karam Chand | last post by:
Hello I have been working with Access and MySQL for pretty long time. Very simple and able to perform their jobs. I dont need to start a flame anymore :) I have to work with PGSQL for my...
1
by: jlee | last post by:
I'm pretty much a newbie on mysql, and I need some help. I am running mysql Ver 12.22 Distrib 4.0.24, for portbld-freebsd5.4 (i386) on a server hosting an active website. The site's developer...
3
by: John Meyer | last post by:
Okay, I have a database listing various meetings. The meetings have a day of the week and a time (Say Sunday at 8:00). Now, given a set of these meetings, how would I order them so that they...
6
Atli
by: Atli | last post by:
This is an easy to digest 12 step guide on basics of using MySQL. It's a great refresher for those who need it and it work's great for first time MySQL users. Anyone should be able to get...
1
ssnaik84
by: ssnaik84 | last post by:
Hi Guys, Last year I got a chance to work with R&D team, which was working on DB scripts conversion.. Though there is migration tool available, it converts only tables and constraints.. Rest of...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.