Jump to content

Need help updating a mysql record by adding an image with php

- - - - -

  • Please log in to reply
21 replies to this topic

#1
whitestar

whitestar

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
Hello All

I hope you can help me.
I have followed the tutorials:-Tutorial: Storing Images in MySQL with PHP& Tutorial: Storing Images in MySQL with PHP / Part II / Display your images . andI am able to add the image to the database
I have a database"the_secrets_out with a table"subscriber_info" where all the websites subscribers info is stored like first_name, last_name, username, password etc. I have a field called subscriber_id(primary key) and 1 called subscriber_image.

Once people have subscribed to the website I want to allow them to add a profile picture into "subscriber_image". to be added to their record.

I understand that their record needs to be updated with the image being placed in it and this tutorial only adds a new record. How do I change the script to update the record instead of insert a new one?

I am new to php and am using dreamweaver if that helps.

Your assistance is greatly appreciated

#2
LuthfiHakim

LuthfiHakim

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 765 posts
From the sample code in http://forum.codecal...-mysql-php.html, change the following lines:

      $query = "INSERT INTO tbl_images ";

      $query .= "(image) VALUES ('$data')";


into something like:

      $query = "UPDATE subscriber_info ";

      $query .= "SET subscriber_image='$data' ";

      $query .= "WHERE subscriber_id=xxx";


Note that you must replace the "xxx" with correct subscriber_id value matches the one whose image you want to replace.

#3
whitestar

whitestar

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
thanks alot I will try that!

#4
whitestar

whitestar

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
I tried it and Im getting the Message on the page "Thank you your image has been uploaded" but its not going into the database!
I have a feeling that you are very close though!

#5
LuthfiHakim

LuthfiHakim

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 765 posts
If you are sure that the query has been executed correctly, then the only problem is whether you had replaced the "xxx" with correct value. If it's not found in the database (field with subscriber_id of xxx) then the command will do nothing. You can test by using a constant of existing subscriber_id. For example, if you know that a subscriber with subscriber_id of 1 does exist in the database then try this code:


      $query = "UPDATE subscriber_info ";

      $query .= "SET subscriber_image='$data' ";

      $query .= "WHERE subscriber_id=1";


After executed, try to inspect the image.

#6
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
addslashes() or similar should be added around the blob variable, also double check what you are changing, check affected rows (along with number).

MySQL also has LOAD_DATA_INFILE or even faster would be to store the image as a separate resource and only store the path, there is not much to be gained by storing the image as a binary object in a database. Atleast in most circumstances.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#7
LuthfiHakim

LuthfiHakim

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 765 posts
Thanks Alexander for clarification. So Whitestar, how is it going?

#8
whitestar

whitestar

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
Thank You Both
And you were Right Luthfihakim. I dont know what I was doing wrong but it is working now! The next issue I have Is how do I send the subscriber_id from the add.html page?
I have a record set called subscriber_view that gets the subscriber_id from the prievious page so I have added in this to the form on the add.html page

<input type="hidden" name="MAX_FILE_SIZE" value="102400" >
<input name="subscriber_image" type="file" class="style1" accept="image/jpeg" />
<input type="hidden" name="subscriber_id" value="<?php echo $row_subscriber_view['subscriber_id']; ?>" />
<input type="submit" value="Submit" >

and Ive add this on the insert.php page

$query = "UPDATE subscriber_info ";
$query .= "SET subscriber_image='$data' ";
$query .= "WHERE subscriber_id=<?php echo $row_subscriber_view['subscriber_id']; ?>";
$results = mysql_query($query, $link);

and when I submit it Im getting this error on the insert.php page.

Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in C:\xampp\htdocs\the_secrets_out\Insert.php on line 398

Which refers to this line

$query .= "WHERE subscriber_id=<?php echo $row_subscriber_view['subscriber_id']; ?>";

I did also try it with out the <?php echo $row_subscriber_view['subscriber_id']; ?> on the insert.php page and it doesnt look like the add.html page is sending the subscriber_id through?
Please help me with this

#9
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
I am not sure what you are trying to do, you are putting subscriber_id into a hidden field, but ignoring it and giving the query a PHP code which is invalid. You need to access it like this:
$query .= "WHERE subscriber_id=" . mysql_real_escape_string($_POST['subscriber_id']);
Just note you are grabbing an SQL argument from an HTML field, an end user can modify subscriber_id to anything they wish and the query will run with it.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#10
whitestar

whitestar

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
I am very new to this kind of thing so I dont know all the ins and outs.
I did however try your code and it is still giving me the same error!
can you advise me of a better way to do this?

#11
whitestar

whitestar

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
Thank you So much that did the Trick!!!
You Are A Machine!!!!
I am self taught on all this but it works Im so glad!!!!!

#12
LuthfiHakim

LuthfiHakim

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 765 posts
If alex is a machine, then I believe he is a coffeemaker. :c-laugh:

Congrats whitestar for solving your problem!!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users