How to Insert Data into a MySQL Database

How to insert values to mysql database from other schema table?

You have to remove VALUES from your query, this should work:

INSERT INTO B.woreemp (id, created_at, designation, expertise, name) 
SELECT
employees.id,
employees.created_at,
employees.designation,
employees.expertise,
employees.name
FROM A.employees;

Depending on the schema of the table, you might not be able to do this (if id in B.woreemp.id is autoincrement or something similar). In that case just not populate that column and remove it from both column lists.

You can find more details here: http://www.mysqltutorial.org/mysql-insert-into-select/

How to insert data into mysql table without duplicating with python?

Check out this article

In short:
You have to check if those values exist in your database or not and then insert.

Cannot insert data into MySQL Database

Try this:

INSERT INTO users (username, first_name, last_name, email, password, sign_up_date, activated) VALUES ('$un','$fn','$ln','$em','$pswd','$d','0')`

If your id column is auto increment, you're not required to fill this up by yourself.

And for your date, you can check this link for date reference. date structured column requires YY-MM-DD format, which you can get by:

$d = date("Y-m-d");

And does users from your system get to upload videos? :P

Insert data into MySQL database from PHP form with (SELECT) and (INSERT INTO)

The error directly relates to the fact that there are the wrong amount of variables supplied for the SQL statement parameters. Alternatively, you are trying to update a column which does not exist in the table.

Check the column names match exactly with the supplied column names in the statement, and make sure you supply data only for columns which exist in the table.

Also, if you want PHP to interpolate values iinto variables within your SQL statement, you MUST use double quotes instead of single quotes for your SQL statement. PHP will only interpolate values into variables if you use double quotes.

Database php scripting, insert data into table

So you had a syntax error with a missing ; (which you've now fixed), but there is still two issues:

Your DB_HOST variable is not set properly, it should just be:

define('DB_HOST', '127.0.0.1');

You don't need the port number and with MAMP you most likely need to use the localhost IP: 127.0.0.1

Your second problem is the way you're creating the insert query, it should look like this when you're concatenating values:

$sql = "INSERT INTO movies (title, rating, Runtime, movie_rating, release_date)
VALUES ('".$value."', '".$value2."', '".$value3."','".$value4."','".$value5."')";

In saying that PHP MySQL has been deprecated since PHP 5.5.0

So try using PHP MySQLi like below:

<?php
$databaseName = 'movie_database';
$databaseUser = 'root';
$databasePassword = 'root';
$databaseHost = '127.0.0.1';

$conn = new mysqli($databaseHost, $databaseUser, $databasePassword, $databasePassword);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

echo "Connected successfully";

// Note* This is basic sanitizing but you can be more careful with this
$value = mysql_real_escape_string($_POST['title']);
$value2 = mysql_real_escape_string($_POST['rating']);
$value3 = mysql_real_escape_string($_POST['Runtime']);
$value4 = mysql_real_escape_string($_POST['movie_rating']);
$value5 = mysql_real_escape_string($_POST['release_date']);

// Concatenate the $values into the string
$sql = "INSERT INTO movies (title, rating, Runtime, movie_rating, release_date)
VALUES ('".$value."', '".$value2."', '".$value3."','".$value4."','".$value5."')";

if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();

?>

How to insert data into database with column name default

Try escaping the column name default with backticks, like you have with the table name.

Check before adding the data into table in mysql database

If you need to have this check in your insert statement, you can structure it like this:

SQL Fiddle: http://sqlfiddle.com/#!9/d3dd77/1

insert into emp_master (token_no, vehicle_no) 
select 1234, 12345
from dual
where not exists (select * from emp_master where vehicle_no = '12345');

Another way to do this on any DBMS:

DB Fiddle Demo

insert into emp_master (token_no, vehicle_no) 
select token_no, vehicle_no
from (
select 1234 token_no, 12345 vehicle_no
) rec
where not exists (select * from emp_master where vehicle_no = rec.vehicle_no);


Related Topics



Leave a reply



Submit