Jump to content

Array help


plastik77

Recommended Posts

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

?>

Link to comment
Share on other sites

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.

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.