Jump to content

Duplicate entry '' for key 1


VicHost

Recommended Posts

Hey folks,

 

Sorry for being a pain in the ass.

 

I am trying to submit data to my database via a form and when I click Submit, I get:

 

Duplicate entry '' for key 1

 

I understand that it means I have a duplicate entry with the ID of 1 or something like that. I can't find where the issue is.

 

Here is the form:

 

<form actin="" id="settings" name="settings">
                    <table class="listing form" cellpadding="0" cellspacing="0">
                        <tr>
                            <th class="full" colspan="2"><?php echo $lang_settings; ?></th>
                        </tr>
                        <tr>
                            <th colspan="2"><?php echo $lang_settings_description; ?></th>
                        </tr>
                        <tr>
                           <td><?php echo $lang_sitename; ?>: </td> <td><input type="text" name="sitename" value="<?php echo $site_name; ?>" width="172" /> <em>Site name for logo</em></td>
                        </tr>
                        <tr>
                           <td><?php echo $lang_email; ?>: </td> <td><input type="text" name="email" value="<?php echo $site_email; ?>" width="172" /> <em>Your email address</em></td>
                        </tr>
                        <tr>
                           <td><?php echo $lang_yourname; ?>: </td> <td><input type="text" name="name" value="<?php echo $your_name; ?>" width="172" /> <em>Your own name</em></td>
                        </tr>
                        <tr>
                           <td><?php echo $lang_meta_description; ?>: </td> <td><input type="text" name="meta-description" value="<?php echo $description; ?>" width="172" /> <em>SEO</em></td>
                        </tr>
                        <tr>
                           <td><?php echo $lang_keywords; ?>: </td> <td><input type="text" name="meta-keywords" value="<?php echo $keywords; ?>" width="172" /> <em>Separate with Commas</em></td>
                        </tr>
                       <tr>
                       <td><input type="submit" class="button" name="submit" value="<?php echo $lang_button_savesettings; ?>"></td>
                       </tr>
                    </table>
</form>

 

Here is the Insert code:

$insert = "INSERT INTO settings (site_name, description, keywords, email, name) 
VALUES ('$sitename', '$meta_description', '$meta_keywords', '$site_email', '$your_name')"; 						
mysql_query($insert) or die(mysql_error());

 

Can anyone please tell me where I am going wrong here?

 

Much appreciated.

Link to comment
Share on other sites

Not sure if this is impacting in anyway but I have just noticed at a quick glance:

 

<form actin="" id="settings" name="settings">

 

rather than:

 

<form action="">

 

Maybe it was just a typo by yourself though.

 

Thanks for pointing that out mate. Yeah was just a typo. Has no impact really on the form because I am not sending the data to another file.

Link to comment
Share on other sites

I'm going to guess that your data is empty due to a logic error and that's why you are attempting to insert a (duplicate) empty value.

 

Posting all your code responsible for producing (reproducing) the symptom is the quickest way to get a solution as to what your code is doing.

Link to comment
Share on other sites

Presumably then, there aren't supposed to be any empty values in the INSERT query? Add to the error handling to echo the query string to see if it holds the values you'd expect it to hold.

 

mysql_query($insert) or die( '<br>Query string: ' . $insert . '<br>Produced error: ' . mysql_error() . '<br>');

Link to comment
Share on other sites

I'm going to guess that your data is empty due to a logic error and that's why you are attempting to insert a (duplicate) empty value.

 

Posting all your code responsible for producing (reproducing) the symptom is the quickest way to get a solution as to what your code is doing.

 

Sure mate.

 

Here is my settings.php page:

 

<?php
include "../lang/english.php";
include "../config/config.php";
mysql_connect("$db_hostname", "$db_username", "$db_password") or die(mysql_error());
mysql_select_db("$db_database") or die(mysql_error());

$insert = "INSERT INTO settings (site_name, description, keywords, email, name) 
VALUES ('$sitename', '$meta_description', '$meta_keywords', '$site_email', '$your_name')"; 						
mysql_query($insert) or die(mysql_error());
?>

