0

I need a little help to an error with my PhP code.
I have a website, that work as a company website and a online store. The Idea is that the Admin can make custom database row, in order to generate through a PhP script a custom page that show to the user the product info.
The problem i that my PhP code Just fail when uploading the script into the database, the error that show up is just chrome saying "This page isn’t working, localhost is currently unable to handle this request. HTTP ERROR 500"

I have do many research in web and here on StackOverflow, but nothings work's.

<?php
session_start();

$username = $_SESSION['username'];
$password = $_SESSION['password'];

$db_connect = mysqli_connect('localhost', $username, $password, 'cosipi');

if(!$db_connect) {
    session_unset();
    session_destroy();
    exit('Non sei autorizzato ad accedere a questa pagina.');
}



$file = $_FILES['image']['tmp_name'];

$image = file_get_contents($_FILES['image']['tmp_name']);
$image_name = $_FILES['image']['name'];


$sql    = "INSERT INTO products ('','name','description','photo') VALUES ('', 'nome foto','descrizione','12','');";
$query  = mysqli_query($db_connect, $sql);

mysqli_close ($db_connect);

?>

I have even followed a video tutorial to try make this work, but it just won't.

Here is the HTML form code.

<form action="/new_product.php" method="post" enctype="multipart/form-data">

                        <input name="MAX_FILE_SIZE" value="102400" type="hidden">
                        <input type="file" name="image" value="Photo" accept="image/jpg" /><br /><br />
                        <input type="submit" value="Save" />
                    </form>

MySql database have a specific column named "photo" with type "longblob".

I'm not the best with PHP and usually i don't work with photos on form and mysql database.

Any help would be appreciated, thank's.

-EDIT-

Fixed some Syntax and the mixed API problem, still not working.

Lynxes Exe
  • 45
  • 1
  • 8
  • 1
    check your logs – Funk Forty Niner Jul 20 '17 at 15:58
  • you do have an obvious syntax/parse error, a few actually. – Funk Forty Niner Jul 20 '17 at 15:58
  • where is the syntax error? – Lynxes Exe Jul 20 '17 at 15:59
  • `exit('You can't see this page.');` <<< in there. Edit: and you now edited that. https://stackoverflow.com/revisions/45219717/2 – Funk Forty Niner Jul 20 '17 at 15:59
  • 2
    and probably in `$image)");` most likely. – Funk Forty Niner Jul 20 '17 at 15:59
  • 3
    you're also mixing different mysql apis. Your code contains too many errors. – Funk Forty Niner Jul 20 '17 at 16:00
  • Yes, but that's not the problem, I have translated that from italian now originally it would be "Non sei autorizzato ad accedere", I just forget to add the slash now – Lynxes Exe Jul 20 '17 at 16:00
  • *si, io capito* (I understand), again, your code has too many errors. – Funk Forty Niner Jul 20 '17 at 16:00
  • 1
    [Can I mix MySQL APIs in PHP?](https://stackoverflow.com/questions/17498216/can-i-mix-mysql-apis-in-php) most likely a duplicate of this question – Funk Forty Niner Jul 20 '17 at 16:02
  • i'll try to fix this, but it started not working only when I added the mysql_query function, and visual studio code is not even declaring any syntax error – Lynxes Exe Jul 20 '17 at 16:02
  • 1
    *"not even declaring any error"* - that's because you're *not* checking for them at all. – Funk Forty Niner Jul 20 '17 at 16:03
  • Possible duplicate of [Can I mix MySQL APIs in PHP?](https://stackoverflow.com/questions/17498216/can-i-mix-mysql-apis-in-php) – chris85 Jul 20 '17 at 16:04
  • Use `mysqli` throughout the code. Parameterize your query. An `else` is not required for an `if`. It would also be easier to store the image on your filesystem and just store the location in your DB. – chris85 Jul 20 '17 at 16:05
  • removing the else and using mysqli_query will remove the chrome error, but the image won't anyway upload to the database. And ok, I'll try to do another script to store the location on the database and img on the file system, but there is a way to fix this script? – Lynxes Exe Jul 20 '17 at 16:07
  • Check for an error on the `mysqli_query` execution, that query still looks invalid. e.g. `$image` is not an integer. – chris85 Jul 20 '17 at 16:11
  • Note: The object-oriented interface to `mysqli` is significantly less verbose, making code easier to read and audit, and is not easily confused with the obsolete `mysql_query` interface. Before you get too invested in the procedural style it’s worth switching over. Example: `$db = new mysqli(…)` and `$db->prepare("…")` – tadman Jul 20 '17 at 16:12
  • If some one still care, at the end of all I followed one the comment that sayid to upload the file in a dir and then save the path into the database, this is working, so thank's anyway for all the help. – Lynxes Exe Jul 20 '17 at 17:15

0 Answers0