mysql - PHP check if value exists -
i’m trying write if
code, can't figure out how.
in database table have primary key id
. in table have objekt_nr
& element_nr
. now, before phone call instert
need check if objekt_nr
element_nr
exist. if
objekt_nr
element_nr
exist. update
instead of instert
. id can unique.
example of table:
id......objekt_nr......element_nr
1.......1...............1
2.......1...............2
3.......1...............3
4.......2...............1
5.......2...............2
php:
if(isset($_post["import"])){ echo '<a href=".php">fortsätt...</a><br />'; echo $path=$_files["file"]["tmp_name"]; //load file phpexcel $objphpexcel = phpexcel_iofactory::load($path); //loop threw file info foreach ($objphpexcel->getworksheetiterator() $worksheet) { $worksheettitle = $worksheet->gettitle(); $highestrow = $worksheet->gethighestrow(); // e.g. 10 $highestcolumn = 'j'; //$worksheet->gethighestcolumn(''); // e.g 'f' $highestcolumnindex = phpexcel_cell::columnindexfromstring($highestcolumn); $nrcolumns = ord($highestcolumn) - 64; //echo file info echo "<br>the worksheet ".$worksheettitle." has "; echo $nrcolumns . ' columns (a-' . $highestcolumn . ') '; echo ' , ' . $highestrow . ' row.'; echo '<br>data: <table border="1"><tr>'; //loop threw colum, rows , cells ($row = 2; $row <= $highestrow; ++ $row) { echo '<tr>'; ($col = 0; $col < $highestcolumnindex; ++ $col) { $cell = $worksheet->getcellbycolumnandrow($col, $row); $val = $cell->getcalculatedvalue(); //$datatype = phpexcel_cell_datatype::datatypeforvalue($val); echo '<td>' . $val . '<br></td>'; } echo '</tr>'; } echo '</table>'; } ($row = 2; $row <= $highestrow; ++ $row) { $val=array(); ($col = 0; $col < $highestcolumnindex; ++ $col) { $cell = $worksheet->getcellbycolumnandrow($col, $row); $val[] = $cell->getcalculatedvalue(); } // prepare query $query = "insert phpexcel( objekt_nr, objekt_rev, element_nr, element_hojd, element_typ, element_langd, element_oppningar, element_vikt, element_ritare, element_status) values ( :objekt_nr, :objekt_rev, :element_nr, :element_hojd, :element_typ, :element_langd, :element_oppningar, :element_vikt, :element_ritare, :element_status )"; // security measures $query_params = array( ':objekt_nr' => $val[0], ':objekt_rev' => $val[1], ':element_nr' => $val[2], ':element_hojd' => $val[3], ':element_typ' => $val[4], ':element_langd' => $val[5], ':element_oppningar' => $val[6], ':element_vikt' => $val[7], ':element_ritare' => $val[8], ':element_status' => $val[9] ); seek { $stmt = $db->prepare($query); $result = $stmt->execute($query_params); } catch(pdoexception $ex){ die("failed run query: " . $ex->getmessage()); } //echo $query."\n"; } }
two options here.
1) create "objekt_nr" field primary or unique key , utilize replace
instead of insert
(http://dev.mysql.com/doc/refman/5.0/en/replace.html). pretty much @hd 's reply in comments.
2) check existing "objekt_nr" , "element_nr" , run appropriate query
$check_duplicate_query = "select count(*) phpexcel objekt_nr = ':objekt_nr' , element_nr = ':element_nr'"; $stmt = $db->prepare($check_duplicate_query); $result = $stmt->execute($query_params); $rows = $stmt->fetch(pdo::fetch_num); if($rows[0] > 0) { //update } else { //insert } $stmt = $db->prepare($query); $result = $stmt->execute($query_params);
edit:
just thought of 3rd option
create field "objekt_element_nr" , set field unique. field gets assigned objekt , element number combination, illustration should unique.
e.g. 1_1, 1_2, 1_3, 2_1, 2_2, etc.
then utilize replace
function linked above.
your $query_params
like
$query_params = array( ':objekt_nr' => $val[0], ':objekt_rev' => $val[1], ':element_nr' => $val[2], ':objekt_element_nr' => $val[0].'_'.$val[2] ':element_hojd' => $val[3], ':element_typ' => $val[4], ':element_langd' => $val[5], ':element_oppningar' => $val[6], ':element_vikt' => $val[7], ':element_ritare' => $val[8], ':element_status' => $val[9] );
php mysql
No comments:
Post a Comment