Related
What I'm actually trying to do is insert a row with:
INSERT INTO users VALUES (col1, col2, ...)
where col1 is an auto_increment.
The PHP code is:
<?php
$host = "http://name.altervista.org/";
$user = "name";
$psw = "";
$db = "my_name";
$response = array();
$response["success"] = true;
$connessione = new mysqli($host, $user, $psw, $db);
if($connessione->connect_errno == 0)
{
$nome = $_POST["nome"];
$cognome = $_POST["cognome"];
$username = $_POST["username"];
$password = $_POST["password"];
$nTelefono = $_POST["nTelefono"];
$email = $_POST["email"];
$sql = "INSERT INTO users
VALUES (DEFAULT, '$nome', '$cognome', '$username', '$password', '$nTelefono', '$email')";
$ris = $connessione->query($sql);
if($connessione->affected_rows == 1)
{
echo(json_encode($response));
}
else
{
$response["success"] = false;
echo(json_encode($response));
}
}
else
{
$response["success"] = false;
echo(json_encode($response));
}
?>
I search similar questions here in stackoverflow, and I try to use DEFAULT or NULL, but it doesn't work. And if I put a number instead of the default value that is not already in the table it works, so I really don't understand where the problem is.
Have you any other suggestions?
EDIT: The table structure on the database:
click
EDIT 2: I tried to delete the table and create it again, and now it works with the NULL thing. Thanks for the support!
When you are doing an insert, list all the columns being inserted. You seem to want:
INSERT INTO users (nome, cognome, username, password, nTelefono, email)
VALUES ('$nome', '$cognome', '$username', '$password', '$nTelefono', '$email');
Next. Never store clear-text passwords in the database. You should be encrypting the value on the client side so the values are never passed over the network.
Next. Learn to use parameterized queries. When you munge query strings with parameter values, your are asking for inexplicable syntax errors and making the code subject to SQL injection attacks.
From the Mysql docs
INSERT INTO users VALUES ('$nome', '$cognome', '$username', '$password', '$nTelefono', '$email')";
The auto_increment fields doesn't need to be set in an INSERT statement
In MySQL, if you have an auto_increment column you don't need to put it in the insert statement.
Example:
You have the table:
CREATE TABLE test (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(30) NOT NULL
);
Your SQL statement should be:
INSERT INTO test(name) VALUES ('My name');
I have two tables
user_details
load_owner
user_details
user_id as primary key
name
password
phone
load_owner
load_owner_id as a primary key
user_id as foreign key
address
bank name
I need to insert all the values at the same time. I tried it but in load_owner table user_id values are insert as NULL. My PHP code is following
if(isset($_POST['submit']))
{
$name = $_POST['name'];
$phone = $_POST['phone'];
$password = $_POST['password'];
$address = $_POST['address'];
$bank_name = $_POST['bank_name'];
$user_insert = mysql_query("insert into user_details (name, password, phone) values ('$name', '$password', '$phone')");
if($user_insert==false)
{
echo "".mysql_error();
}
$load_owner_insert = mysql_query("insert into load_owner ( address, bank_name) values ('$address''$bank_name')");
if($load_owner_insert==false)
{
echo "".mysql_error();
}
}
?>
That is because you aren't actually specifying user_id value while inserting to load_owner table. You need to fetch the user_id value first, so that it becomes:
$user_insert = mysql_query("insert into user_details (name, password, phone) values ('$name', '$password', '$phone')");
$user_id = mysql_insert_id();
And then insert user_id into load_owner
$load_owner_insert = mysql_query("insert into load_owner ( user_id, address, bank_name) values ($user_id, '$address', '$bank_name')");
PS: Use PDO with prepared statement, this SQL query is prone to SQL Injections and the mysql library is deprecated.
I have the following code, which will get values from these 9 variables and insert them in to a table. However, I don't know how to insert NULL value in to my table. As shown in line 10, if I try to use the keyword NULL, it consider as a string.
$WorkOrder = $_POST['WorkOrder'];
$Originated = $_POST['Originated'];
$CustID = $_POST['CustID'];
$Customer = $_POST['Customer'];
$Contact = $_POST['Contact'];
$Completed = $_POST['Completed'];
$AccountNum = $_POST['AccountNum'];
$Description = $_POST['Description'];
$Status = $_POST['Status'];
$Status = "NULL"; //Testing with NULL
$insertquery = "INSERT INTO ElectronicShop(WorkOrder, Originated, CustID, Customer, Contact, Completed, AccountNum, Description, Status)
VALUES ('$WorkOrder','$Originated','$CustID','$Customer','$Contact','$Completed','$AccountNum','$Description','$Status')";
$data = sqlsrv_query($connectString, $insertquery) or die(print_r(sqlsrv_errors(SQLSRV_ERR_ALL), true));
I will be getting values for these 9 variables via text boxes in a webpage. So, I can't manually enter NULL in my query.
Instead of:
$insertquery = "INSERT INTO ElectronicShop(WorkOrder, Originated, CustID, Customer, Contact, Completed, AccountNum, Description, Status) VALUES ('$WorkOrder','$Originated','$CustID','$Customer','$Contact','$Completed','$AccountNum','$Description','$Status')";
Use:
$insertquery = "INSERT INTO ElectronicShop(WorkOrder, Originated, CustID, Customer, Contact, Completed, AccountNum, Description, Status) VALUES
('$WorkOrder','$Originated','$CustID','$Customer','$Contact','$Completed','$AccountNum','$Description', NULL)";
You don't need to specify a variable for it, just write "NULL" without string marking signs (') into your query.
Greetz
Don't use the qoutes "":
$Status = "NULL";
Use:
$Status = null;
If you have not default value for Status column another than NULL, simply do not use this column in the columns list of insert statement.
Great, now I can 'insert' into the database, but in phpmyadmin the fields are white, without content... This is the php code:
$name = $_POST["name"];
$institution = $_POST["institution"];
$email = $_POST["email"];
$country = $_POST["country"];
$hcp = $_POST["hcp"];
$texto = $_POST["texto"];
$state = $_POST["state"];
$license = $_POST["license"];
$consulta = ("INSERT INTO `1264`(`name`,`institution`,`email`,`country`,`hcp`,`texto`,`state`,`license`) VALUES ('$name','$institution','$email','$country','$hcp','$texto','$state','$license');");
mysql_query($consulta,$connection) or die("You cannot register. Try it later, please.");
Does anybody know why?
Please change your query
you have extra ',' both after 'license' and '$license'
$consulta = ("INSERT INTO `1264`(`id`,`name`,`institution`,`email`,`country`,`hcp`,`texto`,`state`,`license`) VALUES (NULL, '$name','$institution','$email','$country','$hcp','$texto','$state','$license');");
mysql_query($consulta,$connection) or die("You cannot register. Try it later, please.");
Also try using mysqli_* functions. mysql_* functions going to deprecated.
Try this:
$consulta = ("INSERT INTO `1264`(`id`,`name`,`institution`,`email`,`country`,`hcp`,`texto`,`state`,`license`) VALUES (NULL, '$institution','$email','$country','$hcp','$texto','$state','$license');");
ie, remove the extra comma at the last
Dont make the query so clumpsy. Here is a simple edit,
$consulta = "INSERT INTO `1264` SET
`name` = '$name',
`institution` = '$institution',
`email` = '$email',
`country` = '$country',
`hcp` = '$hcp',
`texto` = '$texto',
`state` = '$state',
`license` = '$license'";
You don't have to write the id and leave its value field null, it will be auto generated by default.
if id is an primary key then it should not be null
and if it is an auto increment field have no need to put it on field list and value
$consulta = ("INSERT INTO 1264(name,institution,email,country,hcp,texto,state,license,) VALUES ( '$name','$institution','$email','$country','$hcp','$texto','$state','$license',);");
do echo $consulta ;
to check if the query is coming properly or try put the variables out of cote check the name one
VALUES ( '".$name."','$institution','$email','$country','$hcp','$texto','$state','$license',)
This should work - enter each variable between quotes, especially when its a string
$consulta = ("INSERT INTO `1264`(`id`,`name`,`institution`,`email`,`country`,`hcp`,`texto`,`state`,`license`) VALUES (NULL, "'. $name ."','".$institution."','".$email."','".$country."','".$hcp.'","'.$texto.'","'.$state.'",'".$license."');");
Removed the last comma and Id -> NULL so try this:
`$consulta = ("INSERT INTO` `1264`(`name`,`institution`,`email`,`country`,`hcp`,`texto`,`state`,`license`) VALUES `('$name','$institution','$email','$country','$hcp','$texto','$state','$license');");
Wondering if there is a shorthand version to insert a new record into a table that has the primary key enabled? (i.e. not having to include the key column in the query)
Lets say the key column is called ID, and the other columns are Fname, Lname, and Website
$query = "INSERT INTO myTable VALUES ('Fname', 'Lname', 'Website')";
Use the DEFAULT keyword:
$query = "INSERT INTO myTable VALUES (DEFAULT,'Fname', 'Lname', 'Website')";
Also, you can specify the columns, (which is better practice):
$query = "INSERT INTO myTable
(fname, lname, website)
VALUES
('fname', 'lname', 'website')";
Reference:
http://dev.mysql.com/doc/refman/5.6/en/data-type-defaults.html
I prefer this syntaxis:
$query = "INSERT INTO myTable SET fname='Fname',lname='Lname',website='Website'";
$query = "INSERT INTO myTable VALUES (NULL,'Fname', 'Lname', 'Website')";
Just leaving the value of the AI primary key NULL will assign an auto incremented value.
This is phpMyAdmin method.
$query = "INSERT INTO myTable
(mtb_i_idautoinc, mtb_s_string1, mtb_s_string2)
VALUES
(NULL, 'Jagodina', '35000')";
You can also use blank single quotes for the auto_increment column. something like this. It worked for me.
$query = "INSERT INTO myTable VALUES ('','Fname', 'Lname', 'Website')";