Duplicate entry '' for key 1 mysql error
Posted: Fri Mar 11, 2005 11:11 am
Can anyone please tell me what this error usually means? One of my queries has it. What checks should I do to fix it?
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
$check = "SELECT * FROM users WHERE username='$username'";
$res = mysql_query($check):
$total = mysql_num_rows($res);
if ($total > 0){
echo "Username already taken";
die;
}Code: Select all
<?php
session_start();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Welcome to DIYMusic.com</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#000000" text="#CCCCCC" link="#666666" vlink="#666666">
<?php if(isset($_SESSION['valid_user'])) { ?>
<table width="89%" height="462" border="0">
<tr>
<tr><td width="40%" height="305" valign="top"><img src="Register%20Side%20Bar.jpg" width="290" height="500" border="0" usemap="#Map"></td>
<td width="60%" valign="top"><p>Hello <? echo $_SESSION['valid_user']; ?>, welcome to the backstage area! <a href="logout.php">LOG OUT</a></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<table width="100%" border="0">
<tr>
<form method="post" action="submit.php">
<td width="26%"><p>How Many Band Members are there in your Band?</p>
<p></p></td>
<td width="66%">
<input type="text" name="member" style="width: 20px">
</p>
<td width="2%">
<td width="6%">
<tr>
<td width="26%"><p>How Many Bands have Directly Influenced Your Band?</p></td>
<td><input type="text" name="influences" style="width: 20px">
</td>
<tr>
<td></td>
</tr>
<tr>
<td><p>
<input type="submit" name="Submit" value="Submit Details">
</p></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php
?>
<map name="Map">
<area shape="rect" coords="97,138,198,172" href="index.htm" alt="Go Back to Welcome Page">
<area shape="rect" coords="95,180,196,213" href="login.php" alt="Go Back to Login">
</map>
<?
session_destroy();
}
else
{
session_destroy();
echo'You have to have registered and logged in to view this page, register <a href="Register.php">here</a>';
}
?>
</body>
</html>Code: Select all
<?php
session_start();
include('connect.inc');
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Welcome to DIYMusic.com</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#000000" text="#CCCCCC" link="#666666" vlink="#666666">
<table width="96%" height="1000" border="0">
<tr>
<td width="31%" valign="top"><img src="Register%20Side%20Bar.jpg" width="290" height="500" border="0" usemap="#Map"></td>
<div align="center"><b>Enter the details of each member of your band and each band who has influenced you below:</b></div>
<p></p>
<p><p>
<p></p>
<td width="69%" valign="top"><form action="fileupload.php" method="post">
<b><i>Members:</i></b>
<?php
$i=1;
$loginname = $_SESSION['valid_user'];
$_SESSION['no_of_influences'] = $HTTP_POST_VARS['member'];
$_SESSION['no_of_members'] = $HTTP_POST_VARS['influences'];
while ($i <= $_SESSION['no_of_members'])
{
?>
<table border="0">
<tr><td><p>
<strong>Member</strong></td>
<td><input type="text" name=<?"'member ' . $i . '"?> style="width: 175px">
</td></tr>
<tr><td>
<strong>Role</strong></td>
<td><input type="text" name=<?"'role ' . $i . '"?> style="width: 175px">
</td></tr>
<tr><td>
<strong>Description</strong></td>
<td><input type="text" name= <?"'mdescription ' . $i . '"?> style="width: 175px">
</td></tr>
<?php
$i++;
}
?>
<tr><td><b><i>Influences:</i></b></td></tr>
<?php
$n=1;
while ($n <= $_SESSION['no_of_influences'])
{
echo'<p><tr><td>
<strong>Influence</strong></td>
<td><input type="text" name="influence ' . $n . '" style="width: 175px">
</td></tr>';
echo'<tr><td>
<strong>Description</strong></td>
<td><input type="text" name="description ' . $n . '" style="width: 175px">
</td></tr>';
$n++;
}
if(isset($_POST)) {
$sql = "SELECT band_ID FROM user where login_name = '$loginname'";
$result = mysql_query($sql);
$bandID = mysql_fetch_array($result);
$i=1;
while ($i <= $_SESSION['no_of_members'])
{
$member = $_POST['member ' . $i . ''];
$role= $_POST['role ' . $i . ''];
$mdescription= $_POST['mdescription ' . $i . ''];
$sql = "INSERT INTO `member` (`band_ID`, `member_name`)
VALUES (
'$bandID', '$member'
)";
mysql_query($sql, $dbh) or die("Failed <pre>$sql</pre>".mysql_error());
$memberID = mysql_insert_ID();
$sql = "INSERT INTO `member_role` (`member_ID`, `role`)
VALUES(
'$memberID', '$role'
)";
mysql_query($sql, $dbh) or die("Failed <pre>$sql</pre>".mysql_error());
$sql = "INSERT INTO `role` (`role`, `description`)
VALUES(
'$role', '$description'
)";
mysql_query($sql, $dbh) or die("Failed <pre>$sql</pre>".mysql_error());
$i++;
}
$n=1;
while ($n <= $_SESSION['no_of_influences'])
{
$influence = $_POST['influence ' . $n . ''];
$description= $_POST['description ' . $n . ''];
$sql = "INSERT INTO `bandinfluence` (`band_ID`, `influence`)
VALUES(
'$bandID', '$influence';
)";
mysql_query($sql, $dbh) or die("Failed <pre>$sql</pre>".mysql_error());
$sql = "INSERT INTO `influence` (`influence`, `description`)
VALUES(
'$influence', '$description'
)";
mysql_query($sql, $dbh) or die("Failed <pre>$sql</pre>".mysql_error());
$n++;
}
echo'Your Members and Influences are now in the database, all you need to do now is upload your mp3!';
}
?>
<tr></tr>
<tr><td></td>
<td><input type="submit" name="submit" value="Submit Details" /></td></tr>
</table>
</form>
</table>
</body>
</html>Code: Select all
if(isset($_POST['text1']) && isset($_POST['text2']) && isset($_POST['text3']))
{
//then do something
}Code: Select all
if(strcasecmp($_SERVER['QUERY_METHOD'],'post') == 0)
{
// it's a post
}
else
{
// it isn't a post
}Code: Select all
if(isset($_POSTї'member ' . $i . '']) && isset($_POSTї'role ' . $i . '']) &&
isset($_POSTї'mdescription ' . $i . '']) && isset($_POSTї'influence ' . $n . '']) &&
($_POSTї'description ' . $n . ''])) {Code: Select all
$sql = "SELECT * FROM role WHERE role == $role && description == $mdescription";
$result = mysql_query($sql) or die("Failed <pre>$sql</pre>".mysql_error());
if (mysql_num_rows($result) == 0 ) {
$sql = "INSERT INTO `role` (`role`, `description`)
VALUES(
'$role', '$description'
)";
mysql_query($sql, $dbh) or die("Failed <pre>$sql</pre>".mysql_error());
$i++;
}
}Code: Select all
$sql = "SELECT * FROM influence WHERE influence == $influence && description == $description";
$result = mysql_query($sql) or die("Failed <pre>$sql</pre>".mysql_error());
if (mysql_num_rows($result) == 0 ) {
$sql = "INSERT INTO `influence` (`influence`, `description`)
VALUES(
'$influence', '$description'
)";
mysql_query($sql, $dbh) or die("Failed <pre>$sql</pre>".mysql_error());
$n++;
}Code: Select all
andCode: Select all
tags where approriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]Tried that, still doesn't work.feyd wrote:isset() will only really help you detect if the page request is a post versus get with the code above.. If that's all you want to checkCode: Select all
if(strcasecmp($_SERVER['QUERY_METHOD'],'post') == 0) { // it's a post } else { // it isn't a post }
feyd | Please useCode: Select all
andCode: Select all
tags where approriate when posting code. Read: Posting Code in the Forums[/quote] OK will do in future.
Code: Select all
$sql = "SELECT * FROM role WHERE role == $role && description == $mdescription";Code: Select all
$sql = "SELECT * FROM role WHERE role = '".$role."' AND description = '".$mdescription."'";Code: Select all
$sql = "SELECT * FROM influence WHERE influence == $influence && description == $description";Code: Select all
$sql = "SELECT * FROM influence WHERE influence = '".$influence."' AND description = '".$description."'";Code: Select all
$_SESSION['no_of_influences'] = $HTTP_POST_VARS['member']; $_SESSION['no_of_members'] = $HTTP_POST_VARS['influences'];Code: Select all
$_SESSION['no_of_influences'] = $_POST['member']; $_SESSION['no_of_members'] = $_POST['influences'];Code: Select all
$_SESSION['no_of_influences'] = $_POST['member'];
$_SESSION['no_of_members'] = $_POST['influences'];Code: Select all
$_SESSION['no_of_influences'] = $_POST['influences'];
$_SESSION['no_of_members'] = $_POST['member'];Code: Select all
if(isset($_POST['member ' . $i . '']) && isset($_POST['role ' . $i . '']) && isset($_POST['mdescription ' . $i . '']) && isset($_POST['influence ' . $n . '']) &&($_POST['description ' . $n . ''])) {