Page 1 of 1

Error: Duplicate entry '' for Key 1

Posted: Mon Aug 13, 2007 12:55 pm
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.

Posted: Mon Aug 13, 2007 1:11 pm
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); ?>

Posted: Mon Aug 13, 2007 1:28 pm
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]

Posted: Mon Aug 13, 2007 1:33 pm
by superdezign
Why do you give a name attribute to the options?

Posted: Mon Aug 13, 2007 3:00 pm
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

Posted: Sun Aug 19, 2007 9:51 am
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']')";

Posted: Sun Aug 19, 2007 9:55 am
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.

Posted: Sun Aug 19, 2007 10:16 am
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

Posted: Sun Aug 19, 2007 8:25 pm
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.)

Posted: Mon Aug 20, 2007 3:13 pm
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

Posted: Mon Aug 20, 2007 3:16 pm
by feyd
Swap the double quotes for single quotes. There will be added cleanup, but it should be evident then.

Posted: Mon Aug 20, 2007 3:18 pm
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.

Posted: Mon Aug 20, 2007 3:34 pm
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.

Posted: Tue Aug 21, 2007 8:56 am
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']."')";