HereToHelpPHP
HereToHelpPHP

Reputation: 189

inserting data into mysqli database

Ive got a piece of code that is working great on another site, but when i try to implementate it on my other site it get a php 500 internal error.

The code is connecting to my database to then get an image file and resize the image. Then it should insert the data into the database. But either image is uploaded to the server or the database row is beeing created.

the code is:

    <?php
$con=mysqli_connect("localhost","user","pass","db");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// defined the upload image directory and it must be read and writable 
// it is used for save the image 
define('UPLOAD_PATH', $_SERVER['DOCUMENT_ROOT'] . '/websites/img/');

// defined the image directory, and this used for display
define('DISPLAY_PATH', '/websites/img/');

//defined the image size
define('MAX_FILE_SIZE', 2000000);

// image extension 
$permitted = array('image/jpeg', 'image/jpg', 'image/png', 'image/gif');

   $fileName = $_FILES['userbanner']['name'];
   $tmpName = $_FILES['userbanner']['tmp_name'];
   $fileSize = $_FILES['userbanner']['size'];
   $fileType = $_FILES['userbanner']['type'];

// make a new image name
   // get the file extension 
   $ext = substr(strrchr($fileName, "."), 1);
   // generate the random file name
   $randName = md5(rand() * time());

   // image name with extension
   $myfile = $randName . '.' . $ext;
   // save image path
   $path = UPLOAD_PATH . $myfile;

   if (in_array($fileType, $permitted) && $fileSize > 0
           && $fileSize <= MAX_FILE_SIZE) {

      $result = move_uploaded_file($tmpName, $path);

      if ($result) {
        $imageurl = "" . DISPLAY_PATH . $myfile . "";
      }
      else {
          $imageurl = "/src/img/space.gif";
      }
   }

//store image to the upload directory

      include("../smart_resize_image.function.php");

       //indicate which file to resize (can be any type jpg/png/gif/etc...)
      $file = "" . DISPLAY_PATH . $myfile . "";

      //indicate the path and name for the new resized file
      $resizedFile = "" . DISPLAY_PATH . $myfile . "";

      //call the function (when passing path to pic)
      smart_resize_image($file , null, 250 , 250 , false , $resizedFile , false , false ,100 );
      //call the function (when passing pic as string)
      smart_resize_image(null , file_get_contents($file), 250 , 250 , false , $resizedFile , false , false ,100 );

      //done!

      include('session.php');

      $userid = $login_id;
      $url = $_POST['linkurl'];
      $image = $imageurl;
      $userviews = $login_views;

$sql="INSERT INTO websites (userid, url, image, userviews)
VALUES ('". $userid ."', '". $url ."', '". $image ."', '". $userviews ."')";

    if (!mysqli_query($con,$sql)) {
      die('Error: ' . mysqli_error($con));
    }

    mysqli_close($con);

    header( 'Location: /dashboard/#start' );
}
else 
{
    header( 'Location: /dashboard/#start?error=1' );
}

?>

Upvotes: 1

Views: 594

Answers (2)

user3061787
user3061787

Reputation: 36

Try this

     <?php
$con=mysqli_connect("localhost","user","pass","db");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// defined the upload image directory and it must be read and writable 
// it is used for save the image 
define('UPLOAD_PATH', $_SERVER['DOCUMENT_ROOT'] . '/websites/img/');

// defined the image directory, and this used for display
define('DISPLAY_PATH', '/websites/img/');

//defined the image size
define('MAX_FILE_SIZE', 2000000);

// image extension 
$permitted = array('image/jpeg', 'image/jpg', 'image/png', 'image/gif');

   $fileName = $_FILES['userbanner']['name'];
   $tmpName = $_FILES['userbanner']['tmp_name'];
   $fileSize = $_FILES['userbanner']['size'];
   $fileType = $_FILES['userbanner']['type'];

// make a new image name
   // get the file extension 
   $ext = substr(strrchr($fileName, "."), 1);
   // generate the random file name
   $randName = md5(rand() * time());

   // image name with extension
   $myfile = $randName . '.' . $ext;
   // save image path
   $path = UPLOAD_PATH . $myfile;

   if (in_array($fileType, $permitted) && $fileSize > 0
           && $fileSize <= MAX_FILE_SIZE) {

      $result = move_uploaded_file($tmpName, $path);

      if ($result) {
        $imageurl = "" . DISPLAY_PATH . $myfile . "";
      }
      else {
          $imageurl = "/src/img/space.gif";
      }
   }

//store image to the upload directory

      include("../smart_resize_image.function.php");

       //indicate which file to resize (can be any type jpg/png/gif/etc...)
      $file = "" . DISPLAY_PATH . $myfile . "";

      //indicate the path and name for the new resized file
      $resizedFile = "" . DISPLAY_PATH . $myfile . "";

      //call the function (when passing path to pic)
      smart_resize_image($file , null, 250 , 250 , false , $resizedFile , false , false ,100 );
      //call the function (when passing pic as string)
      smart_resize_image(null , file_get_contents($file), 250 , 250 , false , $resizedFile , false , false ,100 );

      //done!

      include('session.php');

      $userid = $login_id;
      $url = $_POST['linkurl'];
      $image = $imageurl;
      $userviews = $login_views;

$sql="INSERT INTO websites (userid, url, image, userviews)
VALUES ('". $userid ."', '". $url ."', '". $image ."', '". $userviews ."')";

    if (!mysqli_query($con,$sql)) {
      die('Error: ' . mysqli_error($con));


    mysqli_close($con);

    header( 'Location: /dashboard/#start' );
}
else 
{
    header( 'Location: /dashboard/#start?error=1' );
}

?>

Upvotes: 0

geggleto
geggleto

Reputation: 2625

The issue is at the the bottom of the file...

specifically this section

if (!mysqli_query($con, $sql)) {
die('Error: ' . mysqli_error($con));
}

mysqli_close($con);

header( 'Location: /dashboard/#start' );
}
else
{
header( 'Location: /dashboard/#start?error=1' );
}

I don't actually see a way to fix it as the flow is screwed up. There are actually 2 errors.

1) You have an extra brace }

2) And you have an else that is not linked to an if.

Upvotes: 0

Related Questions