Link to home
Start Free TrialLog in
Avatar of Yaku Kakashi
Yaku Kakashi

asked on

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.
Avatar of Nitin Sontakke
Nitin Sontakke
Flag of India image

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?
Avatar of Yaku Kakashi
Yaku Kakashi

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Nitin Sontakke
Nitin Sontakke
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Thank You so much Nitin Sontakke. You solve my problem for only a minute.
Good to hear that. And I have no knowledge of either PHP or MySQL!