将数据插入mysqli数据库

时间:2014-12-31 13:45:12

标签: php mysql mysqli insert

我有一段代码在另一个网站上运行良好,但是当我尝试在我的其他网站上实现它时,它会收到一个php 500内部错误。

代码连接到我的数据库,然后获取图像文件并调整图像大小。然后它应该将数据插入数据库。 但是要么将任何一个图像上传到服务器,要么创建数据库行。

代码是:

    <?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' );
}

?>

2 个答案:

答案 0 :(得分:0)

问题出在文件的底部......

特别是本节

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

mysqli_close($con);

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

当流量被搞砸时,我实际上没有找到解决方法。 实际上有2个错误。

1)你有一个额外的支撑}

2)你有一个没有链接到if。

的其他人

答案 1 :(得分:0)

试试这个

     <?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' );
}

?>