Hi!
I have a problem with the function UPDATE in sql. Posting and updating works fine except that I need to upload my image everytime when I want to update an, for an example, newsitem.
I know there is a way to make the already uploaded image to stay, but I don´t know how to do that.
Can anyone help me please? Thanks in advance!
Below is the code for the update-page.
<HTML>
<body bgcolor="#ADC5DE">
<FONT FACE="verdana" SIZE="1"><B>
<?php
if($submit)
{
if (is_uploaded_file($_FILES['userfile1']['tmp_name'])) {
$webpath = 'http://www.my-domain.com/images/';
$folder = '/my/full/path/images/';
$imagelocation = $webpath.$_FILES['userfile1']['name'];
move_uploaded_file($_FILES['userfile1']['tmp_name'], $folder.$_FILES['userfile1']['name']);
echo "<BR>";
echo "Bilden uppladdad.<BR>";
} else {
$imagelocation = '';
echo "Ingen bild uppladdad.<BR>";
}
// Koppla till DB
$db = mysql_connect("localhost", "username", "password"); mysql_select_db("my_db");
$sql = "INSERT INTO front (titel, text, imagelocation, sortorder)
VALUES ('$titel','$text','$imagelocation','$sortorder')";
$result = mysql_query($sql);
echo "Nyheten är publicerad.\n";
}
else if($update)
{
if (is_uploaded_file($_FILES['userfile1']['tmp_name'])) {
$webpath = 'http://www.my-domain.com/images/';
$folder = '/my/full/path/images/';
$imagelocation = $webpath.$_FILES['userfile1']['name'];
move_uploaded_file($_FILES['userfile1']['tmp_name'], $folder.$_FILES['userfile1']['name']);
echo "<BR>";
echo "Bilden uppladdad. ";
} else {
$imagelocation = '';
echo "Ingen bild uppladdad.<BR>";
}
$db = mysql_connect("localhost", "username", "password"); mysql_select_db("my_db");
$sql = "UPDATE front SET titel='$titel',
text='$text',imagelocation='$imagelocation',sortorder='$sortorder' WHERE id=$id";
$result = mysql_query($sql);
echo "Nyheten är uppdaterad.\n";
}
else if($id)
{
$db = mysql_connect("localhost", "username", "password"); mysql_select_db("my_db");
$result = mysql_query("SELECT * FROM front WHERE id=$id",$db);
$myrow = mysql_fetch_array($result);
?>
<form method="post" action="<?php echo $PHP_SELF?>" ENCTYPE="multipart/form-data">
<input type="hidden" name="id" value="<?php echo $myrow["id"]?>">
<FONT FACE="verdana" SIZE="1"><B>S/O:<BR> <input type="Text" size="3" name="sortorder" value="<?php echo $myrow["sortorder"]?>"><br>
<FONT FACE="verdana" SIZE="1"><B>Titel:<BR> <input type="Text" name="titel" value="<?php echo $myrow["titel"]?>"><br>
<FONT FACE="verdana" SIZE="1"><B>Brödtext:<BR> <textarea rows="10" name="text" cols="36"><?php echo $myrow["text"]?></textarea><br>
<b>Ladda upp ny bild:<BR></b>
<input type="file" name="userfile1" size="35">
<BR><input type="Submit" name="update" value="Uppdatera"></form>
<?
}
else
{
?>
<form method="post" action="<?php echo $PHP_SELF?>" ENCTYPE="multipart/form-data">
<FONT FACE="verdana" SIZE="1"><B>S/O:<BR> <input type="Text" size="3" name="sortorder" value="<?php echo $myrow["sortorder"]?>"><br>
<FONT FACE="verdana" SIZE="1"><B>Titel:<BR><input type="Text" name="titel"><br>
<FONT FACE="verdana" SIZE="1"><B>Brödtext:<BR><input type="Textarea" rows="10" cols="30" name="text"><br>
<?php
print("<input type=\"file\" name=\"userfile1\" size=\"35\">\n");
?>
<input type="Submit" name="submit" value="Enter information"></form>
<?
}
?>
</HTML>