473,385 Members | 1,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

connecting to MySQL using PHP need help !

George Lft
[php]
<?php //beginning tag
$host = "localhost";
$dbuser = "root";
$dbpass = "";
$connection = mysql_connect($host,$dbuser,$dbpass);

$dbname = "tutorial";
$db = mysql_select_db($dbname);

//grab data from form
$name = $_POST['Name'];
$pass = $_POST['pwd'];
$email = $_POST['email'];
$re_email = $_POST['reemail'];

//if else (else if)
if($name == false || $pass == false || $re_email == false || email == false) {
echo "Please enter all required fields. "; }

if($email != $re_email) {
echo " Email do not match. ";
}else{
$connection = mysql_connect($host,$dbuser,$dbpass);

$dbname = "tutorial";
$db = mysql_select_db($dbname);
$sql = INSERT INTO user (Username, Password, Email)
VALUES ($name, $pass, $email)
$result = mysql_query($sql);
echo " Thank you for registering .";
}
?>
[/php]



Hi PHP pro, I can't connect to the database. when i insert data , the page loads as normal without inserting the data . Can anyone help out with this if the scripts have error or am i missing something ?
Jan 15 '08 #1
3 1228
Markus
6,050 Expert 4TB
[php]
<?php //beginning tag
$host = "localhost";
$dbuser = "root";
$dbpass = "";
$connection = mysql_connect($host,$dbuser,$dbpass);

$dbname = "tutorial";
$db = mysql_select_db($dbname);

//grab data from form
$name = $_POST['Name'];
$pass = $_POST['pwd'];
$email = $_POST['email'];
$re_email = $_POST['reemail'];

//if else (else if)
if($name == false || $pass == false || $re_email == false || email == false) {
echo "Please enter all required fields. "; }

if($email != $re_email) {
echo " Email do not match. ";
}else{
$connection = mysql_connect($host,$dbuser,$dbpass);

$dbname = "tutorial";
$db = mysql_select_db($dbname);
$sql = INSERT INTO user (Username, Password, Email)
VALUES ($name, $pass, $email)
$result = mysql_query($sql);
echo " Thank you for registering .";
}
?>
[/php]
You're missing a few things and there's a few things i would still change about your connection/insertion code.

We'll start of with the connection!
[php]
<?php //beginning tag
$host = "localhost";
$dbuser = "root";
$dbpass = "";
mysql_connect($host, $dbuser, $dbpass) or die("Error: ".mysql_error());
[/php]
See i took out the $connection variable - it's not really needed; we'll just explicitly connect up here. Because we're doing it this way we add a or die which, if the connection doesn't work, it'll show an error explaining why! (it'll also kill any code that comes after this point). Now we shall select the database. We will do it exactly the same way as connecting to mysql.
[php]
$dbname = "tutorial";
mysql_select_db($dbname) or die("Error: ".mysql_error());
[/php]
All fine and dandy?

Now we'll handle the form POST. There's a nasty little thing called mysql injection (tutorial) i suggest you read up on and always prevent! I'll put it into action for you:
[php]
$name = mysql_real_escape_string($_POST['Name']); // all escaped!
$pass = mysql_real_escape_string($_POST['pwd']);
$email = mysql_real_escape_string($_POST['email']);
$re_email = mysql_real_escape_string($_POST['reemail']);
[/php]
Safely escaped. Now that chunk of if/else statements, there's a few things i'd change about it. (You need to do some more validation but that's another long story for another day :] )
[php]
if($name == false || $pass == false || $re_email == false || email == false) {
echo "Please enter all required fields. "; }

else if($email != $re_email) {
echo "Emails do not match. ";
} else { //everything was good - let's insert!
[/php]
I made the second IF and ELSE IF because, if the first statement returns false but the second IF returns TRUE it's still going to go onto the ELSE statement. Inserting time!
[php]
$sql = "INSERT INTO user (Username, Password, Email)
VALUES
('$name', '$pass', '$email')";
[/php]
This, itself, had a few syntax problems: you always need double / single quotes surrounding variables! and on the VALUES wrap them in single quotes too! - I think you should put curly brackets around the variables in the VALUES aswell.. someone else can clear that up.
[php]
$result = mysql_query($sql);
if($result){
echo "Thankyou for registering - Everything was successful!";
} else {
echo "There was a problem with your registration...";
}
[/php]
I changed it to a simple if/else statement. The IF checks whether the $result variable returns TRUE and if so, the insertion is complete and the user is notified. ELSE there is a problem and user is notified.

Some things you should consider:
Sanitising user input!(for that ugly IF statement you use to check if things are FALSE) - It's paramount you do so or some little script kiddy could work his little magic and give you headaches!
A decent mysql tutorial

And the code in full:
[php]
<?php //beginning tag
$host = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "tutorial";
mysql_connect($host, $dbuser, $dbpass) or die("Error: ".mysql_error());
mysql_select_db($dbname) or die("Error: ".mysql_error());

// grab form data
$name = mysql_real_escape_string($_POST['Name']); // all escaped!
$pass = mysql_real_escape_string($_POST['pwd']);
$email = mysql_real_escape_string($_POST['email']);
$re_email = mysql_real_escape_string($_POST['reemail']);

if($name == false || $pass == false || $re_email == false || email == false) {
echo "Please enter all required fields. "; }

else if($email != $re_email) {
echo "Emails do not match. ";
} else { //everything was good - let's insert!
$sql = "INSERT INTO user (Username, Password, Email)
VALUES
('$name', '$pass', '$email')";
if($result){
echo "Thankyou for registering - Everything was successful!";
} else {
echo "There was a problem with your registration...";
}
} // close first ELSE
[/php]
Jan 15 '08 #2
Awesome, Awesome. RESOLVED .


actually i'm more impressed by your kindness than the codes you engineered..



Thanks man, Best wishes!
Jan 15 '08 #3
Markus
6,050 Expert 4TB
Awesome, Awesome. RESOLVED .


actually i'm more impressed by your kindness than the codes you engineered..



Thanks man, Best wishes!
Well, makes my day it does!

Anytime, my friend.

:)
Jan 15 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Google Mike | last post by:
After a lot of thought and research, and playing with FreeTDS and InlineTDS, as well as various ODBC connections, I have determined that the fastest and cheapest way to get up and going with PHP on...
2
by: news | last post by:
We currently have our mySQL server on the same box as the Apache server. For security and load balancing, we're going to be moving the mySQL server to another box. We're already using a single...
3
by: Jeremy Dillinger | last post by:
I am trying to design a program that will use data from a MySQL database. Currently all the data is being used with PHP scripts from a website. I am also trying to build a software solution that...
3
by: Paradox Synthesist | last post by:
hi, i am a newbie to asp.net and have started learning mainly from websites. i have a question which IS very silly and trivial. ...
8
by: Ananthu | last post by:
Hi I have done all the codings part for connecting mysql with java in eclipse environment. Coding Part: import java.sql.Connection; import java.sql.DriverManager; public class...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.