When ever i use this PHP Script it gives me an Error saying:
Warning: mysql_connect() [function.mysql-connect]: Unknown MySQL server host '$host' (11001) in C:\Program Files\xampp\htdocs\website\newuser.php on line 49
Unable to connect!
I have XAMPP installed which basically sets up the webserver for you. To acess the server i type "localhost" in the url of a browser and it works.
Please help, Here is the script:
<html>
<head>
<basefont face="Arial">
</head>
<body>
<?php
if (!isset($_POST['submit'])) {
// form not submitted
?>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
Username: <input type="text" name="user">
Password: <input type="text" name="pass"> <br>
<input type="text" name="email1">
@<input type="text" name="email2"> <br>
First Name: <input type="text" name="name1">
Last Name: <input type="text" name="name2"><br>
Country: <input type="text" name="country">
<input type="submit" name="submit">
</form>
<?php
}
else {
// form submitted
// set server access variables
$host = "localhost";
$user = "user";
$pass = "password";
$db = "wintersword";
// get form input
// check to make sure it's all there
// escape input values for greater safety
$user = empty($_POST['user']) ? die ("Please Enter A Username") : mysql_escape_string($_POST['user']);
$pass = empty($_POST['pass']) ? die ("Please Enter A Password") : mysql_escape_string($_POST['pass']);
$email1 = empty($_POST['email1']) ? die ("Please Enter Your Email Address") : mysql_escape_string($_POST['email1']);
$email2 = empty($_POST['email2']) ? die ("Please Enter Your Email Address") : mysql_escape_string($_POST['email2']);
$name1 = empty($_POST['name1']) ? die ("Please Enter Your First Name") : mysql_escape_string($_POST['name1']);
$name2 = empty($_POST['name2']) ? die ("Please Enter Your Last Name") : mysql_escape_string($_POST['name2']);
$country = empty($_POST['country']) ? die ("Please Enter What Country You Are In") : mysql_escape_string($_POST['country']);
// open connection
$connection = mysql_connect('$host', '$user', '$pass') or die ("Unable to connect!");
// select database
mysql_select_db($db) or die ("Unable to select database!");
// create query
$query = "INSERT INTO users (user, pass, email1, email2, name1, name2, country) VALUES ('$user', '$pass', 'email1', '$email2', '$name1', '$name2', '$country',)";
// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
// print message with ID of inserted record
echo "New record inserted with ID ".mysql_insert_id();
// close connection
mysql_close($connection);
}
?>
</body>
</html>
Whats wrong with this code?
Moderator: General Moderators
Right here
should beDarksevear wrote:Code: Select all
// open connection $connection = mysql_connect('$host', '$user', '$pass') or die ("Unable to connect!");
Code: Select all
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");Code: Select all
$connection = mysql_connect("$host", "$user", "$pass") or die ("Unable to connect!");-
Darksevear
- Forum Newbie
- Posts: 11
- Joined: Sat May 13, 2006 10:38 pm
It has another error
I have fixed the old problem here is the new code: (see lower)
But now it says:
Error in query: INSERT INTO users (user1, pass1, email1, email2, name1, name2, country) VALUES ('gkhf', '', 'fkfkh', 'dfkhk', 'fkfkgfk', 'fkfk', 'fkfkfk',). 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 ')' at line 1
Please help
- Cheers, Daniel
<html>
<head>
<basefont face="Arial">
</head>
<body>
<?php
if (!isset($_POST['submit'])) {
// form not submitted
?>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
Username: <input type="text" name="user">
Password: <input type="text" name="pass"> <br>
<input type="text" name="email1">
@<input type="text" name="email2"> <br>
First Name: <input type="text" name="name1">
Last Name: <input type="text" name="name2"><br>
Country: <input type="text" name="country">
<input type="submit" name="submit">
</form>
<?php
}
else {
// form submitted
// set server access variables
$host = 'localhost';
$user = "admin";
$pass = "login";
$db = "wintersword";
// get form input
// check to make sure it's all there
// escape input values for greater safety
$user1 = empty($_POST['user']) ? die ("Please Enter A Username") : mysql_escape_string($_POST['user']);
$pass2 = empty($_POST['pass']) ? die ("Please Enter A Password") : mysql_escape_string($_POST['pass']);
$email1 = empty($_POST['email1']) ? die ("Please Enter Your Email Address") : mysql_escape_string($_POST['email1']);
$email2 = empty($_POST['email2']) ? die ("Please Enter Your Email Address") : mysql_escape_string($_POST['email2']);
$name1 = empty($_POST['name1']) ? die ("Please Enter Your First Name") : mysql_escape_string($_POST['name1']);
$name2 = empty($_POST['name2']) ? die ("Please Enter Your Last Name") : mysql_escape_string($_POST['name2']);
$country = empty($_POST['country']) ? die ("Please Enter What Country You Are In") : mysql_escape_string($_POST['country']);
// open connection
$connection = mysql_connect("$host", "$user", "$pass") or die ("Unable to connect!");
// select database
mysql_select_db($db) or die ("Unable to select database!");
// create query
$query = "INSERT INTO users (user1, pass1, email1, email2, name1, name2, country) VALUES ('$user1', '$pass1', '$email1', '$email2', '$name1', '$name2', '$country',)";
// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
// print message with ID of inserted record
echo "New record inserted with ID ".mysql_insert_id();
// close connection
mysql_close($connection);
}
?>
</body>
</html>
But now it says:
Error in query: INSERT INTO users (user1, pass1, email1, email2, name1, name2, country) VALUES ('gkhf', '', 'fkfkh', 'dfkhk', 'fkfkgfk', 'fkfk', 'fkfkfk',). 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 ')' at line 1
Please help
- Cheers, Daniel
<html>
<head>
<basefont face="Arial">
</head>
<body>
<?php
if (!isset($_POST['submit'])) {
// form not submitted
?>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
Username: <input type="text" name="user">
Password: <input type="text" name="pass"> <br>
<input type="text" name="email1">
@<input type="text" name="email2"> <br>
First Name: <input type="text" name="name1">
Last Name: <input type="text" name="name2"><br>
Country: <input type="text" name="country">
<input type="submit" name="submit">
</form>
<?php
}
else {
// form submitted
// set server access variables
$host = 'localhost';
$user = "admin";
$pass = "login";
$db = "wintersword";
// get form input
// check to make sure it's all there
// escape input values for greater safety
$user1 = empty($_POST['user']) ? die ("Please Enter A Username") : mysql_escape_string($_POST['user']);
$pass2 = empty($_POST['pass']) ? die ("Please Enter A Password") : mysql_escape_string($_POST['pass']);
$email1 = empty($_POST['email1']) ? die ("Please Enter Your Email Address") : mysql_escape_string($_POST['email1']);
$email2 = empty($_POST['email2']) ? die ("Please Enter Your Email Address") : mysql_escape_string($_POST['email2']);
$name1 = empty($_POST['name1']) ? die ("Please Enter Your First Name") : mysql_escape_string($_POST['name1']);
$name2 = empty($_POST['name2']) ? die ("Please Enter Your Last Name") : mysql_escape_string($_POST['name2']);
$country = empty($_POST['country']) ? die ("Please Enter What Country You Are In") : mysql_escape_string($_POST['country']);
// open connection
$connection = mysql_connect("$host", "$user", "$pass") or die ("Unable to connect!");
// select database
mysql_select_db($db) or die ("Unable to select database!");
// create query
$query = "INSERT INTO users (user1, pass1, email1, email2, name1, name2, country) VALUES ('$user1', '$pass1', '$email1', '$email2', '$name1', '$name2', '$country',)";
// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
// print message with ID of inserted record
echo "New record inserted with ID ".mysql_insert_id();
// close connection
mysql_close($connection);
}
?>
</body>
</html>
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Re: It has another error
Do you see the comma before the closing parathesis? Kill that. See below...Darksevear wrote:I have fixed the old problem here is the new code: (see lower)
But now it says:
Error in query: INSERT INTO users (user1, pass1, email1, email2, name1, name2, country) VALUES ('gkhf', '', 'fkfkh', 'dfkhk', 'fkfkgfk', 'fkfk', 'fkfkfk',). 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 ')' at line 1
Code: Select all
<?php
// create query
$query = "INSERT INTO users (user1, pass1, email1, email2, name1, name2, country) VALUES ('$user1', '$pass1', '$email1', '$email2', '$name1', '$name2', '$country')"; // You need to ditch the ending comma after $country
?>-
AshrakTheWhite
- Forum Commoner
- Posts: 69
- Joined: Thu Feb 02, 2006 6:47 am
AshrakTheWhite wrote:also shouldnt varchar values be between " " instead of just there?
Code: Select all
<?php
// create query
$query = "INSERT INTO users (user1, pass1, email1, email2, name1, name2, country) VALUES ('$user1', '$pass1', '$email1', '$email2', '$name1', '$name2', '$country')"; // You need to ditch the ending comma after $country
?>