<!DOCTYPE html>
<html>
<head>
    <title><?php echo $sitename; ?> :: <?php echo $eng_cp; ?> :: Powered by osPHPSite</title>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    <link  href="css/admin.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <div id="main">
        <div id="header">
            <h1><?php echo $sitename; ?></h1>
            <ul id="top-navigation">
                    <li><a href="index.php"><?php echo $lang_button_index; ?></a></li>
                    <li><a href="settings.php" class="active"><?php echo $lang_button_settings; ?></a></li>
                    <li><a href="pages.php"><?php echo $lang_button_pages; ?></a></li>
                    <li><a href="gallery.php"><?php echo $lang_button_gallery; ?></a></li>
                    <li><a href="/"><?php echo $lang_button_viewsite; ?></a></li>
                    <li><a href="logout.php"><?php echo $lang_button_logout; ?></a></li>
            </ul>
        </div>
        <div id="middle">
            <div id="left-column">
                <h3><?php echo $eng_navigation; ?></h3>
                <ul class="nav">
                    <li><a href="index.php"><?php echo $lang_button_index; ?></a></li>
                    <li><a href="settings.php"><?php echo $lang_button_settings; ?></a></li>
                    <li><a href="pages.php"><?php echo $lang_button_pages; ?></a></li>
                    <li><a href="gallery.php"><?php echo $lang_button_gallery; ?></a></li>
                    <li><a href="/"><?php echo $lang_button_viewsite; ?></a></li>
                    <li><a href="logout.php"><?php echo $lang_button_logout; ?></a></li>
                </ul>
                <a href="http://www.osphpsite.com" target="_blank" class="link">osPHPSite</a>
                <a href="http://www.osphpsite.com/forums/" target="_blank" class="link">Support Forums</a>
                <a href="http://www.vichost.com" target="_blank" class="link">VicHost.Com</a>
            </div>
            <div id="center-column">
                <div class="table">
                    <form action="" id="settings" name="settings">
                    <table class="listing form" cellpadding="0" cellspacing="0">
                        <tr>
                            <th class="full" colspan="2"><?php echo $lang_settings; ?></th>
                        </tr>
                        <tr>
                            <th colspan="2"><?php echo $lang_settings_description; ?></th>
                        </tr>
                        <tr>
                           <td><?php echo $lang_sitename; ?>: </td> <td><input type="text" name="sitename" value="<?php echo $site_name; ?>" width="172" /> <em>Site name for logo</em></td>
                        </tr>
                        <tr>
                           <td><?php echo $lang_email; ?>: </td> <td><input type="text" name="email" value="<?php echo $site_email; ?>" width="172" /> <em>Your email address</em></td>
                        </tr>
                        <tr>
                           <td><?php echo $lang_yourname; ?>: </td> <td><input type="text" name="name" value="<?php echo $your_name; ?>" width="172" /> <em>Your own name</em></td>
                        </tr>
                        <tr>
                           <td><?php echo $lang_meta_description; ?>: </td> <td><input type="text" name="meta-description" value="<?php echo $description; ?>" width="172" /> <em>SEO</em></td>
                        </tr>
                        <tr>
                           <td><?php echo $lang_keywords; ?>: </td> <td><input type="text" name="meta-keywords" value="<?php echo $keywords; ?>" width="172" /> <em>Separate with Commas</em></td>
                        </tr>
                       <tr>
                       <td><input type="submit" class="button" name="submit" value="<?php echo $lang_button_savesettings; ?>"></td>
                       </tr>
                    </table>
</form>
                </div>
            </div>
            <div id="right-column">
                <strong class="h"><?php echo $eng_version; ?></strong>
                <div class="box">Current Stable Release:<h3><?php echo $eng_version; ?></h3></div>
            </div>
        </div>
        <div id="footer"><p><?php echo $eng_poweredby; ?><br/>Copyright © 2010 <a href="http://www.vichost.com" target="_blank">VicHost.Com</a>. All Rights Reserved.</p></div>
    </div>
</body>
</html>

 

Not sure if that's all you need.

 

I also notice when I login to phpmyadmin after submitting the data that a second empty row appears.

I am new to this guys so I am unable to figure out why this is happening.

Maybe I should be using an update query rather than an insert?

I dunno.

Link to comment
Share on other sites

Presumably then, there aren't supposed to be any empty values in the INSERT query? Add to the error handling to echo the query string to see if it holds the values you'd expect it to hold.

 

mysql_query($insert) or die( '<br>Query string: ' . $insert . '<br>Produced error: ' . mysql_error() . '<br>');

 

hehe Got this:

 

Query string: INSERT INTO settings (site_name, description, keywords, email, name) VALUES ('', '', '', '', '')
Produced error: Duplicate entry '' for key 1

 

Like, WTF?

Link to comment
Share on other sites

There's no code in your code setting the $sitename, $meta_description, $meta_keywords, $site_email, and $your_name variables, so of course they are all empty.

 

Since your form is using the GET method (the default when no method="..." is specified), you would need to use the correct $_GET['...'] variables to reference the data from the form.

 

Edit: I would add that you should be learning php, developing php code, and debugging php code on a system with error_reporting set to E_ALL and display_errors set to ON so that all the errors that your code is producing will be reported and displayed. There would have been undefined error messages for those variables alerting you to the fact that they are not being set by the code.

Link to comment
Share on other sites

