oneumbre
oneumbre

Reputation: 13

php editing entry with uploaded image

just a basic one I can imagine, but to me I'm struggling :)

I have created this testimonial script, works fine apart from when I go to update an entry. All the data is there, which has been submitted apart from the images that have been uploaded for the entry.

I am using EzSql and Mysql.

The part of the code which isn't working correctly:

<? if($_GET['action'] != 'edit') { ?>
    Add a new testimonial:  
<? }else{ ?>
    Update testimonial: 
<?
        $sql = "SELECT * from testimonials where id=".$_GET['tid'];
        $testimonialsData = $dbEze->get_row($sql);

        $update_company = $testimonialsData->company;
        $update_company_url = $testimonialsData->company_url;
        $update_content = $testimonialsData->content;
        $update_contentby_name = $testimonialsData->contentby_name;
        $update_contentby_position = $testimonialsData->contentby_position;
        $update_contentby_rating = $testimonialsData->rating;
        $update_logo = $testimonialsData->logo;
        $update_image = $testimonialsData->image;
        $update_twitter = $testimonialsData->twitter;

    } ?>

<form  enctype="multipart/form-data" action="testadmin?action=insert" method="post">
    <p>Company:
<input type="text" name="company" size="20" value="<?=$update_company?>" />

      Website: 
      <input type="text" name="company_url" size="20" value="<?=$update_company_url?>" />
      <br />
      <br>
      Name: 
      <input type="text" name="contentby_name" size="20" value="<?=$update_contentby_name?>" />
       Job Title: 
       <input type="text" name="contentby_position" size="20" value="<?=$update_contentby_position?>" />
      <br />
      <br>
      Rating: <input type="text" name="rating" size="20" value="<?=$update_contentby_rating?>" />
      Twitter ID: <input type="text" name="twitter" size="20" value="<?=$update_twitter?>" />
      <br />
      <br />
       <br>
      Testimonial: 
      <textarea name="content" rows="4" cols="35"><?=$update_content?>
    </textarea>
  </p>
    <p>Company Logo:
<input name="logo" type="file" size="20" value="<?=$update_logo?>" /> 
      <br />
      Product Photo:
<input name="image" type="file" size="20" value="<?=$update_logo?>" />
  </p>
    <p>

      <? if($_GET['action'] == 'insert' || $_GET['action'] == 'delete') { ?>
      <input type="hidden" name="insert" value="1" />
      <a onclick="jQuery(this).closest('form').submit()" class="btn add">Add <span class="helper"></span></a>
      <? }
    if($_GET['action'] == 'edit') {  ?>
      <input type="hidden" name="update" value="<?=$_GET['tid']?>" />
      <input type="submit" value="Update"/> 
      <input type="button" value="Cancel" onclick="window.location='testadmin?action=insert'" />
  </p>  
    <?  } ?>
</form>

<br>
<br>
<br><?php $dbEze->debug(); ?>


<table width="927" border="0" cellpadding="5" style="table-layout:fixed; width: 900px; word-wrap:break-word; border-spacing: 5px">
    <tr>
      <td width="85" bgcolor="#CCCCCC">Company</td>
      <td width="73" bgcolor="#CCCCCC">Website</td>
      <td width="97" bgcolor="#CCCCCC">Testimonial</td>
      <td width="55" bgcolor="#CCCCCC">Name</td>
      <td width="76" bgcolor="#CCCCCC">Job Title</td>
      <td width="56" bgcolor="#CCCCCC">Rating</td>
      <td width="48" bgcolor="#CCCCCC">Logo</td>
      <td width="125" bgcolor="#CCCCCC">Product Photo</td>
      <td width="91" bgcolor="#CCCCCC">Twitter</td>
      <td width="99" bgcolor="#CCCCCC"></td>
    </tr>
    <?
    $sql = "SELECT * from testimonials";
    $testimonialsData = $dbEze->get_results($sql);
    foreach ($testimonialsData as $testimonial){

    $logo_cutup = explode('/', $testimonial->logo);
    $logo_lastword = $logo_cutup[(count($logo_cutup))-1];

    $image_cutup = explode('/', $testimonial->image);
    $image_lastword = $image_cutup[(count($image_cutup))-1];
    ?>
    <tr>
      <td><?=$testimonial->company?></td>
      <td><?=$testimonial->company_url?></td>
      <td><?=$testimonial->content?></td>
      <td><?=$testimonial->contentby_name?></td>
      <td><?=$testimonial->contentby_position?></td>
      <td><?=$testimonial->rating?></td>
      <td><a href="<?=$testimonial->logo?>"><?=$logo_lastword?></a></td>
      <td><a href="<?=$testimonial->image?>"><?=$image_lastword?></a></td>
      <td><?=$testimonial->twitter?></td>
      <td>
          <a href="testadmin?action=edit&tid=<?=$testimonial->id?>" class="btn edit">Update <span class="helper"></span></a>
          <br>
          <a href="testadmin?action=delete&tid=<?=$testimonial->id?>" class="btn delete" onClick="return confirm('Are you 100% totally certain that you want to DELETE this testimonial?')">Delete <span class="helper"></span></a>

Any advice would be great!

Upvotes: 0

Views: 1849

Answers (1)

Joe Green
Joe Green

Reputation: 1777

It looks like you're trying to populate a form from the database. Which is cool. Unfortunately it also looks like you're trying to assign a value to a the input type="file" elements, which is not so cool.

What are these values you are trying to pass? If you are trying to assign a relative URL it certainly won't work. If you are trying to assign an absolute URL I'm not so sure that will work either.

Usually in forms like these you would fill in the form with a preview of the existing image and let the user click a button to replace the image or add a new one. Upon clicking the button you would show a new file input. Validate everything on the php side.

Upvotes: 1

Related Questions