1136 Column count doesn’t match value count at row 1

I’ve created a php mysql driven web site. I’ve specified a primary and foreign key. The gallery table has an id and the images table has an id. There can be one gallery to many images: gallery table has a primary key and images table has a foreign key— they’re joined. The script below adds data to the images table, but the images are supposed to linked to a specific gallery. I’m doing an insert join in the script below. I keep getting his error message: Insert value list does not match column list: 1136 Column count doesn’t match value count at row 1. Thank you in advance for the help.

Code:

<?php require (“common.php”); $g_id = $_POST[‘g_id’]; $query = " INSERT INTO images ( p_id, p_name, p_title, p_desc, g_id ) SELECT gallery.g_id FROM gallery Inner JOIN images on images.g_id = gallery.g_id; "; try { $stmt = $db->prepare($query); $result = $stmt->execute(); } catch(PDOException $ex) { die("Oh, No something went wrong. Please contact your systems administrator… " . $ex->getMessage()); } ?>

An INSERT … SELECT query is used when you want to insert data into a table from other table(s). For what you are apparently doing, inserting data from a form submission, there’s no point in using this type of query most of the time (if you need to limit the number of rows you can do this in a more complicated INSERT … SELECT query to do the insert and limiting in a single query.) You need to just produce and execute an INSERT query.

You would write a SELECT … query JOINing the tables when you want to retrieve related data from the gallery and images tables.

I’ve created a php mysql driven web site. I’ve specified a primary and foreign key. The gallery table has an id and the images table has an id. There can be one gallery to many images: gallery table has a primary key and images table has a foreign key— they’re joined. The script below adds data to the images table, but the images are supposed to linked to a specific gallery. I’m doing an insert join in the script below. I keep getting his error message: Insert value list does not match column list: 1136 Column count doesn’t match value count at row 1. Thank you in advance for the help.

Code:

<?php
require ("common.php");

$g_id = $_POST['g_id'];

$query = "
INSERT INTO images (
                p_id,
                p_name, 
                p_title,  
                p_desc,
		g_id
				)
SELECT gallery.g_id
FROM gallery
Inner JOIN images on images.g_id = gallery.g_id;

         
		 ";


try
{    
        $stmt = $db->prepare($query);
        $result = $stmt->execute();
		}
    catch(PDOException $ex)
    {
        
        die("Oh, No something went wrong. Please contact your systems administrator... " . $ex->getMessage());
    }

?>

It’s simple.

You are trying to insert these,
p_id, p_name, p_title, p_desc, g_id BUT only selecting this gallery.g_id. So, your count is not correct.

Merging topics. We don’t need two.

The gallery will have information about the images. The images gallery will have images associated with a gallery by id. The gallery should have the same id as the gallery. That’s why I used a foreign key and primary key linking the two together – 1 to many there can be 1 gallery but many images. How do I insert data so that the primary and foreign key have the same id linking gallery with the correct images associated with the gallery? Thank you for the help in advance.

Who or what determines which gallery an image gets associated with?

Does the person uploading the image select from the existing galleries?

Does someone review the uploaded images and assign them to one of the existing galleries or create a new gallery if needed?

Do the images get automatically assigned to one of the galleries based on some characteristic of the image, such as alphabetically using the filename, title, or description, or the date the image was taken or uploaded?

Do the images get randomly assigned to a gallery?

The gallery has a primary key of g_id. The images gallery has a foreign key of g_id. When a photo gets added data about that photo should be inserted into the images table. The images table should have the same g_id (foreign key) data as the primary key g_id has in gallery. That’s what links the images to that gallery. So, when you click on the specific gallery the images show up that are associated with that gallery ID. This should be done automatically once data has been added to images table. I’m having an issue with the syntax. How do you add data from a primary key in a foreign key field automatically? Thank you for the help in advance.

Sponsor our Newsletter | Privacy Policy | Terms of Service