/    Sign up×
Community /Pin to ProfileBookmark

Connecting to SQL DB

hi guys this place has been a real help for some of the problems i have encountered so i have decided to throw another problem your way

i am trying to write to an sql database but this does not update

i am using the following code, oviously replacing the passwords and user name etc

[code=php]$pass_id = md5($pass_id);
$db = mysql_connect(“hostname”, “username”,”password”);
mysql_select_db(“databaseName”,$db);

$sql = “INSERT INTO cust_profile
(name,gender,DOB,address01,address02,city,country,
post_code,email_add,phone_number,
user_id,password,card_no,card_type,expiry_date)
VALUES
(‘$name’,’$gender’,’$age’,’$add01′,’$add02′,’$city’,’$country’,
‘$post_code’,’$email’,’$phone_no’,
‘$user_id’,’$pass_id’,’$card_no’,’$card_type’,’$expiry_date’)”;

$result = mysql_query($sql);

if (!$result)
{
echo “Registration Fails..Someone has used the userID”;
exit() ;
}
echo “Registration Successful! <br /> Enjoy Your Shopping”;
exit();
}[/code]

obvously there is some thing going on becasue the data base is empty but because of the validation in this code it allwasy says please choose another id

any help would be much appreciated

thanks

scott

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@themartyJun 07.2006 — try this:

[code=php]$pass_id = md5($pass_id);
$db = mysql_connect("hostname", "username","password");
mysql_select_db("databaseName",$db);

$sql = "INSERT INTO cust_profile (
name,gender,DOB,address01,address02,city,country,
post_code,email_add,phone_number,
user_id,password,card_no,card_type,expiry_date
) VALUES (
'".$name."',
'".$gender."',
'".$age."',
'".$add01."',
'".$add02."',
'".$city."',
'".$country."',
'".$post_code."',
'".$email."',
'".$phone_no."',
'".$user_id."',
'".$pass_id."',
'".$card_no."',
'".$card_type."',
'".$expiry_date."'
)";
if (!$result = mysql_query($sql))
{
if (mysql_errno() == 1062)
{
echo "Registration Fails..Someone has used the userID";
}
else
{
echo "Registration fails due to database error<br>n";
echo $query."<br>n".mysql_error();
}
}
else
{
echo "Registration Successful! <br /> Enjoy Your Shopping";
}[/code]


You wrongly assume that if it fails it is because the id already exists. If the query fails it could also be because of another error. This you can check with mysql_errno() (btw, i think 1062 is duplicate entry, but you'll have to look it up to be sure). Also note that if it fails for another reason, that is the way to get some more information about the error. Of course when you go into production you should not output that to the screen (it's not something you want to show to visitors), but rather write to a log.

And once again: don't use so many exits! they will really cause you problems in the future.

Ah, and another things. Where do those variables come from that you try to write to the database? Are you sure it shouldn't be $_POST['name'] instead of $name, etc.?
Copy linkTweet thisAlerts:
@sinkyauthorJun 08.2006 — thanks thats a great help.. got in working now

Ty
×

Success!

Help @sinky spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...