drgzmmy6379 2013-09-20 18:36
浏览 44

如何使用PHP将值插入MySQL? [关闭]

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 
  • 写回答

4条回答 默认 最新

  • drwjv28028 2013-09-20 18:38
    关注

    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.

    评论

报告相同问题?