unknown column in field list error with mysql - php

I did
include_once'include/connection.php';
$fname = 'olo';
$lname = 'mike';
$uname = 'nolo';
$pass = 'mmmmm';
$query2 = "INSERT INTO imt.`$uname`
(fname, lname, pass) VALUES (`$fname`, `$lname`, `$pass`)";
$result2 = mysql_query($query2, $connection);
if(!$result2){echo mysql_error();}
var_dump($query2);
but got this output
Unknown column 'olo' in 'field list'
string 'INSERT INTO imt.`nolo`
(fname, lname, pass) VALUES (`olo`, `mike`, `mmmmm`)' (length=76)
I also tried using single quotes and/ or curly braces but still the same thing. I switched the variables to the actual data and got the same error. Help please.

Not backticks, but also quotes
$query2 = "INSERT INTO imt.`$uname` (fname, lname, pass) VALUES('$fname', '$lname', '$pass')";
^ here ^ ^ ^
backticks are needed when you have to deal with column names which matches native SQL functions, otherwise they are not necessary. In this case if you'd like to use them you should do like this
"INSERT INTO imt.$uname (`fname`, `lname`, `pass`) VALUES('$fname', '$lname', '$pass')"

VALUES (`$fname`, `$lname`, `$pass`)
should be
VALUES ('$fname', '$lname', '$pass')

Related

unable to perform query using mysqli_query

$name = mysqli_real_escape_string($connection, $_POST["name"]);
$surname = mysqli_real_escape_string($connection, $_POST["surname"]);
$username = mysqli_real_escape_string($connection, $_POST["username"]);
$email = mysqli_real_escape_string($connection, $_POST["email"]);
$pw1 = mysqli_real_escape_string($connection, $_POST["pw1"]);
$query = "INSERT INTO 'users' ('id','name', 'surname', 'username', 'email', 'password') VALUES (NULL,'$name', '$surname', '$username', '$email', '$pw1')";
$result = mysqli_query($connection, $query);
if(!$result){
echo ("fail");
}
I test if the query has worked using if(!$result){ echo ("fail");} and it echoes fail every time and no data is inserted into the database every time! I have checked the syntax and i believe it is correct... could this be because of the database "collation"?
You should not use the single quote at the table or field name. You have to use a Backtick (like ``) which is located in under Esc key or left side of 1 Key or upper side of Tab key. It should looks like:
$query = "INSERT INTO `users` (`id`, `name`, `surname`, `username`, `email`,
`password`) VALUES ('null', '$name', '$surname', '$username', '$email', '$pw1')";
or
$query = "INSERT INTO users (id, name, surname, username, email,
password) VALUES ('null', '$name', '$surname', '$username', '$email', '$pw1')";
Note: If your id field is already set auto increment then you can remove id and value null. Because id value will automatically increment.
Hope it will helpful.

php issue with inserting into sql

I am having an issue inserting into mysql db using php.
specifically this code is giving me problems:
$sql = "INSERT INTO '$mysql_database'.'$UsersTable' ('firstName', 'lastName', 'password', 'email','userType')VALUES ('$firstName', '$lastName', '$password','$email','$userType')";
I can't seem to find the error with syntax here. All the information to me seems correct. where am I going wrong ?
try this
$sql = "INSERT INTO $mysql_database.$UsersTable (firstName, lastName, password, email,userType)VALUES ('$firstName', '$lastName', '$password','$email','$userType')";
remove Quote from table column
Try this query
$sql = "INSERT INTO $mysql_database.$UsersTable (firstName, lastName, password, email,userType)VALUES ('$firstName', '$lastName', '$password','$email','$userType')";
Hello you can't use single Quote from table column name so remove it
$sql = "INSERT INTO $mysql_database.$UsersTable (firstName, lastName, password, email,userType)VALUES ('$firstName', '$lastName', '$password','$email','$userType')";
otherwise use this way
$sql = "INSERT INTO $mysql_database.$UsersTable (`firstName`, `lastName`, `password`, `email`,`userType`)VALUES ('$firstName', '$lastName', '$password','$email','$userType')";

INSERT query of xampp MySQL doesn't work

