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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.