Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Duplicate entry '0' for key 'PRIMARY'

I can't resolve my problem, this is the error from mysql that I'm getting:

Error: Duplicate entry '0' for key 'PRIMARY'

I can edit and update my data when I've got one record in the database but when I add two rows, I get the error.

Some pictures from database

And when I change the row, row ID goes down to 0 and that's is a problem as I can't edit other rows.

When i try to change row, first row ID goes down to 0Database

enter image description here

CREATE TABLE `dati` (
 `ID` int(11) NOT NULL AUTO_INCREMENT,
 `title` varchar(255) NOT NULL,
 `value1` varchar(255) NOT NULL,
 `value2` varchar(255) NOT NULL,
 PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1 PACK_KEYS=1

Update Code:

<?php // Izlabot datus datubāzē!
$titletxt = $_POST['title_edit'];
$value1 = $_POST['value1_edit'];
$value2 = $_POST['value2_edit'];

if(isset($_POST['edit'])){
$con=mysqli_connect("localhost","root","","dbname");
if (mysqli_connect_errno())
  {
  echo "Neizdevās savienoties ar MySQL: " . mysqli_connect_error();
  }
$sql="UPDATE dati SET ID='$ID',title= '$titletxt',value1='$value1',value2='$value2' WHERE 1";
if (!mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error($con));
  }
echo '<script>
        alert(" Ieraksts ir veiksmīgi labots! ");

        window.location.href = "index.php";
    </script>';
mysqli_close($con);
}
?>

From form:

<?php
            $con=mysqli_connect("localhost","root","","dbname");
            if (mysqli_connect_errno())
              {
              echo "Neizdevās savienoties ar MySQL: " . mysqli_connect_error();
              }
            $result = mysqli_query($con,"SELECT * FROM dati");
            while($row = mysqli_fetch_array($result))
              {
              echo "<tr>";
                  echo "<td><input id='titled' type='text' name='title_edit' value='" . $row['title'] . "'></td>";
                  echo "<td><input id='value1d' type='text' name='value1_edit' value='" . $row['value1'] . "'></td>";
                  echo "<td><input id='value2d' type='text' name='value2_edit' value='" . $row['value2'] . "'></td>";
                  echo "<input type='hidden' name='id' value='" . $row['ID'] . "'>";
                  echo "<td><button name='edit' id='edit_btn' class='frm_btns' value='" . $row['ID'] . "'>Edit</button></td>";
              echo "</tr>";
              }
            mysqli_close($con);
        ?>

It couldn't read the value of ID, as 0 was returned.

like image 808
Arvis Avatar asked Sep 08 '13 21:09

Arvis


People also ask

What is duplicate entry for key primary?

When creating a primary key or unique constraint after loading the data, you can get a “Duplicate entry for key 'PRIMARY'” error. If the data in the source database is valid and there are no any duplicates you should check which collation is used in your MySQL database.

How do I fix error 1062 in MySQL?

1062 - Duplicate Entry To solve this, Set the primary key column as AUTO_INCREMENT . And when you are trying to insert a new row, ignore the primary key column or insert NULL value to primary key.

How do I fix a duplicate entry in MySQL replication?

Try to locate duplicate entry and delete that entry from slave DB. Once you have deleted the old entry then execute stop slave and then start slave on slave DB. Most probably replication will start again and come back to normal. If it gets stuck again for same error for some other record then repeat same steps.

Is duplicate entry of primary key is allowed in SQL?

1 Answer. Easiest explanation: Duplicate values are allowed but not for Primary Key attributes.


2 Answers

For those arriving at this question because of the question title (as I did), this solved my problem:

This error can indicate that the table's PRIMARY KEY is not set to AUTO-INCREMENT, (and your insert query did not specify an ID value).

To resolve:

Check that there is a PRIMARY KEY set on your table, and that the PRIMARY KEY is set to AUTO-INCREMENT.

How to add auto-increment to column in mysql database using phpmyadmin?

like image 144
cssyphus Avatar answered Oct 04 '22 01:10

cssyphus


The problem is that your code attempts to change every row in the data changing the primary key to the value in $ID. This is not set anywhere in your code, and presumably is being cast as 0

$sql="UPDATE `dati` SET `ID`='$ID',`title`= 
'$titletxt',`value1`='$value1',`value2`='$value2' WHERE 1";

The primary key value should be sent to the form and returned so it can be processed by your code, but the value should be retained, hence....

$sql="UPDATE `dati` SET `title`= 
'$titletxt',`value1`='$value1',`value2`='$value2' WHERE `ID`=$ID";

You should also read up on MySQL injection - even after you've fixed the errors here, anyone can do just about anything they want with your database.

like image 36
symcbean Avatar answered Oct 04 '22 02:10

symcbean