This php insert query is not working in MYSQL xampp. I couldn't find any error
QUERY:
$query = "INSERT INTO member (id, username,fname,lname,email, password, salt )
VALUES ( '$username', '$password', '$email', '$salt' )";
you are missing $fname, $lname in query also use NULL for id if auto incremented
$query = "INSERT INTO member (id, username,fname,lname,email, password, salt )
VALUES (NULL, '$username', '$fname', '$lname', '$password', '$email', '$salt' )";
you are passing wrong number of column names.your correct query should look like this:
$query = "INSERT INTO member (username,password,email,salt )
VALUES ( '$username', '$password', '$email', '$salt' )";
You are not inserting values to all the columns specified in the query.
In your query
$query = "INSERT INTO member (id, username,fname,lname,email, password, salt )
VALUES ( '$username', '$password', '$email', '$salt' )";
You are specifying 7 columns and only 4 values .So either add more values or remove unnecessary columns specified in the query like
$query = "INSERT INTO member (username, password,email, salt )
VALUES ( '$username', '$password', '$email', '$salt' )";
OR
$query = "INSERT INTO member (id, username,fname,lname,email, password, salt )
VALUES ('$id', '$username','$fname','$lname','$email', '$password', '$salt' )";

Mysql Insert Into Error

I've created a registration form and I'm adding the data with php but for some reason it will not let me add the data into the database. Can you see anything wrong with this code?
<?php
mysql_connect("localhost", "root", "password") or die("No Connection");;
mysql_select_db("music") or die("No Database");;
$username= mysql_real_escape_string($_REQUEST["username"]);
$password= mysql_real_escape_string($_REQUEST["password"]);
$email= mysql_real_escape_string($_REQUEST["email"]);
$hash = md5( rand(0,1000) );
mysql_query("INSERT INTO members (id, username, password, email, hash, active) VALUES('', '$username', '$password', '$email', '$hash', '')") or die("Can't Add");
if(mysql_affected_rows()>0){
echo "1";
}else{
echo "2";
}
?>
I Keep getting a Can't Add error indicating that there is a simple problem with the mysql_query row
Thank you
Can't add is not an error, just a catch all statement you added at the end.
To see your actual problem, change your code at the end of the mysql_query line to include the actual error retrieved from mysql_error().
mysql_query("INSERT INTO members (username, password, email, hash, active) VALUES('$username', '$password', '$email', '$hash', '')")
or die("Can't Add - " . mysql_error());
That will give you more details regarding the error, if you post that, I can update my answer with the reason why.
Note that I've also removed the insertion of id, it's not needed if your column is AUTO_INCREMENT, which it should be.
if id is your primary key then replane '' with null
Make id as auto-incremented primary key and remove it from insert query.
there may be unique constraints for any of the column
Either have
mysql_query("INSERT INTO members (id, username, password, email, hash, active)
VALUES(null, '$username', '$password', '$email', '$hash', '')") or die("Can't Add");
^^^^
OR
mysql_query("INSERT INTO members (username, password, email, hash, active)
VALUES( '$username', '$password', '$email', '$hash', '')") or die("Can't Add");

MySQL can't see column in PHP. Ok as SQL statement

Im trying use the following insert:
mysql_query ("INSERT INTO users (company_name, fname, lname, salt, email, date_added, password)
VALUES ('$CompanyName', '$fname', '$lname', '$salt', '$email', '$mysqldate', '$encrypted')")
or die(mysql_error());
But I get an error:
Unknown column 'company_name' in 'field list'
If I echo out the query, paste it as an SQL statement and run it, it does the insert. All the fields exist, and, as I say, if I echo out the result it works fine.
I see you have mixed case in the $CompanyName variable. Is it possible your MySQL column is also titled in mixed case? Maybe "Company_name" or "Company_Name"?
Why are your variables enclosed in single quotes? PHP will read those as string. Enclose them in double quotes. But I don't see why you get that error. How about using table_name.column_name instead of just column_name?
I don't see any problem with your query, if it matches the schema, it should work.
Anyway, try escaping the table and field names, below is the modified query:
msql_query ("INSERT INTO `users` (`company_name`, `fname`, `lname`, `salt`, `email`, `date_added`, `password`)
VALUES ('$CompanyName', '$fname', '$lname', '$salt', '$email', '$mysqldate', '$encrypted')")
or die(mysql_error());
Try
mysql_query ("INSERT INTO users (fname, lname, salt, email, date_added, password)
VALUES ('$fname', '$lname', '$salt', '$email', '$mysqldate', '$encrypted')")
or die(mysql_error())
If the problem still persists, there is a problem with your connection or your users table.
Just a blind guess... are you sure that the mysql_query statement replaces the placeholders with the actual variable values as "echo" does?
Try:
mysql_query ("INSERT INTO users (company_name, fname, lname, salt, email, date_added, password)
VALUES ('" . $CompanyName . "', '" . $fname . "', '" . $lname . "', '" . $salt. "', '" . $email . "', '" . $mysqldate . "', '" . $encrypted . "')")
or die(mysql_error());

Categories