Lumikko Posted July 17, 2011 Share Posted July 17, 2011 Hello I have coded many years but this one is killing me I have table and it looks like this: table name: products id | name There is an form which are used to select prodcuts (with jquery autocomplete). I used comma (,) to separate these prodcuts, example: Prodcut 1, Prodcut 2, Product 3, Product 4 Now is tricky part (at least for me ): I need to function to check database that there is same product name's that user has given in a form and return's id and if there is unknown product, then it will be added to database. I have used this code to separate form's input to array: function split_product($string) { $string = preg_split("/[\s]*[,][\s]*/", $string); return $string; } Array looks like this: Array ( [0] => Product 1 [1] => Product 2 [2] => Product 3 [3] => Product 4 ) I have an idea but i still don't get it. Here is my function that i have tryed: function check_product($array) { foreach($array as $data) { // Check is there same product name $check = mysql_num_rows(mysql_query("SELECT name FROM products WHERE name = '$data'")); if($check == "1") { $checkid = mysql_fetch_array(mysql_query("SELECT id FROM prodcuts WHERE name ='$data'")); $id = $checkid['id']; return $id; } else { mysql_query("INSERT INTO products VALUES ('', '$data')"); } } } Of course, code don't work :-\ Quote Link to comment Share on other sites More sharing options...
trq Posted July 17, 2011 Share Posted July 17, 2011 Firstly, use explode to create your array. Secondly, why are you executing two identical queries? Third and most importantly, what debugging have you done? Simply telling us your code doesn't work doesn't help. Quote Link to comment Share on other sites More sharing options...
Lumikko Posted July 17, 2011 Author Share Posted July 17, 2011 Firstly, use explode to create your array. Secondly, why are you executing two identical queries? Third and most importantly, what debugging have you done? Simply telling us your code doesn't work doesn't help. Thanks for the tip. I now use explode to split string to array. I did use two almost same identical queries because.. I don't know I just trying to make it work and then clean it up I use this to give output something: <?php error_reporting(-1); $products = "Prodcut 1, Prodcut 2, Product 3, Product 4, Product 5"; $string = split_product($products); print_r($string); echo "<br />"; $string = check_product($string); print_r($string); Output is: Array ( [0] => Prodcut 1 [1] => Prodcut 2 [2] => Product 3 [3] => Product 4 [4] => Product 5 ) 5 Number 5 (which is bolded) is database id number for Product 1. Quote Link to comment Share on other sites More sharing options...
Lumikko Posted July 17, 2011 Author Share Posted July 17, 2011 Sorry for the douple post, but i got it working. Reason why it did not work was that mysql_fetch_array works only one time so i put it while loop. 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.