plastik77 Posted July 25, 2007 Share Posted July 25, 2007 Hi - just posting this one again as i've made no progress and got no replies the last time - I've set up a form for uploading multiple images and I am writing the urls to a database, which is queried and outputs the images on a web page. This works fine, however, I also want to add alt/title text for each image upload but can't quite seem to get it working. I'm pretty new to PHP so my knowledge of how to use arrays is fairly limited. If anyone could offer any help that would be appreciated. Code is as follows. //html form <form method="post" action="addimgck.php" id="newsForm" enctype="multipart/form-data"> <label for="user">Title</label> <input type="text" name="title" value="" /> <label for="comments">Content:</label> <textarea name="story"></textarea> <p align="justify"> Upload images: <span id="alttext">Image Description</span> <input type="file" name="images[]" size="40"><input type="text" name="alt[]" value="" /> <input type="file" name="images[]" size="40"><input type="text" name="alt[]" value="" /> <input type="file" name="images[]" size="40"><input type="text" name="alt[]" value="" /> <input type="file" name="images[]" size="40"><input type="text" name="alt[]" value="" /> </p> <input type="submit" name="Submit" id="submitbutton" value="Submit" /> //addimgck.php script <?php include("functions.php"); dbconnect(); //get inputs $title = $_POST['title']; $story = $_POST['story']; //insert story into dbs $insert = mysql_query("insert into news (title,story) values ('$title','$story')"); if(!$insert) echo "insert problem"; $query = mysql_query("select id from news where title = '$title' and story = '$story'"); if(!query) echo "no data found"; else { $row = mysql_fetch_array($query); $id = $row['id']; } //insert corresponding images into dbs while(list($key,$value) = each($_FILES[images][name])) { if(!empty($value)) { $filename = $value; //$alt = $_POST[alt][name]; $add = "news/$filename"; //echo $_FILES[images][type][$key]; // echo " "; copy($_FILES[images][tmp_name][$key], $add); chmod("$add",0777); $insert_img = mysql_query("insert into image (url,alt,news_id) values ('$filename','$alt',$id) "); if(!insert_img) echo "didn't insert images"; } } header("location:newsbackup2.php"); // direct to news page ?> Quote Link to comment Share on other sites More sharing options...
akitchin Posted July 25, 2007 Share Posted July 25, 2007 specify a key for all of the corresponding inputs, such that no ambiguity occurs: <input type="file" name="images[1]" size="40"><input type="text" name="alt[1]" value="" /> when processing each image, use the key from its input name to obtain the corresponding alt text. Quote Link to comment Share on other sites More sharing options...
mpharo Posted July 25, 2007 Share Posted July 25, 2007 also be sure your PHP syntax is proper, I see 2 errors just at a glance.... if(!query) echo "no data found"; and... if(!insert_img) echo "didn't insert images"; Quote Link to comment 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.