Jump to content

Recommended Posts

This is to upload an image then write the link to the database.  It does upload the image but does not write the link to the database. 

Any help would be great...

 


<?php
// Connect database.

// ***** This part will process when you Click on "Submit" button *****
// Check, if you clicked "Submit" button
if($_GET['page'] == "update")
{

$id=$_GET['id'];
$url=$_POST['url'];
$title=$_POST['title'];



$photo=$_FILES['photo']['name']; // < --- this line

// Do update statement.
mysql_query("update ads set photo='$photo', url='$url', title='$title' where id='$id'") or die(mysql_error());


}
// ************* End update part *************

// *** Select data to show on text fields in form. ***

// Get id parameter (GET method)
$id=$_GET['id'];

// Get records in all columns from table where column id equal in $id and put it in $result.
$result=mysql_query("select * from ads where id='$id'");

// Split records in $result by table rows and put them in $row.
$row=mysql_fetch_assoc($result);
?>

<?php
// Close database connection.
mysql_close();
?>
<?php 
//This is the directory where images will be saved
$target = "http://www.entertainfortwayne.com/images/ads/"; $target = $target . basename( $_FILES['photo']['name']); 

//This gets all the other information from the form 
$pic=($_FILES['photo']['name']); 

//Writes the photo to the server 
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))  ?> 


<!-- END OF PHP CODES AND START HTML TAGS -->

<html>
<style type="text/css">
<!--
.style2 {font-size: 12px}
-->
</style>
<body>
<br>
<br>

<!-- set this form to POST method and target this form to itself ($PHP_SELF;)-->

<form enctype="multipart/form-data" id="form1" name="form1" method="post"  action="?id=<? echo $id; ?>&page=update">

<table width="707" border="0" cellpadding="3" cellspacing="0" class="appfields">
  
  <tr>
    <td><p> </p>
      <p><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14"><strong><?php echo "<img src=http://www.entertainfortwayne.com/images/ads/".$row['photo'] ."> "; ?></strong></span></p>
      <p>
        <label><span class="style2">ALT/TITLE</span>
        <input type="text" name="title" id="title" value="<?php echo $row['title']  ?>">
        </label>
      </p>
      <p><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14"><strong>
        <label>        </label>
        </strong><span class="style2">HTTP://</span><strong>
        <label>
        <input type="text" name="url" id="url" Value="<?php echo $row['url']  ?>">
        </label>
        <br>
        <br>
        </strong></span><strong>
          <input type="file" name="photo" />
            </strong>
        <input type="submit" name="Submit" value="Submit" />
      Image needs to be 200px wide</p></td>
    </tr>
</table>
<p> </p>
<p> </p>
</form>
</body>
</html>

changed it to this is it is really weird but what happens say I upload an image called cans.jpg .... it does not put it in the image folder but in the root and is displayed as imagescans.jpg it does write it to the database correctly.

 

<?php
if($_GET['page'] == "update")
{

$id=$_GET['id'];
$url=$_POST['url'];
$title=$_POST['title'];



$photo=$_FILES['photo']['name']; // < --- this line

// Do update statement.
mysql_query("update ads set photo='$photo', url='$url', title='$title' where id='$id'") or die(mysql_error());


}
// ************* End update part *************

// *** Select data to show on text fields in form. ***

// Get id parameter (GET method)
$id=$_GET['id'];

// Get records in all columns from table where column id equal in $id and put it in $result.
$result=mysql_query("select * from ads where id='$id'");

// Split records in $result by table rows and put them in $row.
$row=mysql_fetch_assoc($result);
?>

<?php
// Close database connection.
mysql_close();
?>
<?php 
//This is the directory where images will be saved
$target = "images"; $target = $target . basename( $_FILES['photo']['name']); 

//This gets all the other information from the form 
$pic=($_FILES['photo']['name']); 

//Writes the photo to the server 
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))  ?> 

<?php
//connect to database
if($_GET['page'] == "update")
{
   $id    = (int)$_GET['id'];
   $title = $_POST['title'];
   $photo = $_FILES['photo']['name'];

   //This is the directory where images will be saved
   $target    = "images"; 
   $target    = $target . DIRECTORY_SEPARATOR . $photo; 
   $urlTarget = $target . '/' . $photo;
   //move file from temp location to target location
   if( move_uploaded_file($_FILES['photo']['tmp_name'], $target) )
   {
      $url = 'http://' . $_SERVER['SERVER_NAME'] . $urlTarget;

      // Do update statement.
      mysql_query("update ads set photo='$photo', url='$url', title='" . mysql_real_escape_string($title) . "' where id=$id") or die(mysql_error());
   }

}

// Close database connection.
mysql_close();
?>

I need you to post the FULL code of the page, I don't really understand what you're trying to do atm but I know that what you posted is only a part of the code. I can't understand why your upload logic is spread throught various code blocks.

Andy-H I am simply trying to write an upload script that uploads the image to the folder images and then updates the database with the name of the image... Here is the complete script I have now.

 

