Jump to content

Please help - Write To SQL Within While Statement


- - NC - -

Recommended Posts

Hi everybody!

 

First post, of many. I am new to all this so please bare with me if my code is a little lame  :-\

 

I have this code which takes a url from my sql database, extracts the filename from the url, copies the image from the url to the local server and names it with the extracted filename:

 

$result = mysql_query("SELECT url_info FROM table1");

while($row = mysql_fetch_array( $result )) {

 

$url = $row[File_url];

$file_name = basename($url);

echo "<img src=\"".$row[File_url]."\" />";

copy($row[File_url], "images/$file_name");

 

}

 

This works fine, but I also need to copy the file name for each url to another table. I have this code, which is wrong because it inserts too many records. it takes $file_name from the code above.

 

 

mysql_query("INSERT INTO table2 (idcode, listingsimages_file_name, listingsdb_id, listingsimages_rank, listingsimages_active)

SELECT '2', '$file_name', ID, File_index, 'yes' FROM table1;");

 

example:

 

8 images, creates 64 records

id1  image1

id1  image2

id1  image3

id1  image4

id1  image5

id1  image6

id1  image7

id1  image8

id2  image1

id2  image2

id2  image3

etc........

 

when all I want is:

 

id1  image1

id2  image2

id3  image3

etc...........

 

I'm sure there is a simple (yet unknown to me) explanation here.

 

Please help!!!

 

Thanks

 

Nick

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.