ttmt Posted January 25, 2009 Share Posted January 25, 2009 Hi all I'm working on a photo gallery where images are group under sections. There are 7 sections but one image can be connected to more than one section. I have 3 tables in my DB 1. keywords(which is the sections). 2. photos tables 3. keyword_photo - table that connects the keywords to the sections. I'm using this at the moment to test - Sorry it's connected to the DB, I'm using MAMP locally. http://www.ttmt.org.uk/test/test.html So I can enter a name (which will be the uploaded image) and then choose one or more sections to put the image under I'm using this to insert the info into the DB <?php require_once("includes/connection.php"); require_once("includes/functions.php"); ?> <?php foreach($_POST as $key => $value){ $$key = $value; } // $charid = ""; // foreach($title as $input){ $query = "INSERT INTO photos (filename) VALUES ('$input')"; $result = mysql_query($query); confirm_query($result); if($result){ $charid = mysql_insert_id(); } } if($section != ""){ $val = ""; foreach($section as $key => $id){ $val[] = "('$charid','$id')"; } $values = implode(',',$val); $query = "INSERT INTO keyword_photo (photo_id, key_id) VALUES $values"; $result = mysql_query($query); confirm_query($result); } if($result){ echo "Done"; } ?> Everything works fine when I upload one image, If the image has more than one section selected the joins table shows the connection as it should. My problem is getting it to work with more than one image uploaded. If I try it with two images (they're just title at the moment) it's only the last one that is entered to the DB. I've been looking at this all day and my heads starting to hurt so any help would be greatly appreicated. ttmt Link to comment https://forums.phpfreaks.com/topic/142372-multi-inserts-into-two-tables/ Share on other sites More sharing options...
ttmt Posted January 26, 2009 Author Share Posted January 26, 2009 Does anyone have any ideas on this. I'm really stuck ! Link to comment https://forums.phpfreaks.com/topic/142372-multi-inserts-into-two-tables/#findComment-746427 Share on other sites More sharing options...
ttmt Posted January 26, 2009 Author Share Posted January 26, 2009 I think I've worked out the problem here but I don't have a solution. I think the problem is I have 2 $_POST arrays 1. titles 2. sections For each element in the titles array there could be more than one element in the section array connected to it. So my solution if it can be done would be to have a multi-diemensional array for the $_POST['section'] so that way I could find title[1] and the relating info in section[0][1] Or is it possible to create an array for each $_POST['section']. So this is the php I'm using to generate the form <?php require_once("includes/connection.php"); require_once("includes/functions.php"); include("includes/header.php"); ?> <?php $query = "SELECT key_id, words FROM keywords"; $result = mysql_query($query); confirm_query($result); $options = ""; while($row = mysql_fetch_array($result)){ $options .= "{$row[1]}:<input type=\"checkbox\" value=".$row[0]." name=\"section[]\" />\n"; } $number_of_uploads = 3; ?> <body> <div id="con"> <form name="upload_form" action="upload.php" method="post" enctype="multipart/form-data"> <?php for($counter = 1;$counter<=$number_of_uploads;$counter++){ ?> <p> <b>Image:</b> <textarea name="title[]" cols="30" rows="1"></textarea> </p> <p> <b>Sections:</b> <?php echo $options ;?> </p> <?php } ?> <br/> <p> <input type="submit" name="submit" value="Add Photos" /> </p> </form> </div> </body> </html> Is it possible here to name each array different on each loop of the while loop while($row = mysql_fetch_array($result)){ $options .= "{$row[1]}:<input type=\"checkbox\" value=".$row[0]." name=\"section.'{$row[0]}'.[]\" />\n"; } Link to comment https://forums.phpfreaks.com/topic/142372-multi-inserts-into-two-tables/#findComment-746476 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.