Error: Duplicate entry '' for Key 1

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
roondog
Forum Newbie
Posts: 18
Joined: Sun Jul 22, 2007 11:56 am

Error: Duplicate entry '' for Key 1

Post by roondog »

Here is my code

Code: Select all

<?php
include ('./header.html');


$con = mysql_connect("localhost","******","******");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("model", $con);

if (!isset($_POST['submiited'])) {

if ($_POST['gallery'] == 'fashion') {

$sql="INSERT INTO fashion (picname, style)
VALUES
('$_POST[picname]','$_POST[style]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 picture added";

if (($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
&& ($_FILES["file"]["size"] < 500000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "images/fashion/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "images/fashion/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
}
elseif ($_POST['gallery'] == 'glamour') {

$sql="INSERT INTO glamour (picname, style)
VALUES
('$_POST[picname]','$_POST[style]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 picture added";

if (($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
&& ($_FILES["file"]["size"] < 500000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "images/glamour/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "images/glamour/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
  }
  elseif($_POST['gallery'] == 'shows') {
  $sql="INSERT INTO shows (picname, style)
VALUES
('$_POST[picname]','$_POST[style]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 picture added";

if (($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
&& ($_FILES["file"]["size"] < 500000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "images/shows/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "images/shows/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
  }
  elseif($_POST['gallery'] == 'tearsheets') {
  $sql="INSERT INTO tearsheets (picname, style)
VALUES
('$_POST[picname]','$_POST[style]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 picture added";

if (($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
&& ($_FILES["file"]["size"] < 500000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "images/tearsheets/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "images/tearsheets/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
}else{ echo "Back to <a href='./index.php'>admin</a>";
  }
}

mysql_close($con)
?>
<?php
include ('./footer.html');
?>
I'm not really sure whats causing it.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Your database has a primary key (or at least a unique key) field and something is trying to insert the same value twice which is not allowed. The error comes from your database, not your code. Incidentally the string it's trying to use as the key is "" which suggests you need to turn E_ALL error reporting on and you'll probably see an undefined variable or undefined index error:

Code: Select all

<?php error_reporting(E_ALL); ini_set("display_errors", true); ?>
roondog
Forum Newbie
Posts: 18
Joined: Sun Jul 22, 2007 11:56 am

Post by roondog »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


its given me this error: Notice: Undefined index: gallery in C:\wamp\www\model\admin\picadd.php on line 18

which suggests that

Code: Select all

$_POST['gallery']
is causing the error

not sure why though.

here is the form:

Code: Select all

<?php
include ('./header.html');
?>
<form action="./picadd.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<select name="gallery">
<option value="fashion" name="fashion">Fashion</option>
<option value="glamour" name="glamour">Glamour</option>
<option value="shows" name="shows">Shows</option>
<option value="tearsheets" name="tearsheets">Tearsheets</option>
<input value="Upload Picture" type="submit" />
<input type="hidden" value="TRUE" name="submitted" />
<p>
</form>

<?php
include ('./footer.html');
?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Why do you give a name attribute to the options?
User avatar
nykoelle
Forum Newbie
Posts: 22
Joined: Fri Aug 10, 2007 9:05 am
Location: New York

Post by nykoelle »

can you post what your database structure is? including what the pkey is and if its auto incrementing?

also, when you get the error, is it for all the options or just for one or two of them?

you could simplify the size of your script by instead of using the if $_POST['gallery'] = "fashion" by:

Code: Select all

if ($_POST['gallery'] !=NULL) { 

$sql="INSERT INTO ". $_POST['gallery'] ." (picname, style) 
VALUES 
('$_POST[picname]','$_POST[style]')"; 

if (!mysql_query($sql,$con)) 
  { 
  die('Error: ' . mysql_error()); 
  }
right now there's too much for me to read through to see if the issue is everywhere. One thing I do see though, just looking at the if fashion part, is that before you check if the file is a valid filetype, you insert into the database? Shouldn't you want to execute that after you upload the file, because right now even if you don't accept the file, you're inserting into the database, that could be why you're getting duplicate errors.

Just a couple suggestions, let us know what you find
roondog
Forum Newbie
Posts: 18
Joined: Sun Jul 22, 2007 11:56 am

Post by roondog »

If changed it abot a bit and now getting an error of
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\wamp\www\model\admin\picadd.php on line 41

the line is:

Code: Select all

$sql="INSERT INTO ". $_POST['gallery'] ." (picname, style)
VALUES
('$_POST['gallery'] . {$_FILES["file"]["name"]}','$_POST['style']')";
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

roondog wrote:

Code: Select all

$sql="INSERT INTO ". $_POST['gallery'] ." (picname, style)
VALUES
('$_POST['gallery'] . {$_FILES["file"]["name"]}','$_POST['style']')";
Are you looking at the same highlighting I'm looking at? You can't put double quotation makes inside of double quotation marks without escaping them.
roondog
Forum Newbie
Posts: 18
Joined: Sun Jul 22, 2007 11:56 am

Post by roondog »

ok i know have this

Code: Select all

$sql='INSERT INTO '. $_POST['gallery'] .' (picname, style)
VALUES
(''$_POST['gallery'] . {$_FILES["file"]["name]"}','$_POST['style']'')';
but still getting an error, this time its

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in C:\wamp\www\model\admin\picadd.php on line 41

i really am rubbish at this
Z3RO21
Forum Contributor
Posts: 130
Joined: Thu Aug 17, 2006 8:59 am

Post by Z3RO21 »

same problem you are not escaping characters that need escaping (in this case you are using single quotes so all single quotes contained within must be escaped.)
roondog
Forum Newbie
Posts: 18
Joined: Sun Jul 22, 2007 11:56 am

Post by roondog »

I'm really not getting this.
Heres what i have now

Code: Select all

$sql="INSERT INTO ' . $_POST['gallery'] . ' (picname, style)
VALUES
('\'$_POST['gallery'] . {$_FILES[\'file\'][\'name\']}\',\'$_POST['style']\'')";
and heres the error
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\wamp\www\model\admin\picadd.php on line 39
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Swap the double quotes for single quotes. There will be added cleanup, but it should be evident then.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

... It really seems to me as though you aren't understanding what we are telling you. Single quotes and double quotes are different.

Why don't you just simplify the whole thing and stop going back and forth between inlining the data and concatenating it. Pick one.
roondog
Forum Newbie
Posts: 18
Joined: Sun Jul 22, 2007 11:56 am

Post by roondog »

I really am not getting it at all. I'm trying really hard to but i'm clearly not there. I don't know if there is a clearer way to put it. Thanks for trying to help me anyway.

I'm persevering and i now get an unexpected T_VARIABLE using

Code: Select all

$sql="INSERT INTO" $_POST['gallery'] "(picname, style)
VALUES
(' '$_POST['gallery'] . $_FILES['file']['name']','$_POST['style']' ')";
Is there something that tells you what all the errors mean and possible reasons for them, i can't seem to find anything.

Started it all again and finally got it working, didn't need the single quotes around gallery etc. WHich is probably what you were all trying to tell me.
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

Ok i will simplify it for you

Code: Select all

$sql="INSERT INTO". $_POST['gallery']. "(`picname`, `style`)
VALUES
('".$_POST['gallery']. $_FILES['file']['name']."','".$_POST['style']."')";
Post Reply