Call to undefined function INSERT INTO

Codes here !

Moderators: egami, macek, gesf

Post Reply
asliceofpizza
New php-forum User
New php-forum User
Posts: 3
Joined: Thu Feb 01, 2018 7:45 pm

Thu Feb 01, 2018 7:50 pm

I cannot figure where I am going wrong with this code, as far as I can tell I am calling to mysql correctly and the function is structured correctly. The files are uploading correctly, I am just having trouble getting the SQL portion to function properly.

Here is the PHP

Code: Select all

     <?php
    
    $host="localhost"; // Host name
    $username="user"; // Mysql username
    $password="pass"; // Mysql password
    $dbname="my_files"; // Database name
    $tbl_name="files"; // Table name
    
    mysql_connect("$host", "$username", "$password")or die("cannot connect");
    
    // Check connection
    
    if(isset($_POST['submit'])){
     
     // Count total files
     $countfiles = count($_FILES['file']['name']);
    
     // Looping all files
     for($i=0;$i<$countfiles;$i++){
      $filename = $_FILES['file']['name'][$i];
      $filetitle=$_POST['filetitle'];
      $notes=$_POST['notes'];
      $name=$_POST['name'];
      $puzzle=$_POST['puzzle'];
     
      $sql="INSERT INTO $tbl_name(filename,filetitle,notes,name,puzzle) VALUES('$filename','$filetitle','$notes','$name','$puzzle')";
    
     $result=mysql_query($sql (mysql_error()));
    
      // Upload file
      move_uploaded_file($_FILES['file']['tmp_name'][$i],'files/'.$filename);
    
     }
    } 
    ?>
Here is the form html I am using

Code: Select all

   <form method='post' action='' enctype='multipart/form-data'>
     <input type="file" name="file[]" id="file" multiple>
       <label class="col-md-4 control-label" for="filetitle">Title</label> 
       <input id="filetitle" name="filetitle" type="text" placeholder="" class="form-control input-md">
       <br>
       <label class="col-md-4 control-label" for="name">Your Name</label>
       <input id="name" name="name" type="text" placeholder="" class="form-control input-md">
       <label class="col-md-4 control-label" for="notes">Notes</label>
       <textarea class="form-control" id="notes" name="notes"></textarea>
       <p> Is this a puzzle? 
       <select name="puzzle">
        <option value="n">N</option>
        <option value="y">Y</option>
       </select>
       </p>
    
     <input type='submit' name='submit' value='Upload'>
    </form>
thank you

chorn
php-forum Fan User
php-forum Fan User
Posts: 536
Joined: Fri Apr 01, 2016 2:18 am

Thu Feb 01, 2018 10:03 pm

Code: Select all

$result=mysql_query($sql (mysql_error()));
what is this? you are trying to use your statement as a function

asliceofpizza
New php-forum User
New php-forum User
Posts: 3
Joined: Thu Feb 01, 2018 7:45 pm

Fri Feb 02, 2018 5:11 pm

my mistake, it was from another code I pieced it from. I removed the "$result=" and still am getting the same error

"Fatal error: Call to undefined function INSERT INTO files"..

User avatar
zaim
New php-forum User
New php-forum User
Posts: 29
Joined: Sat Jan 06, 2018 9:05 am
Location: Kedah, Malaysia.

Fri Feb 02, 2018 9:51 pm

Hello.

I believe he meant not the variable but the query. Other than that, you are using a deprecated extension of mysql_* , use mysqli_* instead.

Code: Select all

$result = mysqli_query( your database connection, your query );
Sorry if my answers aren't top notch but I will try my best to help anyone in need. :oops:

asliceofpizza
New php-forum User
New php-forum User
Posts: 3
Joined: Thu Feb 01, 2018 7:45 pm

Sat Feb 03, 2018 6:09 pm

zaim wrote:
Fri Feb 02, 2018 9:51 pm
Hello.

I believe he meant not the variable but the query. Other than that, you are using a deprecated extension of mysql_* , use mysqli_* instead.

Code: Select all

$result = mysqli_query( your database connection, your query );
Thank you, you led me in the right direction and It works now, I've corrected the following;

Code: Select all

mysql_connect("$host", "$username", "$password")or die("cannot connect");

to

Code: Select all

$con=mysqli_connect("localhost","user","pass","database");
and corrected

Code: Select all

$result=mysql_query($sql (mysql_error()));
to

Code: Select all

if (!mysqli_query($con,$sql))
  {
  echo("Error description: " . mysqli_error($con));
  }

Post Reply