rafal Posted November 22, 2014 Share Posted November 22, 2014 Hello everybody, i try to check if the unique field inventar exists or not before i insert new record. if the inventar exits the user should get error message. but i dont know how to do this. thank you very much for your help. Rafal here is my code <?php include("123.php"); $inventar = $link1->real_escape_string($_POST["inp_inventar"]); $product = $link1->real_escape_string($_POST["inp_product"]); $price = $link1->real_escape_string($_POST["inp_price"]); $link1 = new mysqli("$hoster", "$nameuser", "$password", "$basedata") or die ("Connection Error!"); error_reporting (0); $check_inventar = mysqli_query($link1, "SELECT inventar FROM products WHERE inventar='$inventar'"); $check_inventar_num = mysqli_num_rows($check_inventar); if (isset($_POST['inp_submit']) AND $check_inventar_num = 0) { $query1 = "INSERT INTO products (inventar, product, price) VALUES ('$inventar', '$product', '$price')"; $result1 = $link1->query($query1) or die("Database Error!"); if ($result1 == true) { header ( 'Location:insert.php' ); } } else { echo "there is already product with same inventar id!"; } mysqli_close($link1); ?> Link to comment https://forums.phpfreaks.com/topic/292638-check-if-unique-field-exits-or-not/ Share on other sites More sharing options...
Barand Posted November 22, 2014 Share Posted November 22, 2014 Create a unique index on (product, inventor). This will then give a duplicate key error if you attempt to add the same combination Alternatively, having set up that index, change your query to INSERT INTO products (inventar, product, price) VALUES ('$inventar', '$product', '$price') ON DUPLICATE KEY UPDATE price = $price which will update the existing record instead of trying to insert a duplicate Link to comment https://forums.phpfreaks.com/topic/292638-check-if-unique-field-exits-or-not/#findComment-1497304 Share on other sites More sharing options...
rafal Posted November 22, 2014 Author Share Posted November 22, 2014 Thanks Barand, i made already inventor as unique key in the mysql database. i dont want want update the price or product. i want make insert of duplicate inventor not possible. the user should get the following error message "there is already product with same inventor id!". thanks again Rafal Link to comment https://forums.phpfreaks.com/topic/292638-check-if-unique-field-exits-or-not/#findComment-1497308 Share on other sites More sharing options...
Barand Posted November 22, 2014 Share Posted November 22, 2014 If you have a unique key already set on inventor then inserting a duplicate is already impossible. Link to comment https://forums.phpfreaks.com/topic/292638-check-if-unique-field-exits-or-not/#findComment-1497317 Share on other sites More sharing options...
rafal Posted November 22, 2014 Author Share Posted November 22, 2014 dear Barand, the user can not insert product if there is inventor duplicate, my target is: i want show the user error message if he try to insert product with duplicate inventor. how to do this in the code? thanks Rafal Link to comment https://forums.phpfreaks.com/topic/292638-check-if-unique-field-exits-or-not/#findComment-1497321 Share on other sites More sharing options...
Barand Posted November 22, 2014 Share Posted November 22, 2014 i want show the user error message if he try to insert product with duplicate inventor. Isn't that what you are doing? Link to comment https://forums.phpfreaks.com/topic/292638-check-if-unique-field-exits-or-not/#findComment-1497322 Share on other sites More sharing options...
rafal Posted November 23, 2014 Author Share Posted November 23, 2014 Hello Barand, when i try to enter duplicate of inventor, i get the error message "Database Error!" but not what i want "there is already product with same inventor id!". i think my "if" condition is wrong. am i right? Link to comment https://forums.phpfreaks.com/topic/292638-check-if-unique-field-exits-or-not/#findComment-1497359 Share on other sites More sharing options...
Barand Posted November 23, 2014 Share Posted November 23, 2014 real_escape_string() uses the database connection so you have to connect before using it Link to comment https://forums.phpfreaks.com/topic/292638-check-if-unique-field-exits-or-not/#findComment-1497369 Share on other sites More sharing options...
mikosiko Posted November 23, 2014 Share Posted November 23, 2014 In addition to what Barand said.. You don't see anything wrong with this line in your code? if (isset($_POST['inp_submit']) AND $check_inventar_num = 0) HINT: Assignment Operator is not the same as the Comparison Operator Link to comment https://forums.phpfreaks.com/topic/292638-check-if-unique-field-exits-or-not/#findComment-1497380 Share on other sites More sharing options...
rafal Posted November 23, 2014 Author Share Posted November 23, 2014 Hello Mikosiko, i do not understand your comment. Assignment Operator is not the same as the Comparison Operator. what is wrong and where? Link to comment https://forums.phpfreaks.com/topic/292638-check-if-unique-field-exits-or-not/#findComment-1497431 Share on other sites More sharing options...
Barand Posted November 23, 2014 Share Posted November 23, 2014 http://php.net/manual/en/language.operators.php Link to comment https://forums.phpfreaks.com/topic/292638-check-if-unique-field-exits-or-not/#findComment-1497433 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.