Я пытаюсь выбрать данные из одной таблицы и вставить их в другую, не обновляя страницу, но сейчас я чувствую себя немного беспомощным.
Как мне написать что-то подобное с помощью jQuery / Ajax:
function myFunction($conn){
$sql ="
INSERT INTO MyTable (full_name, main_id, Status, Become)
SELECT mt.full_name, mt.main_id, true, CURRENT_TIMESTAMP
FROM MyOtherTable mt
WHERE NOT EXISTS
(SELECT main_id FROM MyTable mt_copy
WHERE mt_copy.main_id= mt.main_id)
AND mt.main_id= ".$_POST['main_id']." ;
";
$result = $conn->query($sql);
}
Я могу сделать простой как это:
PHP
$conn = mysqli_connect('localhost', $user, $user, $user);
$stmt = $conn->prepare(" INSERT INTO Posts(post_content, posted, customer_id) VALUES(?, CURRENT_TIMESTAMP, '".$_SESSION['id']."')");
$stmt->bind_param("s", $post_content);
$post_content = $_POST['post_content'];
$stmt->execute();
#...
JS
$(document).ready(function(){
$(".new-post").on('click', function(e){
e.preventDefault();
let post_content=$("#post_content").val();
$.ajax({
url:'insertPosts.php',
method:'POST',
data:{
post_content:post_content
},
success:function(r){
console.log("New post updated!!", r);
location.reload();
},
error:function(e){
console.log("Unexpected results", e);
}
});
});
});