averageah64 Posted March 18, 2013 Share Posted March 18, 2013 This is my error in my Vote script: Duplicate entry '0' for key 'PRIMARY'. I did changned in phpmyadmin but he automaticly go's to 0 and everytime I wanna vote I get this error. Here's my website code: <?php session_start(); require_once 'assets/config/config.php'; $pageName = "Runelab - Vote Server"; ?> <?php include 'assets/templates/overall/overallHead.php'; ?> <?php include 'assets/templates/navHead.php'; ?> <div id="res_content" > <h2>Vote Your Server</h2> <center> <p>Server name you want to vote.</p> <?php $note = '<form method="GET" action="vote.php"><input name="servername"/><input type="submit" value="Vote" /></form>'; $variable = ""; $ip = $_SERVER['REMOTE_ADDR']; include('assets/config/config.php'); if(isset($_GET['servername'])){ $sn = $_GET['servername']; $username = mysql_real_escape_string($row['username']); $SQL = "SELECT * FROM id WHERE ip = '$ip'"; $result = mysql_query($SQL); while ($db_field = mysql_fetch_assoc($result)) { $variable = $db_field['edate']; } $dates = date("Ymd"); if($variable == $dates){ header( 'location: votes.php?action=false' ); die(); } else { mysql_query("DELETE FROM id WHERE ip = '$ip' AND Name='$sn'"); } $query = mysql_query("SELECT * FROM clients WHERE servername='$sn'"); if(mysql_num_rows($query) != 0) { $SQL = "SELECT * FROM clients WHERE servername='$sn'"; $result = mysql_query($SQL); while ($db_field = mysql_fetch_assoc($result)) { $votes = $db_field['Votes']; $newvotes = $votes + 1; mysql_query("UPDATE clients SET votes = votes + 1 WHERE servername = '$sn'"); $date = date("Ymd"); mysql_query("INSERT INTO id (ip, edate, Name) VALUES('$ip', '$date', '$sn')") or die(mysql_error()); header( 'location: votes.php?action=true' ); die(); } } } else { $note = '<form method="GET" action="vote.php"><input name="servername" /><input type="submit" value="Vote" /></form>'; } ?> <?php echo $note; ?></center></div> <?php include 'assets/templates/overall/overallFoot.php'; ?> and Here's mysql code: CREATE TABLE IF NOT EXISTS `id` ( `ID` int(255) NOT NULL, `ip` varchar(255) COLLATE latin1_general_ci NOT NULL, `edate` varchar(255) COLLATE latin1_general_ci NOT NULL, `Name` varchar(255) COLLATE latin1_general_ci NOT NULL, PRIMARY KEY (`ID`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci; Quote Link to comment https://forums.phpfreaks.com/topic/275834-duplicate-entry-0-for-key-primary/ Share on other sites More sharing options...
Barand Posted March 18, 2013 Share Posted March 18, 2013 Given that error message, you are obviously trying to add a second record with a value of zero for the primary key. What makes it worse is that there shouldn't be one there with that value in the first place Quote Link to comment https://forums.phpfreaks.com/topic/275834-duplicate-entry-0-for-key-primary/#findComment-1419427 Share on other sites More sharing options...
averageah64 Posted March 18, 2013 Author Share Posted March 18, 2013 Hmm, ohkey, what should i do now? edit my tables or my php? Quote Link to comment https://forums.phpfreaks.com/topic/275834-duplicate-entry-0-for-key-primary/#findComment-1419429 Share on other sites More sharing options...
Barand Posted March 18, 2013 Share Posted March 18, 2013 Both. The easiest way to ensure unique ids is to make the id field auto_increment. And somewhere you are writing zero values in your php code. Quote Link to comment https://forums.phpfreaks.com/topic/275834-duplicate-entry-0-for-key-primary/#findComment-1419432 Share on other sites More sharing options...
averageah64 Posted March 18, 2013 Author Share Posted March 18, 2013 Ah, Thank you very much I Fixed it! Quote Link to comment https://forums.phpfreaks.com/topic/275834-duplicate-entry-0-for-key-primary/#findComment-1419435 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.