1

I have a problem with my code. I'm trying to add new post to the table events. I'm confused because I have used this code in other place on the same website (but it was using mysqli_query to register new user). mysqql_error returns "No database selected"

This is the code:

<?php
$add_title = $_POST['add_title'];
$add_happen = $_POST['add_happen'];
$add_created = date('Y-m-d');
$add_content = $_POST['add_content'];
$add_author = $_POST['add_author'];

//connect to 
    //localhost
$db_host = "localhost";
$db_username = "root";
$db_password = "";
$db_dbname = "zhp2";
$db_con = mysql_connect($db_host, $db_username, $db_password, $db_dbname); 

$query = "
    INSERT INTO events ( title, happen, created, content, author ) 
    VALUES ( '$add_title', '$add_happen', '$add_created', '$add_content', '$add_author') )
";

$retval = mysql_query($query,  $db_con);
if(! $retval ){
    die('Could not enter data: ' . mysql_error());
}
else{
    echo "Entered data successfully\n";
}

mysql_close($db_con);

//header('Location: ../../index.php?link=events');?>

I've tried to fix it using trial and error method playing with different combinations both mysql_query and mysqli_query

spiotr12
  • 79
  • 6
  • 4
    Please, [don't use `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli).[This article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide. [Prevent SQL Injection!](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Oct 29 '14 at 17:06
  • you have an extra ) at the end of your $query statement... – Rich701 Oct 29 '14 at 17:09

3 Answers3

4

Well then, you need to select the database! ;) The fourth parameter of mysql_connect() is not the database name. You need to do this separate of connecting to the MySQL server.

Using mysql_select_db() function:

$db_host = "localhost";
$db_username = "root";
$db_password = "";
$db_dbname = "zhp2";
$db_con = mysql_connect($db_host, $db_username, $db_password );
mysql_select_db( $db_dbname, $db_con );

And of course all the obligatory warnings about SQL injection, sanitizing your data, deprecation of mysql_* functions.

Community
  • 1
  • 1
Patrick Moore
  • 13,251
  • 5
  • 38
  • 63
4

You are confusing mysql_connect and mysqli_connect functions in the way you pass those parameters. In your example:

$db_con = mysql_connect($db_host, $db_username, $db_password, $db_dbname);  

you are passing a fourth parameter which is the database name but that wont work as you should only pass the three first (host,username,password) and then call mysql_select_db():

$db_con = mysql_connect($db_host, $db_username, $db_password);   
mysql_select_db( $db_dbname, $db_con );

In mysqli which is the BETTER way of doing it since mysql_ functions are very vulnerable and being deprecated from php you could pass four elements like here:

$db_con = mysqli_connect($db_host,$db_username, $db_password, $db_dbname) or die("Error " . mysqli_error($link)); 

which is close to what you are trying to do, but in a correct mysqli_ way.

Kypros
  • 2,997
  • 5
  • 21
  • 27
0

You need to select which database to connect to using the mysql_select_db function:

// make $db_dbname the current db
$db_selected = mysql_select_db($db_dbname, $db_con);
if (!$db_selected) {
    die ("Can't use $db_dbname : " . mysql_error());
}

See the PHP manual for more info: http://php.net/manual/en/function.mysql-select-db.php

danmullen
  • 2,556
  • 3
  • 20
  • 28