Click to See Complete Forum and Search --> : MySQL Code not working


Player 11
09-27-2006, 01:47 PM
I have this code for my school site im working on. Its for senior superlative submissions and im using MySQL for it.

I have this code:

// Senior Superlatives Process
if($_POST['super']=="1"){
// Get and Check ID
$id = $_POST['studentid']; // Gets ID from form
$idlength = strlen($id); // Get the length of the ID
if($idlength<=5){ // Check to see if ID is 6 charaters long
header("Location: index.php?action=super&error=id_1"); // If less than or equal to 5, send back with error
exit; // Give it time for the header to work
}
if(isset($_COOKIE["studentid"])){ // If ID Cookie exists then delete it then write it again
setcookie("studentid", $id, time()-3600); // delete ID
setcookie("studentid", $id, time()+3600); // set ID
}else{ // if it doesn't exist write it
setcookie("studentid", $id, time()+3600); // set ID
}

// Get and Check Name
$name = $_POST['studentname'];
$namelength = strlen($name);
if($namelength<=4){ // Check to see if ID is 6 charaters long
header("Location: index.php?action=super&error=name_1"); // If less than or equal to 5, send back with error
exit; // Give it time for the header to work
}
if(isset($_COOKIE["studentname"])){ // If name Cookie exists then delete it then write it again
setcookie("studentname", $name, time()-3600); // delete name
setcookie("studentname", $name, time()+3600); // set name
}else{ // if it doesn't exist write it
setcookie("studentname", $name, time()+3600); // set name
}

// Get and check nominee
$nominee = $_POST['nominee']; // Gets Nominee name from form
if($nominee=="invalid"){
header("Location: index.php?action=super&error=name_2");
exit;
}
if($nominee=="other"){
$other = $_POST['hiddenother'];
$otherlength = strlen($other);
if($otherlength<=4){
header("Location: index.php?action=super&error=name_2");
exit;
}
}

// get and check position
$position = $_POST['position'];
if($position=="invalid"){
header("Location: index.php?action=super&error=pos_1");
exit;
}



// MySQL Section
mysql_connect("db2.awardspace.com", "tristian_12", "master") or die(mysql_error());
mysql_select_db("tristian_12") or die(mysql_error());

// create tables if they don't exist
if(!(mysql_query("SELECT * FROM supervotes$position"))){
mysql_query("CREATE TABLE supervotes$position(
id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), nominee VARCHAR(50), votes INT)") or die(mysql_error());
}
if(!(mysql_query("SELECT * FROM supervotescheck$position"))){
mysql_query("CREATE TABLE supervotescheck$position(
id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), voted VARCHAR(50))") or die(mysql_error());
}

// insert data after checking if they voted already or not
if(!(mysql_query("SELECT * FROM supervotescheck$position WHERE voted = '$id'"))){
$q = "SELECT votes FROM supervotes$position WHERE nominee = $name";
$u = "SELECT id FROM supervotes$position WHERE nominee = $name";
$result = mysql_query($q);
$i = mysql_query($u);
$votes = mysql_result($result,$i,"votes");
$votes1 = $votes + 1;
$query = "UPDATE supervotes$position SET votes = '$votes1' WHERE nominee = '$name'";
if(!(mysql_query($query))){
mysql_query("INSERT INTO `supervoteshair` ( `id` , `nominee` , `position` , `votes` ) VALUES ('', '$name', '$position', '1')");
mysql_query("INSERT INTO `supervotescheckhair` ( `id` , `voted` ) VALUES ('', $id)") or die(mysql_error());
}else{
mysql_query("UPDATE supervotes$position SET votes = '$votes1' WHERE nominee = '$name'");
}
header("Location: index.php?action=view&page=$position");
exit;
}else{
header("Location: index.php?action=super&error=vote_1");
exit;
}
}

But when ever i test it, it all ways executes this line: header("Location: index.php?action=super&error=vote_1");

it creates the tables if they dont exist and it retreives the info ok, so it must be the insertion part

chazzy
09-27-2006, 09:16 PM
the code you have here is very difficult to follow.

just as a tip though, for debugging purposes it's always good to print out errors (if any) so don't do code like this...


if(!mysql_query("something...")){
}


but instead try something like this...

$result = mysql_query("something....");
if(!$result){
echo "MySQL Error: something.... ".mysql_error();
}
else{
//something
}