kenoli Posted February 21, 2021 Share Posted February 21, 2021 Stuck again on simple code. I am trying to insert some fields extracted from one table into another. I'm using code that worked elsewhere. The SQL statement flies, the script runs, the input array is printed back I get an echo back from the end of the script but nothing is added to the table. Even aded an echo print_r in the conditional and I know the data is getting to the execute command. The script follows with a sample of the input array. I have attached am image of the table I am trying to insert the data into. --Kenoli The script: <?php require '__classes/DB.php'; $sql = "SELECT name, table_id, image_name, description, medium FROM tbl_person_data "; $stmt = $pdo->query($sql); $array1 = $stmt->fetchall(PDO::FETCH_ASSOC); $stmt = $pdo->prepare("INSERT INTO Images (name, person_id, filename, description, medium) VALUES (?,?,?,?,?)"); //$pdo->beginTransaction(); foreach ($array1 as $row) { $stmt->execute($row); } echo "<pre>"; print_r ($row); echo "</pre>"; echo '<h4>Got to end of file</h4>'; ?> $array1: The input array [0] => Array ( [name] => Carol Lettko [table_id] => 21 [image_name] => Carol_Lettko-DSC_3022.jpg [description] => Baby Herons/Brickyard [medium] => photo ) [1] => Array ( [name] => [table_id] => 22 [image_name] => Carol_Lettko-DSC_0164.JPG [description] => Heron/Brickyard [medium] => photo ) [2] => Array ( [name] => [table_id] => 23 [image_name] => Carol_Lettko-IMG_5723.jpg [description] => Kayaker/Brickyard [medium] => photo ) Quote Link to comment https://forums.phpfreaks.com/topic/312191-not-inserting-data-in-table/ Share on other sites More sharing options...
Solution Barand Posted February 21, 2021 Solution Share Posted February 21, 2021 You can do with a single INSERT SELECT statement. INSERT INTO Images (name, person_id, filename, description, medium) SELECT name , table_id , image_name , description , medium FROM tbl_person_data; Doing it with a prepared statement, as you are, requires either execute with a numerically indexed array and ? as placeholders, or execute with an associative array and named placeholders matching the array keys Quote Link to comment https://forums.phpfreaks.com/topic/312191-not-inserting-data-in-table/#findComment-1584668 Share on other sites More sharing options...
kenoli Posted February 21, 2021 Author Share Posted February 21, 2021 (edited) 2 hours ago, Barand said: You can do with a single INSERT SELECT statement. INSERT INTO Images (name, person_id, filename, description, medium) SELECT name , table_id , image_name , description , medium FROM tbl_person_data; Doing it with a prepared statement, as you are, requires either execute with a numerically indexed array and ? as placeholders, or execute with an associative array and named placeholders matching the array keys I'm not sure how to implement this. When I try, it just pulls out one record and inserts it in the new table. I have 227 rows I want it to do this to. Not sure how to construct a statement to do this. I was doing it with prepared statements using "?" placeholders as above and thought it should work, but it doesn't. I runs, throws no errors but inserts nothing into the table. --Kenoli Edited February 21, 2021 by kenoli Quote Link to comment https://forums.phpfreaks.com/topic/312191-not-inserting-data-in-table/#findComment-1584669 Share on other sites More sharing options...
kenoli Posted February 21, 2021 Author Share Posted February 21, 2021 Actually, I figured it out. Thanks, --Kenoli Quote Link to comment https://forums.phpfreaks.com/topic/312191-not-inserting-data-in-table/#findComment-1584670 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.