24

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 0 Database

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.

Shadow
  • 33,525
  • 10
  • 51
  • 64
Arvis
  • 269
  • 1
  • 3
  • 9

8 Answers8

48

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?

Community
  • 1
  • 1
cssyphus
  • 37,875
  • 18
  • 96
  • 111
  • 2
    Also make sure you're on the `right` database. I need coffee. #lame – Alwin Kesler May 04 '18 at 14:48
  • 2
    And you need to remove any row that might have gotten added with an ID of 0 because auto incrementing starts at 1 and it'll try to fix that 0 and make it a 1, but chances are there already is an ID of 1 so you'll see an error about duplicate reindexing. – pinksharpii Sep 28 '20 at 15:06
5

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:

enter image description here

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

enter image description here

Following the last steps my issues gone.

4

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.

symcbean
  • 47,736
  • 6
  • 59
  • 94
3

Try this:

ID int(11) PRIMARY KEY AUTOINCREMENT(1,3)
Stefano Munarini
  • 2,711
  • 2
  • 22
  • 26
  • mh, correct, problem is for $ID, they give the same numbers :/ – Arvis Sep 08 '13 at 22:13
  • $sql="UPDATE `dati` SET `title`= '$titletxt',`value1`='$value1',`value2`='$value2' WHERE `ID`='$ID'"; With this he do nothing, all compile but information wont change. – Arvis Sep 08 '13 at 22:23
  • Please add some explanation to your answer - what **exactly** should be tried, and why should that solve the problem? Keep in mind that such an explanation helps others to understand your answer – Nico Haase Jan 18 '20 at 21:17
2

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

a14m
  • 7,808
  • 8
  • 50
  • 67
  • when i add this $sql="UPDATE `dati` `title`= '$titletxt',`value1`='$value1',`value2`='$value2' WHERE 1"; they change all rows not single, like a copy. – Arvis Sep 08 '13 at 21:47
  • @ArvisAvens that what your query in the code currently does. You just have a `WHERE 1` so you don't limit the update to a row. – t.niese Sep 08 '13 at 21:50
  • @t.niese i need to add LIMIT 1? – Arvis Sep 08 '13 at 21:53
  • @ArvisAvens make it `where ID=$ID` but make sure the id was sent correctly in the post aka `$ID=$_POST['ID']` – a14m Sep 08 '13 at 21:56
2

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

Eyal Sooliman
  • 1,876
  • 23
  • 29
1

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.

CrazyWooki
  • 33
  • 3
0

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

enter image description here

NcXNaV
  • 1,657
  • 4
  • 14
  • 23