0

This is my Php file code. When i insert the data using input fields in HTML i get the Error could not enter data. No database selected.

$dbhost="localhost";

$dbuser="root";

$dbpass="";

$conn = mysql_connect($dbhost, $dbuser, $dbpass);

if(! $conn ) {

die('Could not connect: ' . mysql_error());

}

$sql = 'INSERT INTO sale '.
'(Name, Contact, Email, Property_type, Price, Location) '.
'VALUES ( "$a", "$b", "$c", "$d", "$e", "$f" )';

mysql_select_db('gharghar_gharastee');
$retval = mysql_query( $sql, $conn );

if(! $retval ) {

die('Could not enter data: ' . mysql_error());

}

echo "Entered data successfully\n";

mysql_close($conn);

?>
  • What happens if you run query direct database? – Perdeep Singh Jul 03 '16 at 08:09
  • #1064 - 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 ''(Name, Contact, Email, Property_type, Price, Location) '. VALUES ( "oman", "' at line 3 – Sheikh Noman Mehmood Jul 03 '16 at 08:17
  • So theres the problem your query got issue could you please print full query here that you tried to run on db. – Perdeep Singh Jul 03 '16 at 08:19
  • **Warning**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) which has been **removed** entirely from the latest version of PHP. You should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). – Quentin Jul 03 '16 at 16:55

2 Answers2

1

Try checking the connection to your db

// make foo the current db
$db_selected = mysql_select_db('gharghar_gharastee', $conn);
if (!$db_selected) {
    die ('Can\'t use gharghar_gharastee : ' . mysql_error());
}

And I suggest you use PDO instead of mysql_connect - for security reasons and it has be depreciated in the newer version of php

MistaJase
  • 839
  • 7
  • 12
-1

You must use mysql SQL like this.

$sql = 'INSERT INTO sale '.
'(Name, Contact, Email, Property_type, Price, Location) '.
"VALUES ( '$a', '$b', '$c', '$d', '$e', '$f' )";
Andy Ryu
  • 79
  • 2