mat3000000 Posted August 27, 2011 Share Posted August 27, 2011 I just keep getting INSERT FAIL, but this script used to work. Can anyone see the problem? $name = str_replace('"','"',$_POST['name']); $new = $_POST['new']; $cat = $_POST['cat']; $desc = $_POST['desc']; $desc = str_replace("\n", "<br />",$desc); $desc = str_replace('"','"',$desc); $price = $_POST['price']; $stock = $_POST['stock']; $code = $_POST['code']; if (strpos($price,'£')) {die('You must not include the £ sign in the price field');} if($name==''||$price=='')die("Name and Price need to be filled in."); mysql_query("INSERT INTO `stock` ( `id` , `category`, `name` , `description` , `new` , `price` , `stock` , `customer` , `ProductCode` ) VALUES ('', '$cat', '$name', '$desc', '$new', '$price', '$stock', '0', '$code')")or die("INSERT FAIL"); Quote Link to comment https://forums.phpfreaks.com/topic/245822-mysql-insert/ Share on other sites More sharing options...
Pikachu2000 Posted August 27, 2011 Share Posted August 27, 2011 Echoing mysql_error() would give you a better idea of where the problem is. So would echoing the actual query string. Quote Link to comment https://forums.phpfreaks.com/topic/245822-mysql-insert/#findComment-1262615 Share on other sites More sharing options...
mat3000000 Posted August 27, 2011 Author Share Posted August 27, 2011 The mysql_error returns: Duplicate entry '127' for key 1 Quote Link to comment https://forums.phpfreaks.com/topic/245822-mysql-insert/#findComment-1262635 Share on other sites More sharing options...
DavidAM Posted August 27, 2011 Share Posted August 27, 2011 That indicates you have a column defined with a unique index. You are providing the value "127" for that column, but there is already a row with that value in that column. It looks like your "id" column is defined as AUTO INCREMENT. Is that column also defined as TINYINT? A tinyint is one byte which can range from -128 to +127. If you make it UNSIGNED you could go from 0 to 255. You may need to make that column a larger integer type and, for AUTO INCREMENT, you may as well use UNSIGNED since the system will never assign a negative number you may as well use the extra values allowed. Quote Link to comment https://forums.phpfreaks.com/topic/245822-mysql-insert/#findComment-1262637 Share on other sites More sharing options...
mat3000000 Posted August 27, 2011 Author Share Posted August 27, 2011 Thanks for the help, I knew it wasn't the PHP! Quote Link to comment https://forums.phpfreaks.com/topic/245822-mysql-insert/#findComment-1262643 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.