There's no code in your code setting the $sitename, $meta_description, $meta_keywords, $site_email, and $your_name variables, so of course they are all empty.

 

Since your form is using the GET method (the default when no method="..." is specified), you would need to use the correct $_GET['...'] variables to reference the data from the form.

 

Edit: I would add that you should be learning php, developing php code, and debugging php code on a system with error_reporting set to E_ALL and display_errors set to ON so that all the errors that your code is producing will be reported and displayed. There would have been undefined error messages for those variables alerting you to the fact that they are not being set by the code.

 

Sorry mate. I am completely lost. Very sorry. :(

Link to comment
Share on other sites

Hey VicHost,

 

This guy knows far more than me, I know as he has helped me out before!

 

However what I think he is trying to say is:

 

Your variables are coming back empty:

 

e.g. if you wrote echo $sitename there would be not output

 

however if you were to do this:

 

$sitename = "PHP Freaks";

echo $sitename;

 

It would return:

 

PHP Freaks

 

I'm sure that you probably know this, but from I can gather, he is saying this is why you are getting the error.

Link to comment
Share on other sites

In looking at the whole code, you also need to do the following -

 

1) Add some conditional logic to check if the form was submitted before executing the INSERT query. The code currently unconditionally executes the INSERT query every time the page is requested.

 

2) Validate and escape the data that is being put into the query. Had you been validating each expected value, you would have gotten further indication that they were all empty and you would not have been executing the query with empty values.

Link to comment
Share on other sites

The following is some minimal code that accomplishes what you are trying to do, even addressing the duplicate key error that started this thread. The html/form is basically what you posted with the items that I did not have taken out/hard coded. Enjoy -

<?php
include "../lang/english.php";
include "../config/config.php";

$errors = array();
// condition and map inputs
$fields = array('site_name'=>'sitename','site_email'=>'email','your_name'=>'name','meta_description'=>'meta-description','meta_keywords'=>'meta-keywords');
foreach($fields as $var => $field){
$$var = isset($_GET[$field]) ? trim($_GET[$field]) : '';
}
// check if the form was submitted
if(isset($_GET['submit'])){
// connect to/select database
mysql_connect("$db_hostname", "$db_username", "$db_password") or die(mysql_error());
mysql_select_db("$db_database") or die(mysql_error());

// basic validation (not empty) and escape data
foreach($fields as $var => $field){
	if(empty($$var)){
		$errors[] = "The form field: $field, is empty!";
	}
	// escape the data
	$$var = mysql_real_escape_string($$var);
}
// if no validation errors, insert the data
if(empty($errors)){
	$insert = sprintf("INSERT INTO settings (site_name, description, keywords, email, name) VALUES ('%s','%s','%s','%s','%s')",
	$site_name,
	$meta_description,
	$meta_keywords,
	$site_email,
	$your_name
	);
	if(!mysql_query($insert)){
		// a query error occurred
		// check if due to duplicate data
		if(mysql_errno() == 1062){
			// 1062 = duplicate primary key error (your error number might be different depending on your table definition)
			$errors[] = "The site name: $site_name, already exists and cannot be inserted!";
		} else {
			// all other query errors -
			$errors[] = "A database error occurred and your query cannot be processed!";
			trigger_error(mysql_error()); // use error_reporting/display_errors/log_errors to display/log the error condition
		}
	} else {
		echo "The site name: $site_name, was successfully inserted!";
	}
}
}
?>
<!DOCTYPE html>
<html>
<head>
    <title><?php echo $site_name; ?> :: :: Powered by osPHPSite</title>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    <link  href="css/admin.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <div id="main">
        <div id="middle">
            <div id="center-column">
                <div class="table">
			<?php
				if(!empty($errors)){
					echo 'The following errors occurred:<br />';
					foreach($errors as $error){
						echo "$error<br />";
					}
				}
			?>
                    <form action="" id="settings" name="settings">
                    <table class="listing form" cellpadding="0" cellspacing="0">
                        <tr>
                            <th class="full" colspan="2"></th>
                        </tr>
                        <tr>
                            <th colspan="2"></th>
                        </tr>
                        <tr>
                           <td>Site name: </td> <td><input type="text" name="sitename" value="<?php echo $site_name; ?>" width="172" /> <em>Site name for logo</em></td>
                        </tr>
                        <tr>
                           <td>Email: </td> <td><input type="text" name="email" value="<?php echo $site_email; ?>" width="172" /> <em>Your email address</em></td>
                        </tr>
                        <tr>
                           <td>Name: </td> <td><input type="text" name="name" value="<?php echo $your_name; ?>" width="172" /> <em>Your own name</em></td>
                        </tr>
                        <tr>
                           <td>Meta Description: </td> <td><input type="text" name="meta-description" value="<?php echo $meta_description; ?>" width="172" /> <em>SEO</em></td>
                        </tr>
                        <tr>
                           <td>Meta Keywords: </td> <td><input type="text" name="meta-keywords" value="<?php echo $meta_keywords; ?>" width="172" /> <em>Separate with Commas</em></td>
                        </tr>
                       <tr>
                       <td><input type="submit" class="button" name="submit" value="Submit"></td>
                       </tr>
                    </table>
	</form>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

