Mysql Error could not enter data. No database selected - php

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

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

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' )";

Related

mysql api submite towice

I tried many times to submit the form when it submitted it repeated the submission twice on the data. I don't understand why,please help me. and when I put the header location it doesn't work ever
here is the code
<?php
$name= $_POST['form_name'];
$mrn= $_POST['form_mrn'];
$mobile= $_POST['form_mobile'];
$link = mysql_connect('server', 'user', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db('dbname');
if (!$db_selected) {
die('Could not select database: ' . mysql_error());
}
mysql_set_charset('utf8',$link);
$query = "INSERT INTO pharmacy ( name , mrn , mobile ) VALUES ('$name', '$mrn', '$mobile')";
$result = mysql_query($query);
header('Location: form.html');
$link->close();
?>
<?php
$name= $_POST['form_name'];
$mrn= $_POST['form_mrn'];
$mobile= $_POST['form_mobile'];
$link = mysql_connect('server', 'user', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db('dbname');
if (!$db_selected) {
die('Could not select database: ' . mysql_error());
}
mysql_set_charset('utf8',$link);
//Just Put this code before inserting
if($name !=""){
$query = "INSERT INTO pharmacy ( name , mrn , mobile ) VALUES ('$name','$mrn', '$mobile')";
$result = mysql_query($query);
header('Location: form.html');
}
$link->close();
?>
Just check condition before insert. Let me know if facing same issue.

PHP - Column count doesn't match

I've recently trying to add data into a database, (New to php), I've looked over what other people has with this error but yet still I cannot see where I have gone wrong.
The error is:
Column count doesn't match value count at row 1
Code:
$dbhost = 'localhost';
$dbuser = 'evocityi_admin';
$dbpass = 'password';
$database = 'evocityi_stocks';
$conn = mysql_connect($dbhost, $dbuser, $dbpass, $database);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$Dtime = "30/04/16";
$StockName = "FUMUKU";
$FUMUKUPrice = 1000;
$sql = "INSERT INTO stocks".
"(Stock,Price, TimeD) ".
"VALUES ".
"('$StockName,$FUMUKUPrice, $DTime')";
mysql_select_db('evocityi_stocks');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not enter data: ' . mysql_error());
}
echo "Entered data successfully\n";
mysql_close($conn);
?>
SQL Database:
https://gyazo.com/e74a6b9e87c18d60855424dcae647cdf
Change the column type for Stock and TimeD to varchar in your table definition. On the link you posted, they are both int.
as suggested by #PaulB12345, change column type to varchar and there is error in quotes.
ideally your query should be like (see quotes after values)
$sql = "INSERT INTO stocks".
"(Stock,Price, TimeD) ".
"VALUES ".
"('$StockName','$FUMUKUPrice', '$DTime')";

INSERT statement not working mysql

When I process this data, the data is not being inserted into my database. This is my current database structure:
https://drive.google.com/file/d/0BzJ9StkJe55WaG1oaVhqcUJmSGc/edit?usp=sharing
I have no clue why it is not inserting. I am not getting an error message and I am seeing the successfully inserted data piece.
<head><title>Process Punch</title></head>
<?php
define('DB_NAME', 'name');
define('DB_USER', 'user');
define('DB_PASSWORD', 'pass');
define('DB_HOST', 'host');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link){
die('Could not connect: ' .mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected) {
die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}else{
$userid_value = $_POST['userid'];
$punchtype_value = $_POST['punchtype'];
$group_value = $_POST['group'];
$dept_value = $_POST['dept'];
$notes_value = $_POST['notes'];
$table = "tc_".$userid_value;
$date_value = date("Y-m-d h:i:s");
echo $table;
$sql = "INSERT INTO $table (punchtype, groupname, dept, notes) VALUES ('$punchtype_value', '$group_value', '$dept_value', '$notes_value')";
echo "Successfully inserted data";
}
?>
Execute the query with mysql_query(), concatenate the query
$sql = "INSERT INTO `".$table."` (`punchtype`, `groupname`, `dept`, `notes`) VALUES ('".$punchtype_value."', '".$group_value."', '".$dept_value."', '".$notes_value."')";
$qry = mysql_query($sql ,$link);
echo "Successfully inserted data";

Get url parameter and insert into database

Trying to insert into database by typing the value in the url, but having difficulties to insert into the database:
Here is the URL:
http://student.cs.hioa.no/~s180343/updatedb.php?verdi=22
Here is the code:
<?php
$dbhost = "MYSQL-SERVER";
$dbuser = "USERNAME";
$dbpass = "";
$verdi = $_GET['verdi'];
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = "INSERT INTO test ('id') VALUES (`$verdi`)";
mysql_select_db('s180350');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not enter data: ' . mysql_error());
}
echo "Entered data successfully\n";
mysql_close($conn);
?>
Use quotes around your string values. Use ticks around your column names. You have it backwards:
$sql = "INSERT INTO test ('id') VALUES (`$verdi`)";
shjould be
$sql = "INSERT INTO test (`id`) VALUES ('$verdi')";
FYI, you are wide open to SQL injections
You are doing reverse i.e. adding '' for column name and `` for the value
$sql = "INSERT INTO test ('id') VALUES (`$verdi`)";
should be
$sql = "INSERT INTO test (`id`) VALUES ('$verdi')";
Start using prepare statement or at least below after conn is defined.
$verdi = mysql_real_escape_string($verdi);
please dont forget to secure all user input into your sql querys. see SQL injection wiki
The problem with your code is wrong use on quotes. See edited code:
$conn = mysql_connect("MYSQL-SERVER", "USERNAME", $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('s180350');
$retval = mysql_query( "INSERT INTO test ('id') VALUES ('".mysql_real_escape_string($_GET['verdi'])"')", $conn );
if(! $retval )
{
die('Could not enter data: ' . mysql_error());
}
echo "Entered data successfully\n";
PS: Dont use up resources by setting variables you dont need.

Can't execute a sql query (mysql, php)

the connection to the db works, but than the sql query fails (I have tried many variations with no success). Help would be appreciated.
This is my code:
$con = mysqli_connect("127.0.0.1:3306","root","*****","bsr");
if(! $con )
{
die('Could not connect: ' . mysql_error());
}
$query = "INSERT INTO eden (username,password) VALUES ('aaa', 'ddd')";
$retval = mysqli_query($con,$query);
if(! $retval )
{
die('Could not enter data: ' . mysql_error());
}
result: "Could not enter data:"
Try this:
$query = "INSERT INTO eden (username,password) VALUES ('aaa', 'ddd')";
$retval = $con -> query($query);
if(! $retval ){
die('Could not enter data: ' . mysql_error());
}

Categories