Jump to content

Not inserting data in table


kenoli
Go to solution Solved by Barand,

Recommended Posts

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
        )

 

table.png

Link to comment
Share on other sites

  • Solution

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
Link to comment
Share on other sites

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 by kenoli
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.