-1

I need help to insert an image in SQL and then recall it on HTML. I'm currently trying to insert the image into the database, returning this error

"Error sending the file. Please try again! Fatal error: Uncaught Error: Call to undefined function mysql_connect () in /membri/tiservo/inserimentooprodotti/image/write_db.php:10 Stack trace: # 0 {main} thrown in /membri/tiservo/inserimentoprodotti/image/write_db.php online 10 "

Where have I gone wrong? to others it works

P.S I will not insert the code to call the image in HTML as I have not yet tried if it works.

HTML FORM:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        <title>Carica file nel DB</title>
    </head>
    <body>
        <form name="upload" action = "write_db.php" method = "POST" enctype = "multipart/form-data">
            <input type="submit" name="invia" value="Invia file">
            <input type = "file" name = "file_inviato" />
            <input type = "submit"/>
        </form>
    </body>
</html>

PHP:

<?php
    // Verifico eventuali problemi nell'upload del file
    if ((!isset($_FILES["file_inviato"])) || ($_FILES["file_inviato"]["error"] != UPLOAD_ERR_OK))
        echo("Errore nell'invio del file. Riprova!");

    // Connessione e selezione del database
    mysql_connect("localhost", "MYUSER", "")
    or die("Connessione non riuscita: " . mysql_error());

    if (!mysql_select_db("MY_DB"))
        die("Selezione database fallita!");

    // Recupero delle informazioni sul file inviato
    $nome_file_temporaneo = $_FILES["file_inviato"]["tmp_name"];
    $nome_file_vero = $_FILES["file_inviato"]["name"];
    $tipo_file = $_FILES["file_inviato"]["type"];

    // Leggo il contenuto del file
    $dati_file = file_get_contents($nome_file_temporaneo);

    // Preparo il contenuto del file per la query sql
    $dati_file = addslashes($dati_file);

    // Query per inserire il file nel DB
    $query = "INSERT INTO tabella_file SET
              nome = '$nome_file_vero', 
              tipo = '$tipo_file', 
              dati = '$dati_file'";

    mysql_query($query)
    or die("Query non valida: " . mysql_error());

    // Messaggio di successo
    echo("Memorizzazione del file <b>$nome_file_vero</b> nel database eseguita correttamente!");

Edit

Thanks for all the answers you have given me, I didn't know that it was no longer supported. I change the question so that someone can help me further.

Could you give me a working code simply to take the image insert it in the database and then recall it from HTML.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • What version of php are you using? mysql_ functions were removed entirely in php7. [link](https://www.php.net/manual/en/function.mysql-connect.php) I'd look at mysqli_ functions or PDO. – CountKyle Feb 28 '20 at 10:00
  • 1
    Does this answer your question? [Why shouldn't I use mysql\_\* functions in PHP?](https://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – Salines Feb 28 '20 at 10:01
  • INSERT has no SET clause. Seems like you've mixed the INSERT and UPDATE syntax. – jarlh Feb 28 '20 at 10:03
  • Thanks for information use this PHP 7.3.11 - Optimized for AlterVista – Automaction Program Feb 28 '20 at 10:07
  • Thanks a lot for the answer I discovered that it is no longer supported by having version 7.1. I have updated the question, always take a look if you like – Automaction Program Feb 28 '20 at 10:42

2 Answers2

1

If you are using PHP 7.0 this function has been removed as an alternative use mysqli_connect().

Also stated in the Warning in PHP Documentation: https://www.php.net/manual/en/function.mysql-connect.php

Hope this helps.

Fabian Börner
  • 168
  • 1
  • 2
  • 9
1

This code should work:

$host="localhost";<br/>
$user="MYUSER";<br/>
$password="";<br/>
$con=mysqli_connect($host,$user,$password);<br/>
mysqli_select_db($con,"MY_DB"); <br/>
session_start(); //To start the session<br/>
$query=mysqli_query($con,your query);<br/>
Nikos Hidalgo
  • 3,666
  • 9
  • 25
  • 39
Rahul Rai
  • 44
  • 2
  • Thanks a lot for the answer I discovered that it is no longer supported by having version 7.1. I have updated the question, always take a look if you like – Automaction Program Feb 28 '20 at 10:43