Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/267222-help-with-forum-categories-error/
Share on other sites

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..

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.