Jump to content

How to make visible of unvisible error while inserting data into mariadb database?


Go to solution Solved by Barand,

Recommended Posts

Hi all, 

I have a php code which did not give any error(I think) but nothing inserted into the database.  I am trying to insert all file structure and contents of the files into database by getting info from them and supply these into to insertData function:  

<?php 
	ini_set('display_errors', '1');
	error_reporting(E_ALL);
	//mysqli_report(MYSQLI_REPORT_ALL); // can give a fatal errors ans stops php script!
	mysqli_report(MYSQLI_REPORT_OFF);

function insertData($folderName,$subFolder,$file,$parentId,$dateAdded,$dateEdited,$summary,$title,$content) {
$subCategoryIdSQLQuery= "SELECT id FROM `subCategory` WHERE  subCategory.path = '/pages/blog/$folderName/'"; 			
$con=OpenConnection(); // < OPEN DATABASE CONNECTION
$sql2 = mysqli_prepare($con,$subCategoryIdSQLQuery); // < SQL Statement 1 
  if($sql2 !== FALSE ) {  
    $result2 = executeSQLStatement($con,$subCategoryIdSQLQuery);
    $subcategoryId= $result2->fetch_row()[0]; 
    $pathy="/pages/blog/".$folderName."/".$subFolder."/".$file;
    $fileInsertQuery="INSERT INTO `files`  
                     (path,categoryId,subCategoryId,dateAdded,dateEdited,title,summary,content,active) 
                     VALUES(
                     '$pathy',
                     '$parentId',
                     '$subcategoryId',
                     '$dateAdded',
                     '$dateEdited',
                     '$title',
                     '$summary',
                     '$content',
                     '1')";
      $sql3=mysqli_prepare($con,$fileInsertQuery); // < SQL STATEMENT 2
        if( $sql3 !== FALSE ) {
          $result3 = executeSQLStatement($con,$fileInsertQuery);
        } else {
          echo mysqli_error($con)." at file : ".$folderName."/".$subFolder."/".$file; // for sql3
        }
    } else {
      echo mysqli_error($con); // for sql2
    }
    closeConnection($con); // < CLOSE DATABASE CONNECTION
  
}

function CloseConnection($conn) {
		 mysqli_close($conn);
	}
	
function executeSQLStatement($con,$query) {
    if($con) {
      return $con->query($query); 
    } else {
      return null; 
    }
}  

function OpenConnection() {
	  $dbhost ="hostname";
  	  $dbuser ="username";
      $dbpass ="password";
      $db = "filesDb";
      global $conn; // DEFINE  conn as global variable to be able to use in other functions as well!
      $conn = new mysqli($dbhost, $dbuser, $dbpass,$db); 
      if( $conn == true ) { 
        //printf("Success... %s\n", $conn->host_info);
      } else if ($conn->connect_errno) { 

        //printf("Connection failed!... %s\n", $conn->host_info);
        throw new RuntimeException('mysqli connection error: ' . $conn->connect_error);
        exit("Connection failed: %s\n".$conn->connect_error) ; 
      } 
      return $conn;
}
  
  
?>

I could not insert any info into database! Where did I do wrong? Is there any way to make error(if there is any) visible? Thanks.

  • Solution
15 minutes ago, eaglehopes said:

Is there any way to make error(if there is any) visible?

One way is not to turn off the error reporting...

 

16 minutes ago, eaglehopes said:
mysqli_report(MYSQLI_REPORT_OFF);

 

  • Haha 1

Thanks Barand, I opened error reporting and it gives :

Fatal error: Uncaught mysqli_sql_exception: Duplicate entry '/fold1/' for key 'PATH' in /htdocs/filer.php:728

What does that mean? I am looking the table and see no duplicate entry! How can I prevent duplicate entry ?

4 minutes ago, eaglehopes said:

I am looking the table and see no duplicate entry!

You won't if trying to enter a duplicate is throwing an error.

Why is "path" the primary key?

6 minutes ago, eaglehopes said:

How can I prevent duplicate entry ?

Do you ...

  • want to prevent duplicate entries (which it is doing now)?  or
  • do you want to allow them and stop it reporting them? or
  • do you want to prevent them but not report them if they are attempted?

Catch the error. If it's a dupe key error (1062) then continue, otherwise throw the error

try {
   insert a new row
}
catch (error) {
   if error number != 1062 {         // if it's not a dupe error
       throw error                   // report it.
   }
}

 

Thanks Barand, I did and I saw other errors at last and finally I noticed that the error is in dateTime format! Thanks. I think I can solve it now! My code was :

try {
  $result1 = executeSQLStatement($con,$subCategoryInsertQuery);
}
  catch (Exception $e) {
  if( mysqli_errno($con) != 1062 ) {         // if it's not a dupe error
  	echo $e ;                   // report it.
  } 
}

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.