lingo5 Posted December 21, 2009 Share Posted December 21, 2009 Hi, I have a select menu that shows a list of companies from a MySQL table and posts the company ID to a PHP page. This is the query that gets the data for the select: // Firstly Lets build the Category List $result = mysql_query( "SELECT id_A,A_nombre FROM t_atracciones ORDER BY A_nombre" ); while( $row = mysql_fetch_array( $result ) ) { $photo_category_list .=<<<__HTML_END <option value="$row[0]">$row[1]</option>\n __HTML_END; } mysql_free_result( $result ); And this is the code that inserts the selection on the DB: mysql_query( "INSERT INTO t_gallery_photos(`photo_filename`, `photo_caption`, `id_A`) VALUES('0', '".addslashes($photo_caption[$counter])."', '".addslashes($_POST['category'])."')" ); $new_id = mysql_insert_id(); $filetype = $photos_uploaded['type'][$counter]; $extention = $known_photo_types[$filetype]; $filename = $new_id.".".$extention; mysql_query( "UPDATE t_gallery_photos SET photo_filename='".addslashes($filename)."' WHERE photo_id='".addslashes($new_id)."'" ); My problem is that I want the select mnu to pass 2 differents IDs, one eing the existing id_A and the other id_E, stored on the same table. I don't know how to do this and would like your hekp if possible. Thanks a lot . Link to comment https://forums.phpfreaks.com/topic/185892-help-with-select-menu/ Share on other sites More sharing options...
p2grace Posted December 21, 2009 Share Posted December 21, 2009 There are a few ways of doing this. The easiest way would probably to set the value of the option to be id_a:id_e and then use explode() to convert the string into an array where element 0 of the array is the id_a and element 1 of the array is id_e. Make sense? Link to comment https://forums.phpfreaks.com/topic/185892-help-with-select-menu/#findComment-981760 Share on other sites More sharing options...
lingo5 Posted December 21, 2009 Author Share Posted December 21, 2009 Hi p2grace, thanks for your reply but I am very new to PHP and MySQL and wouldn't know how to do that. Could you please use my existing code to explain it to me?. Thanks Link to comment https://forums.phpfreaks.com/topic/185892-help-with-select-menu/#findComment-981813 Share on other sites More sharing options...
p2grace Posted December 21, 2009 Share Posted December 21, 2009 Try this for the display: <?php // Firstly Lets build the Category List $result = mysql_query( "SELECT id_A,A_nombre,ID_E FROM t_atracciones ORDER BY A_nombre" ); while( $row = mysql_fetch_assoc($result)){ $photo_category_list .=<<<__HTML_END <option value="{$row['id_A']}:{$row['id_E']}">{$row['A_nombre']}</option>\n __HTML_END; } ?> And this for the insert <?php $id = mysql_real_escape_string(explode(":",$_POST['category'])); $id_A = $id[0]; $id_E = $id[1]; mysql_query( "INSERT INTO t_gallery_photos(`photo_filename`, `photo_caption`, `id_A`,`id_E`) VALUES('0', '".addslashes($photo_caption[$counter])."', '$id_A','$id_E)" ); $new_id = mysql_insert_id(); $filetype = $photos_uploaded['type'][$counter]; $extention = $known_photo_types[$filetype]; $filename = $new_id.".".$extention; mysql_query( "UPDATE t_gallery_photos SET photo_filename='".addslashes($filename)."' WHERE photo_id='".addslashes($new_id)."'" ); ?> Also note, that you shouldn't use addslashes here, but rather mysql_real_escape_string(). http://us2.php.net/manual/en/function.mysql-real-escape-string.php Link to comment https://forums.phpfreaks.com/topic/185892-help-with-select-menu/#findComment-981821 Share on other sites More sharing options...
lingo5 Posted December 22, 2009 Author Share Posted December 22, 2009 Thanks a lot p2grace, but having tested your code nothing gets added to the DB. Perhaps I should show you the whole code. I have this PHP page called preupload.php: <?php include("config.inc.php"); // initialization $photo_upload_fields = ""; $counter = 1; // default number of fields $number_of_fields = 6; // If you want more fields, then the call to this page should be like, // preupload.php?number_of_fields=20 if( $_GET['number_of_fields'] ) $number_of_fields = (int)($_GET['number_of_fields']); // Firstly Lets build the Category List $result = mysql_query( "SELECT id_A,A_nombre FROM t_atracciones ORDER BY A_nombre" ); while( $row = mysql_fetch_array( $result ) ) { $photo_category_list .=<<<__HTML_END <option value="$row[0]">$row[1]</option>\n __HTML_END; } mysql_free_result( $result ); // Lets build the Photo Uploading fields while( $counter <= $number_of_fields ) { $photo_upload_fields .=<<<__HTML_END <tr> <td> Photo {$counter}: <input name=' photo_filename[]' type='file' /> </td> </tr> <tr> <td> Caption: <textarea name='photo_caption[]' cols='30' rows='1'></textarea> </td> </tr> __HTML_END; $counter++; } // Final Output echo <<<__HTML_END <html> <head> <title>Lets upload Photos</title> </head> <body> <form enctype='multipart/form-data' action='upload.php' method='post' name='upload_form'> <table width='90%' border='0' align='center' style='width: 90%;'> <tr> <td> <select name='category'> <option value="">Seleccione una atracción</option> $photo_category_list </select> </td> </tr> <tr> <td> <p> </p> </td> </tr> <!-Insert the photo fields here --> $photo_upload_fields <tr> <td> <input type='submit' name='submit' value='Add Photos' /> </td> </tr> </table> </form> </body> </html> __HTML_END; ?> and another page named upload.php that ciollects the data from the previous page and sends it to the DB: <?php include("config.inc.php"); // initialization $result_final = ""; $counter = 0; // List of our known photo types $known_photo_types = array( 'image/pjpeg' => 'jpg', 'image/jpeg' => 'jpg', 'image/gif' => 'gif', 'image/bmp' => 'bmp', 'image/x-png' => 'png' ); // GD Function List $gd_function_suffix = array( 'image/pjpeg' => 'JPEG', 'image/jpeg' => 'JPEG', 'image/gif' => 'GIF', 'image/bmp' => 'WBMP', 'image/x-png' => 'PNG' ); // Fetch the photo array sent by preupload.php $photos_uploaded = $_FILES['photo_filename']; // Fetch the photo caption array $photo_caption = $_POST['photo_caption']; while( $counter <= count($photos_uploaded) ) { if($photos_uploaded['size'][$counter] > 0) { if(!array_key_exists($photos_uploaded['type'][$counter], $known_photo_types)) { $result_final .= "File ".($counter+1)." is not a photo<br />"; } else { mysql_query( "INSERT INTO t_gallery_photos(`photo_filename`, `photo_caption`, `id_A`) VALUES('0', '".addslashes($photo_caption[$counter])."', '".addslashes($_POST['category'])."')" ); $new_id = mysql_insert_id(); $filetype = $photos_uploaded['type'][$counter]; $extention = $known_photo_types[$filetype]; $filename = $new_id.".".$extention; mysql_query( "UPDATE t_gallery_photos SET photo_filename='".addslashes($filename)."' WHERE photo_id='".addslashes($new_id)."'" ); // Store the orignal file copy($photos_uploaded['tmp_name'][$counter], $images_dir."/".$filename); // Let's get the Thumbnail size $size = GetImageSize( $images_dir."/".$filename ); if($size[0] > $size[1]) { $thumbnail_width = 100; $thumbnail_height = (int)(100 * $size[1] / $size[0]); } else { $thumbnail_width = (int)(100 * $size[0] / $size[1]); $thumbnail_height = 100; } // Build Thumbnail with GD 1.x.x, you can use the other described methods too $function_suffix = $gd_function_suffix[$filetype]; $function_to_read = 'ImageCreateFrom' . $function_suffix; $function_to_write = 'Image' . $function_suffix; // Read the source file $source_handle = $function_to_read($images_dir . '/' . $filename); if ($source_handle) { // Let's create a blank image for the thumbnail $destination_handle = ImageCreateTrueColor($thumbnail_width, $thumbnail_height); // Now we resize it ImageCopyResampled($destination_handle, $source_handle, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $size[0], $size[1]); } // Let's save the thumbnail $function_to_write($destination_handle, $images_dir . '/tb_' . $filename); $result_final .= "<img src='".$images_dir. "/tb_".$filename."' /> File ".($counter+1)." Added<br />"; } } $counter++; } // Print Result echo <<<__HTML_END <html> <head> <title>Photos uploaded</title> </head> <body> $result_final </body> </html> __HTML_END; ?> I need to add id_E to preupload.php and send it to upload.php. As I said I trie the code you suggested but I got no result at all. Any suggestions? Many, many thanks for your help Link to comment https://forums.phpfreaks.com/topic/185892-help-with-select-menu/#findComment-982190 Share on other sites More sharing options...
p2grace Posted December 22, 2009 Share Posted December 22, 2009 Try this: preupload.php <?php include("config.inc.php"); // initialization $photo_upload_fields = ""; $counter = 1; // default number of fields $number_of_fields = 6; // If you want more fields, then the call to this page should be like, // preupload.php?number_of_fields=20 if( $_GET['number_of_fields'] ) $number_of_fields = (int)($_GET['number_of_fields']); // Firstly Lets build the Category List $result = mysql_query( "SELECT id_A,A_nombre,id_E FROM t_atracciones ORDER BY A_nombre" ); while( $row = mysql_fetch_array( $result ) ) { $photo_category_list .=<<<__HTML_END <option value="{$row[0]}:{$row[2]}">$row[1]</option>\n __HTML_END; } mysql_free_result( $result ); // Lets build the Photo Uploading fields while( $counter <= $number_of_fields ) { $photo_upload_fields .=<<<__HTML_END <tr> <td> Photo {$counter}: <input name=' photo_filename[]' type='file' /> </td> </tr> <tr> <td> Caption: <textarea name='photo_caption[]' cols='30' rows='1'></textarea> </td> </tr> __HTML_END; $counter++; } // Final Output echo <<<__HTML_END <html> <head> <title>Lets upload Photos</title> </head> <body> <form enctype='multipart/form-data' action='upload.php' method='post' name='upload_form'> <table width='90%' border='0' align='center' style='width: 90%;'> <tr> <td> <select name='category'> <option value="">Seleccione una atracción</option> $photo_category_list </select> </td> </tr> <tr> <td> <p> </p> </td> </tr> <!-Insert the photo fields here --> $photo_upload_fields <tr> <td> <input type='submit' name='submit' value='Add Photos' /> </td> </tr> </table> </form> </body> </html> __HTML_END; ?> upload.php <?php include("config.inc.php"); // initialization $result_final = ""; $counter = 0; // List of our known photo types $known_photo_types = array( 'image/pjpeg' => 'jpg', 'image/jpeg' => 'jpg', 'image/gif' => 'gif', 'image/bmp' => 'bmp', 'image/x-png' => 'png' ); // GD Function List $gd_function_suffix = array( 'image/pjpeg' => 'JPEG', 'image/jpeg' => 'JPEG', 'image/gif' => 'GIF', 'image/bmp' => 'WBMP', 'image/x-png' => 'PNG' ); // Fetch the photo array sent by preupload.php $photos_uploaded = $_FILES['photo_filename']; // Fetch the photo caption array $photo_caption = $_POST['photo_caption']; while( $counter <= count($photos_uploaded) ) { if($photos_uploaded['size'][$counter] > 0) { if(!array_key_exists($photos_uploaded['type'][$counter], $known_photo_types)) { $result_final .= "File ".($counter+1)." is not a photo<br />"; } else { $cat = explode(":",$_POST['category']); $id_A = $cat[0]; $id_E = $cat[1]; mysql_query( "INSERT INTO t_gallery_photos(`photo_filename`, `photo_caption`, `id_A`,`id_E`) VALUES('0', '".mysql_real_escape_string($photo_caption[$counter])."', '".mysql_real_escape_string($id_A)."', '".mysql_real_escape_string($id_E)."')" ); $new_id = mysql_insert_id(); $filetype = $photos_uploaded['type'][$counter]; $extention = $known_photo_types[$filetype]; $filename = $new_id.".".$extention; mysql_query( "UPDATE t_gallery_photos SET photo_filename='".mysql_real_escape_string($filename)."' WHERE photo_id='".mysql_real_escape_string($new_id)."'" ); // Store the orignal file copy($photos_uploaded['tmp_name'][$counter], $images_dir."/".$filename); // Let's get the Thumbnail size $size = GetImageSize( $images_dir."/".$filename ); if($size[0] > $size[1]) { $thumbnail_width = 100; $thumbnail_height = (int)(100 * $size[1] / $size[0]); } else { $thumbnail_width = (int)(100 * $size[0] / $size[1]); $thumbnail_height = 100; } // Build Thumbnail with GD 1.x.x, you can use the other described methods too $function_suffix = $gd_function_suffix[$filetype]; $function_to_read = 'ImageCreateFrom' . $function_suffix; $function_to_write = 'Image' . $function_suffix; // Read the source file $source_handle = $function_to_read($images_dir . '/' . $filename); if ($source_handle) { // Let's create a blank image for the thumbnail $destination_handle = ImageCreateTrueColor($thumbnail_width, $thumbnail_height); // Now we resize it ImageCopyResampled($destination_handle, $source_handle, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $size[0], $size[1]); } // Let's save the thumbnail $function_to_write($destination_handle, $images_dir . '/tb_' . $filename); $result_final .= "<img src='".$images_dir. "/tb_".$filename."' /> File ".($counter+1)." Added<br />"; } } $counter++; } // Print Result echo <<<__HTML_END <html> <head> <title>Photos uploaded</title> </head> <body> $result_final </body> </html> __HTML_END; ?> Link to comment https://forums.phpfreaks.com/topic/185892-help-with-select-menu/#findComment-982479 Share on other sites More sharing options...
lingo5 Posted December 22, 2009 Author Share Posted December 22, 2009 It works perfect!!!!! Thanks a lot. So could you please tell me what this does? <option value="{$row[0]}:{$row[2]}">$row[1]</option>\n Link to comment https://forums.phpfreaks.com/topic/185892-help-with-select-menu/#findComment-982528 Share on other sites More sharing options...
p2grace Posted December 22, 2009 Share Posted December 22, 2009 Basically I'm providing both ids separated by a colon. Then I use php's explode() method to convert that string into an array where the first element in the array is ID_A and the second element in the array is ID_E. Make sense? Link to comment https://forums.phpfreaks.com/topic/185892-help-with-select-menu/#findComment-982548 Share on other sites More sharing options...
lingo5 Posted December 22, 2009 Author Share Posted December 22, 2009 I can't say I understand that 100%...but I will keep on learning!!! Thanks very much for your help!!! Link to comment https://forums.phpfreaks.com/topic/185892-help-with-select-menu/#findComment-982560 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.