TRUNCATE and INSERT PHP MySQL

<?php
$connect = mysqli_connect("localhost", "root", "", "root"); 
if(isset($_POST["id"]))
{
foreach($_POST["id"] as $id)
{
$truncatequery = "TRUNCATE TABLE tbl_trans";
mysqli_query($connect, $truncatequery);
$insquery = "INSERT INTO tbl_trans (id, branch, outlet, user, item, serialno, dater, problem, status, remarks, tech)
SELECT id, branch, outlet, user, item, serialno, dater, problem, status, remarks, tech
FROM tbl_repair WHERE id = '".$id."'"; 
mysqli_query($connect, $insquery);

}
}
?>

Open in new window

Anyone here can help my issue?..
I tried to TRUNCATE tbl_trans before inserting data from tbl_repair to tbl_trans. Now my problem is if I execute the query it only copy 1 row instead of multiple data. I tried to check if my insert method is working by removing TRUNCATE query as I expected it work. It copies multiple data, but something happen when I add the TRUNCATE query it copies only 1 row...

Thanks in advance.
Yaku KakashiAsked:
Who is Participating?
 
Nitin SontakkeDeveloperCommented:
Change code as follows:

<?php
$connect = mysqli_connect("localhost", "root", "", "root"); 
$truncatequery = "TRUNCATE TABLE tbl_trans";
mysqli_query($connect, $truncatequery);
if(isset($_POST["id"]))
{
foreach($_POST["id"] as $id)
{
$insquery = "INSERT INTO tbl_trans (id, branch, outlet, user, item, serialno, dater, problem, status, remarks, tech)
SELECT id, branch, outlet, user, item, serialno, dater, problem, status, remarks, tech
FROM tbl_repair WHERE id = '".$id."'"; 
mysqli_query($connect, $insquery);

}
}
?>

Open in new window

0
 
Nitin SontakkeDeveloperCommented:
Sounds hugely unlikely to me, somehow.

Forget the php part, did you manage to issue just the SELECT statement directly in mySQL and what do you get? You must, of course, issue an entire statement without a change of even one character.

If possible, take contents of the $insquery, and just use the select statement from it.

If your id is the unique column in the tbl_repair table (which apparently it looks like) how are you expecting more than one rows?
0
 
Yaku KakashiAuthor Commented:
I want to copy data from tbl_repair to tbl_trans, but before copy executed i want to clear data's from tbl_trans.

Something isn't working on me.
example. If I copy ID's 1, 2, 3 I only get ID 1 which supposedly 1, 2 and 3 rows
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
Yaku KakashiAuthor Commented:
Thank You very much... It works.
And I understand the process.
truncate query will execute by simply opening the page before the insert query execute.
Thank You.
0
 
Yaku KakashiAuthor Commented:
Thank You so much Nitin Sontakke. You solve my problem for only a minute.
0
 
Nitin SontakkeDeveloperCommented:
Good to hear that. And I have no knowledge of either PHP or MySQL!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.