mysql - if file not exist insert in one table and take other values to insert in other table -


i have following database 2 tables pk_hash_id in md5 table , fk_hash_id in files table.

lets example need insert new record, first have check if md5hash not exist in md5, if not add md5 , files, if exist on md5 take hash_id , insert files other valuse taken user such file_name, parent_path, datatype,etc

enter image description here

make md5hash column unique in md5 table, can sure there never duplicate entry in it. try insert each md5hash in md5 table, if query fails check error type if error because of unique key constraint, means hash exists if no error hash inserted table md5. sample code

$sql = "insert md5 values (null,'file_name',$hash)"; $result = mysqli_query($sql); if( mysql_errno() == 1062) {     //write code want execute in case of hash existing  } else {    //write code hash doesn't existed earlier, has added in database. no need add again. } 

i hope helped.


Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -