co.ador Posted July 11, 2011 Share Posted July 11, 2011 Duplicate entry '' for key 'name' what could be causing that error? thank you. Quote Link to comment Share on other sites More sharing options...
gristoi Posted July 11, 2011 Share Posted July 11, 2011 you have a unique or primary key field in your database called name. And you are trying to insert a value that is already in the database Quote Link to comment Share on other sites More sharing options...
Maq Posted July 11, 2011 Share Posted July 11, 2011 Which is blank and is probably the root of your problem. If you show us the relevant code, specifically the query, we can help you. Quote Link to comment Share on other sites More sharing options...
co.ador Posted July 11, 2011 Author Share Posted July 11, 2011 $sql = mysql_query("INSERT INTO products (name, price, details, category, subcategory, category_id, city, state, country, date_added) VALUES('$tname','$price','$details','$category','$subcategory','$category_id','$city','$state','$country', now())") or die (mysql_error()); // Get the inserted ID here to use in the activation email $id = mysql_insert_id(); That's the query where the problem seems to be happening. Also I have a code above this query for validation purposes that goes like if (isset($_POST['Insert'])){ //Connect to the database through our include include "storescripts/connect_to_mysql.php"; // Filter the posted variables $name = ereg_replace("[^A-Za-z0-9]", "", $_POST['name']); // filter everything but numbers and letters $country = ereg_replace("[^A-Z a-z0-9]", "", $_POST['country']); // filter everything but spaces, numbers, and letters $state = ereg_replace("[^A-Z a-z0-9]", "", $_POST['state']); // filter everything but spaces, numbers, and letters $city = ereg_replace("[^A-Z a-z0-9]", "", $_POST['city']); // filter everything but spaces, numbers, and letters $price = $_POST['price']; $details = ereg_replace("[^A-Z a-z0-9]", "", $_POST['details']); // filter everything but spaces, numbers, and letters $category = $_POST['category']; $subcategory = ereg_replace("[^A-Z a-z0-9]", "", $_POST['subcategory']); // filter everything but spaces, numbers, and letters $category_id = $_POST['category_id']; if((!$productname) || (!$country) || (!$state) || (!$city) || (!$price) || (!$details) || (!$subcategory)) { $errorMsg = "You did not submit the following required information!<br /><br />"; if(!$name){ $errorMsg .= "--- User Name"; } else if(!$country){ $errorMsg .= "--- Country"; } else if(!$state){ $errorMsg .= "--- State"; } else if(!$city){ $errorMsg .= "--- City"; } else if(!$price){ $errorMsg .= "--- Price"; } else if(!$details){ $errorMsg .= "--- Details"; } else if(!$subcategory){ $errorMsg .= "--- Subcategory"; } } } else { "fill the spaces in blank please...";} Thank you. Quote Link to comment Share on other sites More sharing options...
Maq Posted July 11, 2011 Share Posted July 11, 2011 Your error is indicating that $tname is blank. Where are you defining it? And use code tags, not m. Quote Link to comment Share on other sites More sharing options...
premiso Posted July 11, 2011 Share Posted July 11, 2011 /* Side Note */ ereg_replace is depreciated. You can easily migrate it to preg_replace, here is an example: preg_replace("#[^A-Za-z0-9]#", "", $_POST['name']); /* End Side Note */ Quote Link to comment Share on other sites More sharing options...
co.ador Posted July 11, 2011 Author Share Posted July 11, 2011 I thought this $name = ereg_replace("[^A-Za-z0-9]", "", $_POST['name']) was defining the variable at this point I think I haven't define the variable #name then I will have to define them. Quote Link to comment Share on other sites More sharing options...
Maq Posted July 11, 2011 Share Posted July 11, 2011 In your query you're using $tname, not $name. Quote Link to comment Share on other sites More sharing options...
co.ador Posted July 12, 2011 Author Share Posted July 12, 2011 The field 'name' was UNIQUE KEY and was not accepting double entries. So what I did was ALTER TABLE Persons DROP INDEX name Drop the UNIQUE index out of name and it did the job guys............ Quote Link to comment Share on other sites More sharing options...
Maq Posted July 12, 2011 Share Posted July 12, 2011 Maybe you wanted an INDEX on multiple columns? Anyway, that still doesn't solve the problem that you were inserting a blank value for 'name'. Quote Link to comment Share on other sites More sharing options...
co.ador Posted July 12, 2011 Author Share Posted July 12, 2011 I never thought that was going to be a problem. But I would like multiple products within that field even when they have the same name they will have different products. 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.