tags:

views:

43

answers:

1

I have a simple table for reference page: id name description image

In reference.php, A form upload image to a folder and save image's name in image section. In reference.php?action=edit page I want to edit the image. What is correct way to edit? Uploading another image and update the table? Functions:

function editRef() {
    ?>
    <?php 
     $row = queryWithID('reference');
     EpUpload();
     ?>
    <div class="form">
        <form action="" method="post" enctype="multipart/form-data">
        <ul>
          <li><label>Name</label></li>
          <li><input name="refname" type="text" class="inp" value="<?php echo $row['name']; ?>" /></li>
          <li><label>Description</label></li>
          <li><textarea name="reftext" cols="" rows=""><?php echo $row['description']; ?></textarea></li>
          <li><label>Image</label></li>
          <li><input name="refile" type="file" /></li>
          <li><label>Sıra</label></li>
          <li><input name="reforder" type="text" class="inp"/></li>
          <li><input name="refsubmit" type="submit" value="Edit"  class="int"/></li>
    </ul>
    </form>
</div>
    <?php
}

function EpUpload() {
    $refsubmit = safe_mysql('refsubmit');
    $reftext     = safe_mysql('reftext');
    $refname     = safe_mysql('refname');
    $reforder    = safe_mysql('reforder');
    $refile    = $_FILES['refile']['name'];
    $tmp         = $_FILES['refile']['tmp_name'];
    $fileType  = $_FILES['refile']['type'];
    $path      = SITE_ROOT."uploads/images/";


    if($refsubmit){
    $require_fields = array("$reftext","$refname", "$reforder");
        if(checkBlank($require_fields)){
            echo "<p class='not'><span>Please fill all inputs!</span></p>";
        }
        else{
      move_uploaded_file($tmp, $path.$refile);
        $query = "UPDATE reference SET name = '$refname', order='$reforder' description = '$reftext', image = '$refile' WHERE id = $id ";
    $result = mysql_query($sql);
      if(mysql_affected_rows () == 1){
        echo "<p class='ok'><span>rBlah blah</span></p>";
    }
    else{
        echo mysql_error();
    }
        }
}
}

function queryWithID($table){
if(is_numeric($_GET['id'])){ $id = mysql_real_escape_string($_GET['id']);}
$sql    = "SELECT * FROM $table WHERE id= $id";
$result = mysql_query($sql);
$row    = mysql_fetch_array($result);
return $row ;
 }

Thanks

+2  A: 

yes, this way is correct.

Well, how I do it:
note this code:

if ($_FILES['userfile']['name'] AND !$_FILES['userfile']['error']) {
  move_uploaded_file($_FILES['userfile']['tmp_name'],$cfg['upload_path'].$id.".jpg");
}

it will move the file only if there was a file and no error.
Note 3 parts of this script.

Ahhh almost forgot it!
I do not save the original file name but use id for it.

<?
include 'cfg.php';
$table=$cfg['db_table'];
$data=array();
$pic='';
$fields=array('title','section','price','annot','visible');

if($_SERVER['REQUEST_METHOD']=='POST') { 
  if ($id=intval($_POST['id'])) {
    $query="UPDATE $table SET ".dbSet($fields)." WHERE id=$id";
  } else {
    $query="INSERT INTO $table SET ".dbSet($fields);
  }
  mysql_query($query) or die(mysql_error());
  if (!$id) {
    $id=mysql_insert_id();
  }
  if ($_FILES['userfile']['name'] AND !$_FILES['userfile']['error']) {
    move_uploaded_file($_FILES['userfile']['tmp_name'],$cfg['upload_path'].$id.".jpg");
  }
  header("Location: http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']); 
  exit; 
} 
include $cfg['tpl_header'];

if (!isset($_GET['id'])) { 
  $LIST=array();
  $query="SELECT * FROM $table"; 
  $res=mysql_query($query);
  while($row=mysql_fetch_assoc($res)) $LIST[]=$row;
?>
<br><a href="?id=0">Add item</a><br><br>
<? foreach ($LIST as $row): ?>
 <li><a href="?id=<?=$row['id']?>"><?=$row['title']?>...</a>
<? endforeach ?>
<?
} else { 
  if ($id=intval($_GET['id'])) {
    $query="SELECT * FROM $table WHERE id=$id"; 
    $res=mysql_query($query);
    $row=mysql_fetch_assoc($res);
    foreach ($row as $k => $v) $row[$k]=htmlspecialchars($v);
    if ($row['visible']) $row['visible']=" checked";
    if (is_readable($cfg['upload_path'].$id.".jpg")) $pic=$id.".jpg";
  } else {
    foreach ($fields as $k => $v) $row[$v]=''; 
  }

?>
<form method="POST" enctype="multipart/form-data">
<table border=0>
<tr><td>Name</td><td><input type="text" name="title" size="100" value="<?=$row['title']?>"></tr>
<tr><td>Price</td><td><input type="text" name="price" size="100" value="<?=$row['price']?>"></tr>
<tr><td>Descr</td><td><textarea rows="20" cols="80" name="annot"><?=$row['annot']?></textarea></tr>
<tr><td>Visible</td><td><input type="checkbox" name="visible" value="1" checked></tr>
</table>
<?if(isset($row['id'])):?> <input type="hidden" name="id" value="<?=$row['id']?>"><?endif?>
Picture:<input name="userfile" type="file" /><br>
<input type="submit">
<br><br>

<a href="?">Back to list</a>
</form>
<? if($pic): ?>
<img src="img/<?=$pic?>">
<? endif ?>
<? 
  } 
include $cfg['tpl_footer'];

?>
Col. Shrapnel
But how can I show value in file input? It doesnot show anything<input name="refile" type="file" value="<?php echo $row['image']; ?>" />
Felicita
@Felicita Yes it doesn't. But why? You don't need it. To leave old image in the database just leave this field blank.
Col. Shrapnel
error: Query was empty :(
Felicita
@Felicita are you asking for your own code or for the some third-party code? If first - just add a code to handle empty field
Col. Shrapnel
@Col. Shrapnel, I have edited my question
Felicita
@Felicita that's quite strange code, it does show the form and run it's handler simultaneously. But it must be separate processes. I can't see "query was empty" error in this code
Col. Shrapnel
what is wrong and strange inmy code? Can you help me about correction my code please? Or any example for this?
Felicita
@Felicita well I've edited my answer as well :) It is not full working code as it use some own functions, but can show you the idea.
Col. Shrapnel
Thank you very much...
Felicita
@Felicita feel free to ask if got any problems with this code
Col. Shrapnel