I have the following piece of code that I am trying to execute:
// Connecting to the database "db_name"
$connection = mysql_connect('......com', 'login', 'password', 'db_name');
if (!$connection) {
die('Could not connect: ' . mysql_error());
}
echo "Connection Successful";
// creating a query
mysql_query($connection, "INSERT INTO `customers` (`FirstName`, `Lastname`, `Username`, `Email`,
`Password`) VALUES ($firstname, $lastname, $username,$email,$password);");
mysql_close($connection);
// printing to the screen
echo "Welcome $lastname . You have successfully logged in!";
The result I get is the following
Connection SuccessfulWelcome . You have successfully logged in! Add another user
However, there is nothing in the MySQL. No entries have been inserted. I get the following error:
Warning: mysql_query() expects parameter 1 to be string, resource given in /hermes/bosweb/web241/b2418/ipg........com/login_action.php on line 28 20130143043: chmmmmm.com/login_action.php PHP Warning: mysql_query() expects parameter 1 to be string, resource given in /hermes/bosweb/web241/b2418/ipg.e........com/login_action.php on line 28
mysql_query
takes the connection as an optional second parameter. Drop the $connection
parameter or move it to the second position rather than the first and you should be fine.
Note, too, that this API is deprecated and will be removed in future versions of PHP. Consider using the newer PDO API instead.
// creating a query
// resource mysql_query ( string $query [, resource $link_identifier = NULL ] )
mysql_query("INSERT INTO `customers` (`FirstName`, `Lastname`, `Username`, `Email`, `Password`) VALUES ($firstname, $lastname, $username,$email,$password);", $connection);
//mysql_query(STRING $query, RESOURCE $link_identifier);
$sql_query = "INSERT INTO `customers` (`FirstName`, `Lastname`, `Username`, `Email`, `Password`) VALUES ($firstname, $lastname, $username,$email,$password);"
mysql_query($sql_query, $connection);
Please read the documentation on mysqli
. Do not use mysql
functions as they are deprecated.
When using mysqli_query()
you do in fact need to provide the $connection
as the first parameter. Using mysqli
the following line of code will work as long as you don't have any SQL syntax errors.
mysqli_query($connection, "INSERT INTO `customers` (`FirstName`, `Lastname`, `Username`, `Email`, `Password`) VALUES ($firstname, $lastname, $username, $email, $password);");
The following is proper syntax for creating a mysqli connection, also.
$connection = mysqli_connect('localhost', 'user', 'pass', 'DB');
if (!$connection) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
According to the mysql_query syntax, the $connection
variable should be placed after the actual query. But this is optional.
Also you need to remove the ;
out of the query.
So replace your current line for this:
mysql_query("INSERT INTO
customers
(FirstName
,Lastname
,Username
,Password
) VALUES ($firstname, $lastname, $username,$email,$password)");
Good Luck!