Forums

Fatal Error

Chris Blair 11 Dec, 2010
Dear All,
I finally managed to get my form completed and working on my localhost machine and shipped the form over to the live server where it all fell apart on me :-(

I ran the form, entered the required data and hit the submit button. After a few seconds this came back to me...

Fatal error: Class 'mysqli' not found in /home/dwyert/public_html/components/com_chronocontact/libraries/customcode.php(64) : eval()'d code on line 7


I am assuming it it referring to line 7 in my On Submit Before code which looks like this

<?php
// Check if a file has been uploaded
if(isset($_FILES['filedata'])) {
    // Make sure the file was sent without errors
    if($_FILES['filedata']['error'] == 0) {
        // Connect to the database
        $dbLink = new mysqli('localhost', 'user', 'pwd', 'db');
        if(mysqli_connect_errno()) {
            die("MySQL connection failed: ". mysqli_connect_error());
        }
 
        // Gather all required data
        $name = $dbLink->real_escape_string($_FILES['filedata']['name']);
        $mime = $dbLink->real_escape_string($_FILES['filedata']['type']);
        $data = $dbLink->real_escape_string(file_get_contents($_FILES['filedata']['tmp_name']));
        $size = intval($_FILES['filedata']['size']);
		$custname = $_POST['custname'];
		$delnoteno = $_POST['delnoteno'];
		$delrefno = $_POST['delrefno'];
$deldate = $_POST['deldate'];
 
        // Create the SQL query
        $query = "
            INSERT INTO `table_name` (
                `filename`, `filemimetype`, `filesize`, `filedata`, `filecreated`, `custname`, `delnoteno`, `delrefno`,`deldate`
            )
            VALUES (
                '{$name}', '{$mime}', {$size}, '{$data}', NOW(), '{$custname}', '{$delnoteno}', '{$delrefno}', '{$deldate}'
            )";
 
        // Execute the query
        $result = $dbLink->query($query);
 
        // Check if it was successfull
        if($result) {
            echo '';
        }
        else {
            echo 'Error! Failed to insert the file'
               . "<pre>{$dbLink->error}</pre>";
        }
    }
    else {
        echo 'An error accured while the file was being uploaded. '
           . 'Error code: '. intval($_FILES['filedata']['error']);
    }
 
    // Close the mysql connection
    $dbLink->close();
}
else {
    echo 'Error! A file was not sent!';
}
?>


I don't know much about this stuff but I am learning (fast) so I don't really know where to begin looking or what I should be looking at, especially as the form worked perfectly on my test server.

TIA & Regards

Chris
nml375 11 Dec, 2010
Hi Chris,
Simply put, this server lacks the mysqli class. It could be your site has an older version of PHP, or that the admins choose not to include it. You could still use the mysql_* functions, or Joomlas JDatabaseMySQL class.
If you go for the JDatabaseMySQL class, the code would look something like this:
<?
//Use this if we're storing the data in the very same database as your Joomla/chronoforms site uses
$db =& JFactory::getDBO();

//Use this instead if you need to use a different hostname, port, username, etc..
$db =& JDatabase::getInstance(array(
    'driver' => 'mysql',
    'host' => 'localhost',
    'user' => 'user',
    'password' => 'pass',
    'database' => 'db',
    'prefix' => 'jos_'
));

//-----
//Now for the good stuff...
//Define the query template... Be aware that $db->quote will add the '' for us, and $db->namequote the ``
$query = 'INSERT INTO %s (
  %s, %s, %s, %s, %s, %s, %s, %s, %s
) VALUES (
  %s, %s, %s, %s, %s, %s, %s, %s, %s
)';

//insert the various values... Use the nameQuote and quote methods to properly escape the names and strings...
$pst = sprintf($query,
  $db->nameQuote('Annaveigh'),
  $db->nameQuote('filename'),
  $db->nameQuote('filemimetype'),
  $db->nameQuote('filesize'),
  $db->nameQuote('fieldata'),
  $db->nameQuote('fielcreated'),
  $db->nameQuote('custname'),
  $db->nameQuote('delnoteno'),
  $db->nameQuote('delrefno'),
  $db->nameQuote('deldate'),
  $db->quote($name),
  $db->quote($mime),
  $db->quote($size),
  $db->quote($data),
  'NOW()',
  $db->quote($custname),
  $db->quote($delnoteno),
  $db->quote($delrefno),
  $db->quote($deldate)
);

$db->SetQuery($query);
$result = $db->execute();
//.. rest of code goes here...


/Fredrik
GreyHead 11 Dec, 2010
Hi Chris,

Did I post somewhere that I got this working with a straightforward ChronoForms file upload and a DB Connection with a few lines in the OnSubmti before box.

Bob
Chris Blair 11 Dec, 2010
Hi Bob, Fredrik,
You did post before Bob. Please accept my apologies...I will post a reply to that one now.

Fredrik, many thanks.

Regards

Chris
This topic is locked and no more replies can be posted.