<?php
// Connect database.
$host=""; // Host name.
$db_user=""; // MySQL username.
$db_password=""; // MySQL password.
$database=""; // Database name.
mysql_connect($host,$db_user,$db_password);
mysql_select_db($database);
// ***** This part will process when you Click on "Submit" button *****
// Check, if you clicked "Submit" button
if($_GET['page'] == "update")
{
   $id    = (int)$_GET['id'];
   $title = $_POST['title'];
   $photo = $_FILES['photo']['name'];

   //This is the directory where images will be saved
   $target   = "images"; 
   $target   = $target . DIRECTORY_SEPARATOR . $photo; 
   $urlTarget = $target . '/' . $photo;
   //move file from temp location to target location
   if( move_uploaded_file($_FILES['photo']['tmp_name'], $target) )
   {
      $url = 'http://' . $_SERVER['SERVER_NAME'] . $urlTarget;

      // Do update statement.
      mysql_query("update ads set photo='$photo', url='$url', title='" . mysql_real_escape_string($title) . "' where id=$id") or die(mysql_error());

}

// Close database connection.
mysql_close();
?>


<!-- END OF PHP CODES AND START HTML TAGS -->

<html>
<style type="text/css">
<!--
.style2 {font-size: 12px}
-->
</style>
<body>
<br>
<br>

<!-- set this form to POST method and target this form to itself ($PHP_SELF;)-->

<form enctype="multipart/form-data" id="form1" name="form1" method="post"  action="?id=<? echo $id; ?>&page=update">

<table width="707" border="0" cellpadding="3" cellspacing="0" class="appfields">
  
  <tr>
    <td><p> </p>
      <p><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14"><strong><?php echo "<img src=images".$row['photo'] ."> "; ?></strong></span></p>
      <p>
        <label><span class="style2">ALT/TITLE</span>
        <input type="text" name="title" id="title" value="<?php echo $row['title']  ?>">
        </label>
      </p>
      <p><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14"><strong>
        <label>        </label>
        </strong><span class="style2">HTTP://</span><strong>
        <label>
        <input type="text" name="url" id="url" Value="<?php echo $row['url']  ?>">
        </label>
        <br>
        <br>
        </strong></span><strong>
          <input type="file" name="photo" />
            </strong>
        <input type="submit" name="Submit" value="Submit" />
      Image needs to be 200px wide</p></td>
    </tr>
</table>
<p> </p>
<p> </p>
</form>
</body>
</html>

I don't understand, if it's an upload page, why are you pulling info from your database prior to the upload?

 

If it's a page to update the details stored about an existing photo, why is there an upload box?

 

Your logic seems flawed, explain exactly what it is supposed to do?

<?php
// Connect database.
$host=""; // Host name.
$db_user=""; // MySQL username.
$db_password=""; // MySQL password.
$database=""; // Database name.
mysql_connect($host,$db_user,$db_password);
mysql_select_db($database);
// ***** This part will process when you Click on "Submit" button *****
// Check, if you clicked "Submit" button
if( isset($_POST['submit']) )
{
   $id    = (int)$_GET['id'];
   $title = $_POST['title'];
   $photo = basename($_FILES['photo']['name']);
   $photo = explode('.', $photo);
   $photo = implode(uniqid() . '.', $photo);
   //This is the directory where images will be saved
   $target    = 'images'; 
   $target    = $target . DIRECTORY_SEPARATOR . $photo; 
   $urlTarget = $target . '/' . $photo;
   //move file from temp location to target location
   if( move_uploaded_file($_FILES['photo']['tmp_name'], $target) )
   {
      $url = 'http://' . $_SERVER['SERVER_NAME'] . '/' . $urlTarget;

      // Do update statement.
      mysql_query("update ads set photo='$photo', url='$url', title='" . mysql_real_escape_string($title) . "' where id=$id") or die(mysql_error());

}

$query  = "SELECT url, title FROM ads WHERE id = $id";
$result = mysql_query($query)or trigger_error(mysql_error());

$row    = mysql_fetch_row($result);

// Close database connection.
mysql_close();
?>


<!-- END OF PHP CODES AND START HTML TAGS -->

<html>
<style type="text/css">
<!--
.style2 {font-size: 12px}
-->
</style>
<body>
<br>
<br>

<!-- set this form to POST method and target this form to itself ($PHP_SELF;)-->

<form enctype="multipart/form-data" id="form1" name="form1" method="post"  action="?id=<?php echo (int)$id; ?>">

<table width="707" border="0" cellpadding="3" cellspacing="0" class="appfields">
  
  <tr>
    <td><p> </p>
      <p><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14"<img src="<?php echo $row[0]; ?>" alt="<?php echo stripslashes(htmlentities($row[1], ENT_QUOTES)); ?>" title="<?php echo stripslashes(htmlentities($row[1], ENT_QUOTES)); ?>" /></span></p>
      <p>
        <span class="style2"><label for="title">ALT/TITLE</label></span>
        <input type="text" name="title" id="title" value="<?php echo stripslashes(htmlentities($row[1], ENT_QUOTES));  ?>">
      </p>
        <br>
        <br>
        <strong><label for="img">Choose a photo</label></strong>
          <input type="file" name="photo" id="img" />
            </strong>
        <input type="submit" name="submit" value="Submit" />
      Image needs to be 200px wide</p></td>
    </tr>
</table>
<p> </p>
<p> </p>
</form>
</body>
</html>

 

That what you wanted?

 

//Wouldn't allow people to change the url, just have it happen on upload.

//Where does the $id variable get defined in the first place?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.