Reputation: 103
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
Upvotes: 1
Views: 3059
Reputation: 157839
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'];
?>
Upvotes: 2