simmsy Posted August 17, 2012 Share Posted August 17, 2012 Hi im getting an error on my forum its lets me add one category then comes up with this error when trying to add another "ErrorDuplicate entry '0' for key 'PRIMARY'", im sure this is something simple im missing but cant find what? Here is the category code and forum code. Any help is appreciated. forum: <?php //create_cat.php include 'connect.php'; include 'header.php'; $sql = "SELECT categories.cat_id, categories.cat_name, categories.cat_description, COUNT(topics.topic_id) AS topics FROM categories LEFT JOIN topics ON topics.topic_id = categories.cat_id GROUP BY categories.cat_name, categories.cat_description, categories.cat_id"; $result = mysql_query($sql); if(!$result) { echo 'The categories could not be displayed, please try again later.'; } else { if(mysql_num_rows($result) == 0) { echo 'No categories defined yet.'; } else { //prepare the table echo '<table border="1"> <tr> <th>Category</th> <th>Last topic</th> </tr>'; while($row = mysql_fetch_assoc($result)) { echo '<tr>'; echo '<td class="leftpart">'; echo '<h3><a href="category.php?id=' . $row['cat_id'] . '">' . $row['cat_name'] . '</a></h3>' . $row['cat_description']; echo '</td>'; echo '<td class="rightpart">'; //fetch last topic for each cat $topicsql = "SELECT topic_id, topic_subject, topic_date, topic_cat FROM topics WHERE topic_cat = " . $row['cat_id'] . " ORDER BY topic_date DESC LIMIT 1"; $topicsresult = mysql_query($topicsql); if(!$topicsresult) { echo 'Last topic could not be displayed.'; } else { if(mysql_num_rows($topicsresult) == 0) { echo 'no topics'; } else { while($topicrow = mysql_fetch_assoc($topicsresult)) echo '<a href="topic.php?id=' . $topicrow['topic_id'] . '">' . $topicrow['topic_subject'] . '</a> at ' . date('d-m-Y', strtotime($topicrow['topic_date'])); } } echo '</td>'; echo '</tr>'; } } } include 'footer.php'; ?> Cat <?php //create_cat.php include 'connect.php'; include 'header.php'; echo '<br /><br /><br /><h2>Create a category</h2>'; if($_SESSION['signed_in'] == false | $_SESSION['user_level'] != 1 ) { //the user is not an admin echo 'Sorry, you do not have sufficient rights to access this page.'; } else { //the user has admin rights if($_SERVER['REQUEST_METHOD'] != 'POST') { //the form hasn't been posted yet, display it echo '<form method="post" action=""> Category name: <input type="text" name="cat_name" /><br /> Category description:<br /> <textarea name="cat_description" /></textarea><br /><br /> <input type="submit" value="Add category" /> </form>'; } else { //the form has been posted, so save it $sql = "INSERT INTO categories(cat_name, cat_description) VALUES('" . mysql_real_escape_string($_POST['cat_name']) . "', '" . mysql_real_escape_string($_POST['cat_description']) . "')"; $result = mysql_query($sql); if(!$result) { //something went wrong, display the error echo 'Error' . mysql_error(); } else { echo 'New category succesfully added.'; } } } include 'footer.php'; ?> mod edit: code in code tags please Quote Link to comment https://forums.phpfreaks.com/topic/267222-help-with-forum-categories-error/ Share on other sites More sharing options...
Christian F. Posted August 17, 2012 Share Posted August 17, 2012 That error message is stating that you're trying to insert a row with a primary key of 0, when you already have a row with that primary key in the table. Judging by your code, this happens because you have not validated that that $_POST values you're using have indeed been set.. Quote Link to comment https://forums.phpfreaks.com/topic/267222-help-with-forum-categories-error/#findComment-1370133 Share on other sites More sharing options...
ialsoagree Posted August 17, 2012 Share Posted August 17, 2012 Actually, it would seem to me that your category ID column (which is probably your primary key) doesn't auto_increment and therefor you have to submit a unique ID with each new category manually. Quote Link to comment https://forums.phpfreaks.com/topic/267222-help-with-forum-categories-error/#findComment-1370163 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.