RRO Posted December 30, 2021 Share Posted December 30, 2021 (edited) Hi there, I'm trying to move a mysql item from one to another db tabel. It worked but after I worked further it stopt working... The delete part works. (Its my first php project) Did I make some mistakes in the code below? if ($_POST['mode'] === 'GO') { mysqli_query($conn, "INSERT INTO a SELECT * FROM b WHERE id='" . $_POST["id"] . "'"); mysqli_query($conn, "DELETE FROM b WHERE id='" . $_POST["id"] . "'"); echo json_encode(true); } Edited December 30, 2021 by RRO Quote Link to comment https://forums.phpfreaks.com/topic/314369-%C2%A0-mysqli-insert-into-from-tabel-not-working/ Share on other sites More sharing options...
Solution Barand Posted December 30, 2021 Solution Share Posted December 30, 2021 Have you tried using mysqil's error reporting to find the reason? Quote Link to comment https://forums.phpfreaks.com/topic/314369-%C2%A0-mysqli-insert-into-from-tabel-not-working/#findComment-1593075 Share on other sites More sharing options...
ginerjm Posted December 30, 2021 Share Posted December 30, 2021 Try this to see if there are some issues: $q = "INSERT INTO a SELECT * FROM b WHERE id='" . $_POST["id"] . "'"; if (mysqli_query($conn, $q)) echo "Insert query successful"; else { echo "Insert query failed. Query is<br>$q<br>"; exit(); } $q = "DELETE FROM b WHERE id='" . $_POST["id"] . "'"; if (mysqli_query($conn, $q)) echo "Delete query successful"; else { echo "Delete query failed. Query is<br>$q<br>"; exit(); } echo json_encode(true); // Not sure what this is. If you get a failure try adding the mysqli error reporting function to it as already suggested. This will give you at least some indication. Suggestion - IMHO it is good practice to NOT bury a query statement inside of another function as you are doing. By assigning it to a variable it makes it easy to echo out your query string should you want to analyze it during any debugging you may need to do as in this case perhaps. Quote Link to comment https://forums.phpfreaks.com/topic/314369-%C2%A0-mysqli-insert-into-from-tabel-not-working/#findComment-1593077 Share on other sites More sharing options...
ginerjm Posted December 30, 2021 Share Posted December 30, 2021 Did a quick (Really quick!) lookup and found this. You have the wrong syntax so apparently you did not do your homework. INSERT INTO destination_table_name(column_1, column_2) SELECT column_1,column_2 FROM source_table WHERE condition; Quote Link to comment https://forums.phpfreaks.com/topic/314369-%C2%A0-mysqli-insert-into-from-tabel-not-working/#findComment-1593078 Share on other sites More sharing options...
RRO Posted December 30, 2021 Author Share Posted December 30, 2021 used the error report function. (I didn't know) error: Column count doesn't match value count at row 1. made the table structures the same and now it works!! Thanks for the reactions!! Quote Link to comment https://forums.phpfreaks.com/topic/314369-%C2%A0-mysqli-insert-into-from-tabel-not-working/#findComment-1593079 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.