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); ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted November 22, 2014 Share Posted November 22, 2014 (edited) 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 Edited November 22, 2014 by Barand Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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? Quote Link to comment 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? Quote Link to comment 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 Quote Link to comment Share on other sites More sharing options...
mikosiko Posted November 23, 2014 Share Posted November 23, 2014 (edited) 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 Edited November 23, 2014 by mikosiko Quote Link to comment 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? Quote Link to comment 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.