Link to comment
Share on other sites

The following is some minimal code that accomplishes what you are trying to do, even addressing the duplicate key error that started this thread. The html/form is basically what you posted with the items that I did not have taken out/hard coded. Enjoy -

<?php
include "../lang/english.php";
include "../config/config.php";

$errors = array();
// condition and map inputs
$fields = array('site_name'=>'sitename','site_email'=>'email','your_name'=>'name','meta_description'=>'meta-description','meta_keywords'=>'meta-keywords');
foreach($fields as $var => $field){
$$var = isset($_GET[$field]) ? trim($_GET[$field]) : '';
}
// check if the form was submitted
if(isset($_GET['submit'])){
// connect to/select database
mysql_connect("$db_hostname", "$db_username", "$db_password") or die(mysql_error());
mysql_select_db("$db_database") or die(mysql_error());

// basic validation (not empty) and escape data
foreach($fields as $var => $field){
	if(empty($$var)){
		$errors[] = "The form field: $field, is empty!";
	}
	// escape the data
	$$var = mysql_real_escape_string($$var);
}
// if no validation errors, insert the data
if(empty($errors)){
	$insert = sprintf("INSERT INTO settings (site_name, description, keywords, email, name) VALUES ('%s','%s','%s','%s','%s')",
	$site_name,
	$meta_description,
	$meta_keywords,
	$site_email,
	$your_name
	);
	if(!mysql_query($insert)){
		// a query error occurred
		// check if due to duplicate data
		if(mysql_errno() == 1062){
			// 1062 = duplicate primary key error (your error number might be different depending on your table definition)
			$errors[] = "The site name: $site_name, already exists and cannot be inserted!";
		} else {
			// all other query errors -
			$errors[] = "A database error occurred and your query cannot be processed!";
			trigger_error(mysql_error()); // use error_reporting/display_errors/log_errors to display/log the error condition
		}
	} else {
		echo "The site name: $site_name, was successfully inserted!";
	}
}
}
?>
<!DOCTYPE html>
<html>
<head>
    <title><?php echo $site_name; ?> :: :: Powered by osPHPSite</title>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    <link  href="css/admin.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <div id="main">
        <div id="middle">
            <div id="center-column">
                <div class="table">
			<?php
				if(!empty($errors)){
					echo 'The following errors occurred:<br />';
					foreach($errors as $error){
						echo "$error<br />";
					}
				}
			?>
                    <form action="" id="settings" name="settings">
                    <table class="listing form" cellpadding="0" cellspacing="0">
                        <tr>
                            <th class="full" colspan="2"></th>
                        </tr>
                        <tr>
                            <th colspan="2"></th>
                        </tr>
                        <tr>
                           <td>Site name: </td> <td><input type="text" name="sitename" value="<?php echo $site_name; ?>" width="172" /> <em>Site name for logo</em></td>
                        </tr>
                        <tr>
                           <td>Email: </td> <td><input type="text" name="email" value="<?php echo $site_email; ?>" width="172" /> <em>Your email address</em></td>
                        </tr>
                        <tr>
                           <td>Name: </td> <td><input type="text" name="name" value="<?php echo $your_name; ?>" width="172" /> <em>Your own name</em></td>
                        </tr>
                        <tr>
                           <td>Meta Description: </td> <td><input type="text" name="meta-description" value="<?php echo $meta_description; ?>" width="172" /> <em>SEO</em></td>
                        </tr>
                        <tr>
                           <td>Meta Keywords: </td> <td><input type="text" name="meta-keywords" value="<?php echo $meta_keywords; ?>" width="172" /> <em>Separate with Commas</em></td>
                        </tr>
                       <tr>
                       <td><input type="submit" class="button" name="submit" value="Submit"></td>
                       </tr>
                    </table>
	</form>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

 

Mate, you are a legend. Thank you!  ;)

Link to comment
Share on other sites

I was hoping someone could help me a little further with this.

The above code works perfectly for inserting data. However, where I am seriously stuck, I need to be able to update the inserted data from the form and have it displayed both on the site and in the form fields.

I genually have tried to follow tutorials guys and get errors all the time.

If I can get past this little bit of a truff, it will be half the battle.

 

I hope someone can help.

 

Link to comment
Share on other sites

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.