I have to insert in a MySQL database some data acquired from javascript. For send data to php script
I use AJAX, $.POST
of jQuery framework and mysqli function of PHP to insert data in the database.
javascript:
$.POST("insertData.php", {
name1: val1,
name2: val2,
...
}, function(data){
console.log(data);
});
php script:
<?php
$val1 = $_POST['name1'];
$val2 = $_POST['name2'];
...
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
$stmt = $mysqli->prepare('INSERT INTO tableName (name1, name2, ...) VALUES (?,?,...)');
$stmt->bind_param('ss...', $val1 , $val2 , ...);
$stmt->execute();
$stmt->close();
$mysqli->close();
?>
But with this method I must write the column name tree times, the first in javascript to send data with AJAX, the second in php to retrive data from $_POST
and the last again in php to make the query.
There is any proper way to insert data in the MySQL database from Javascript where you should not write more times the columns names?