Error: Duplicate Entry '0' for Key 'Primary'

Error: Duplicate Entry '0' for Key 'Primary'

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

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.

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.

17

8 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?

2

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.

0

The error log like (In my case), I'm using Aurora DB:

PHP message: WordPress database error Duplicate entry '0' for key 'PRIMARY' for query INSERT INTO `date173_postmeta

How to fix it using MySQL Workbench:

1- Connect at your DB, and go to the table with the issue, in my case date173_postmeta

2- Select the tools icon:

3- In the windows/tab at right, select the AI checkbox and click on Apply button:

Following the last steps my issues gone.

Try this:

ID int(11) PRIMARY KEY AUTOINCREMENT(1,3)
3

The problem in set ID = $ID

Try removing it so the code should be

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

Be sure to change this where cause it'll update ever row with these values

4

Just make sure that your primery keys are also A-I.

0

I'd been struggling to fix this. My tables had auto increment (AI) switched on Before I started tinkering with records I tried a simple repair in phpMyAdmin. Go to the SQL tab and run each command in turn.

REPAIR TABLE wp_options REPAIR TABLE wp_users REPAIR TABLE wp_usermeta

This did the trick for me and allowed me to login.

i am using phpmyadmin, so go to db , search for wp_postmeta tabel add AI(auto-increment) to meta_id save the changes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Sophia Al-Mansoor
Author

Sophia Al-Mansoor

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.