Page 1 of 1

Whats wrong with this code?

Posted: Sat May 13, 2006 10:45 pm
by Darksevear
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>

Right here

Posted: Sat May 13, 2006 11:07 pm
by tr0gd0rr
Darksevear wrote:

Code: Select all

// open connection
    $connection = mysql_connect('$host', '$user', '$pass') or die ("Unable to connect!");
should be

Code: Select all

$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
or

Code: Select all

$connection = mysql_connect("$host", "$user", "$pass") or die ("Unable to connect!");

It has another error

Posted: Sat May 13, 2006 11:30 pm
by Darksevear
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>

Re: It has another error

Posted: Sun May 14, 2006 12:06 am
by RobertGonzalez
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
Do you see the comma before the closing parathesis? Kill that. See below...

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
?>

Posted: Sun May 14, 2006 1:13 am
by AshrakTheWhite
also shouldnt varchar values be between " " instead of just there?

Posted: Sun May 14, 2006 1:32 am
by infolock
AshrakTheWhite wrote:also shouldnt varchar values be between " " instead of just there?
:?: They are...

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 
?>
notice how there are ' (single quotes) around each field value..