php issue with inserting into sql - php

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

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.

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

Inserting Integer value into mysql int using INSERT

I'm trying to insert a new record in a MySQL database from PHP, which I've done a million times before, but for some reason, I can't get it to work this time, and it really bugs me.
Inserting strings into all the varchar collumns are going great, but when I get to inserting a value into the int column, I get an error telling me that I have a syntax error.
Basically, the first query works just fine, but the second one returns the error, and as you can see, I've made damn sure it really is an integer I'm trying to insert.
I hope somebody can help. I'm really starting to develop a headache over this :/
$groupId2 = 5;
$groupId = (int)$groupId2;
if(!mysqli_query($link, "INSERT INTO contestants (firstName, lastname, email) VALUES ('$firstName', '$lastName', '$email')"))
echo "First: " . mysqli_error($link);
if(!mysqli_query($link, "INSERT INTO contestants (firstName, lastname, email, group) VALUES ('$firstName', '$lastName', '$email', '$groupId')"))
echo "Second: " . mysqli_error($link);
group is a mysql keyword use back quotes around it
"INSERT INTO contestants (firstName, lastname, email, `group`)
VALUES ('$firstName', '$lastName', '$email', '$groupId')"
The error is because you surrounded your int with ' ', you need to get rid of your apostrophes and it will work just fine.
if(!mysqli_query($link,
"INSERT INTO contestants
(firstName, lastname, email, group) VALUES
('$firstName', '$lastName', '$email', $groupId)"))
^^^^^^^^^
To clarify, when inserting numerical fields you do not need them.
According to pst this is wrong, although, the fact you do not need single quotes is still correct.

Insert into Multiple Tables using PHP with LAST_INSERT_ID()

Below is the code that I've tried but I can't seem to make the right tweaks to get the script to run properly.
$sql = 'INSERT INTO clients (first_name, last_name, email) VALUES('.$_POST['first_name'].', '.$_POST['last_name'].', '.$_POST['email'].')
INSERT INTO client_data (client_id, phone, zip, note) VALUES(LAST_INSERT_ID(), '.$_POST['phone'].', '.$_POST['zip'].', '.$_POST['message'].')';
mysql_query($sql) or die (mysql_error());
Any help is much appreciated.
You cannot execute two sql statements with mysql_query();
Use something like this:
$sql = 'INSERT INTO clients (first_name, last_name, email) VALUES('.$_POST['first_name'].', '.$_POST['last_name'].', '.$_POST['email'].')';
mysql_query($sql);
$clientId = mysql_insert_id();
$sql = 'INSERT INTO client_data (client_id, phone, zip, note) VALUES('.$clientId.', '.$_POST['phone'].', '.$_POST['zip'].', '.$_POST['message'].')';
mysql_query($sql) or die (mysql_error());
But please read up on SQL injections and how to prevent them.
Just make 2 queries:
$sql_insert_clients = "INSERT INTO clients (first_name, last_name, email) VALUES(".$_POST['first_name'].", ".$_POST['last_name'].", ".$_POST['email'].")";
mysql_query($sql_insert_clients) or die (mysql_error());
$sql_insert_client_data = "INSERT INTO client_data (client_id, phone, zip, note) VALUES(".mysql_insert_id().", ".$_POST['phone'].", ".$_POST['zip'].", ".$_POST['message'].")";
mysql_query($sql_insert_client_data) or die (mysql_error());
You should break it up into two separate mysql_query calls and use the mysql_insert_id function:
$firstName = mysql_real_escape_string($_POST["first_name"]);
$lastName = mysql_real_escape_string($_POST["last_name"]);
$email = mysql_real_escape_string($_POST["email"]);
$phone = mysql_real_escape_string($_POST["phone"]);
$zip = mysql_real_escape_string($_POST["zip"]);
$message = mysql_real_escape_string($_POST["message"]);
mysql_query("INSERT INTO clients (first_name, last_name, email) VALUES ('{$firstName}', '{$lastName}', '{$email}')") or die(mysql_error());
mysql_query("INSERT INTO client_data (client_id, phone, zip, note) VALUES ('". mysql_insert_id() ."', '{$phone}', '{$zip}', '{$message}')") or die(mysql_error());
Your just missing the semi-colon to split the Insert's
$sql = 'INSERT INTO clients (first_name, last_name, email)
VALUES('.$_POST['first_name'].', '.$_POST['last_name'].', '.$_POST['email'].')'."; ".' INSERT INTO client_data (client_id, phone, zip, note) VALUES(LAST_INSERT_ID(), '.$_POST['phone'].', '.$_POST['zip'].', '.$_POST['message'].')';
mysqli_multi_query($sql) or die (mysql_error());
the SQL query it should be running (with dummy content) is
INSERT INTO clients (first_name, last_name, email) VALUES('test', 'surname', email); INSERT INTO client_data (client_id, phone, zip, note) VALUES(LAST_INSERT_ID(), 'phone', 'zip', 'message');

Categories