Jump to content

Image / Text Upload Issue


Wayniac

Recommended Posts

I can't seem to get the image upload and entry text (title, age, testimonial) to work the same time and on the same id.

 

When I connect to the database through

include("config.php");

, it works for the entry text, but the image won't show up. It will however add an id in the database for both entry text and image by adding 2 entries. One for the image which is not uploaded, and the other for the entry text which is uploaded. The oposite affect of this happens if I comment out the

include("config.php");

, and connect to the database directly by using

 

$errmsg = "";
//if (! @mysql_connect("localhost","root","")) {
//       $errmsg = "Cannot connect to database";
//       }
@mysql_select_db("album");

 

I would REALLY appreciate any help offered.

 

<?php
//error_reporting(0);
include("config.php");
// Connect to database

/*$errmsg = "";
//if (! @mysql_connect("localhost","root","")) {
//       $errmsg = "Cannot connect to database";
//       }
@mysql_select_db("album");
*/

// Insert any new image into database

if ($_REQUEST[completed] == 1) {
        // Need to add - check for large upload. Otherwise the code
        // will just duplicate old file ;-)
        // ALSO - note that latest.img must be public write and in a
        // live appliaction should be in another (safe!) directory.
        move_uploaded_file($_FILES['imagefile']['tmp_name'],"latest.img");
        $instr = fopen("latest.img","rb");
        $image = addslashes(fread($instr,filesize("latest.img")));
        if (strlen($instr) < 149000) {
                mysql_query ("insert into album (title, imgdata) values (\"".
                $_REQUEST[whatsit].
                "\", \"".
                $image.
                "\")");
        } else {
                $errmsg = "Too large!";
        }
}

// Find out about latest image

$gotten = @mysql_query("select * from album order by albumid desc limit 1");
if ($row = @mysql_fetch_assoc($gotten)) {
        $title = htmlspecialchars($row[title]);
        $bytes = $row[imgdata];
} else {
        $errmsg = "There is no image in the database yet";
        $title = "no database image available";
        // Put up a picture of our training centre
        $instr = fopen("../upload/example_1.jpg","rb");
        $bytes = fread($instr,filesize("../upload/example_1.jpg"));
}

// If this is the image request, send out the image

if ($_REQUEST[gim] == 1) {
        header("Content-type: image/jpeg");
        print $bytes;
        exit ();
        }



  if(isset($_POST['submit']))

  {//begin of if($submit).

      // Set global variables to easier names

     // and pervent sql injection and apostrophe to break the db.

      $title = mysql_real_escape_string($_POST['title']);

      $age = mysql_real_escape_string($_POST['age']);

      $testimonial = mysql_real_escape_string($_POST['testimonial']);



              //check if (title) field is empty then print error message.

              if(!$title){  //this means If the title is really empty.

                     echo "Error: Entry title is a required field. Please fill it.";

                     exit(); //exit the script and don't do anything else.

              }// end of if



         //run the query which adds the data gathered from the form into the database

         $result = mysql_query("INSERT INTO album (title, dtime, age, testimonial)

                       VALUES ('$title',NOW(),'$age','$testimonial')",$connect);

          //print success message.

          echo "<b>Thank you! News added Successfully!<br>You'll be redirected to Home Page after (4) Seconds";

          echo "<meta http-equiv=Refresh content=4;url=index.php>";

  }//end of if($submit).





  // If the form has not been submitted, display it!

else

  {//begin of else



      ?>

<br>
<h3>::Add News</h3>
<font color=red>
<?= $errmsg ?>
</font>
<center>
  <img src=?gim=1 width=500><br>
  <b>
  <?= $title ?>
</center>
<hr>
<h2>Please upload a new picture and title</h2>
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post" enctype="multipart/form-data" name="" id="">
  <input type="hidden" name="MAX_FILE_SIZE" value="15000000">
  Please choose an image to upload:
  <input type="file" name="imagefile">
  <br>
  <!--Please enter the title of that picture: <input name="whatsit"><br>-->
  then: <br>
  <hr>
  <input type="hidden" name="completed" value="1">
  <p> Title:
    <input name="title" size="40" maxlength="255">
    <br>
    Age:
    <textarea name="age"  rows="7" cols="30"></textarea>
    <br>
    Testimonial:
    <textarea name="testimonial" rows="7" cols="30"></textarea>
    <br>
    <input type="submit" name="submit" value="Add News">
</form>
<?php

  }//end of else

?>

Link to comment
https://forums.phpfreaks.com/topic/184433-image-text-upload-issue/
Share on other sites

Thank you for your reply, here you go :)

 

<?php

$dbhost="localhost";  #SQL Database Hostname
$dbusername="root";   #SQL Username
$dbpassword="";   #SQL Password
$dbname="album"; #SQL Database Name


// Connect to Mysql
$connect = mysql_connect($dbhost, $dbusername, $dbpassword);
//Select the correct database.
$db = mysql_select_db($dbname,$connect) or die ("Could not select database");
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.