Forum Moderators: coopster
if($_POST['submit']=='submit') {
$values = "VALUES(NULL, ";
...statements to compile the query values
eg. (the following statement runs a number of times for all the different POST variables.
if (trim($_POST['event_name'])==NULL) {
$values .= "NULL, ")
}
else {
$name = mysql_real_escape_string($_POST['name']) {
$values .= "'$name', ";
}
... The final post variable check does close the values variable
$query = "INSERT INTO events ".$values;
$insert = mysql_query($query) or die(mysql_error());
}
That's the basic gist of the code. The whole query is encompassed in one if() statement. If I comment out the line that runs the query and place an 'echo $query;' statement in its place, it echo's the query just fine and if I copy and paste it into phpmyadmin, it inserts fine, so there is no problem with generating the query itself. If I then remove the 'echo' statement and remove the comment for the query, it inserts the data multiple times. I know the code above is not exactly as I have it, but I'm not sure why it would insert multiple times based on the simple mysql_query command. If anyone else has experienced this same problem let me know, otherwise I'll post my actual code at home.
P.S. I don't know if it would cause the error, but the above code is nested in some other if() statements that basically check to see if the user is logged in to the site, do the have admin access, are the required fields filled out, etc. I don't think that would cause this error though.
if(isset($_POST['submit'])) {
$event_end=strtotime($_POST['event_end']);
$event_date=strtotime($_POST['event_date']);
if (trim($event_end)!=NULL && $event_end<$event_date) {
$dateerror=TRUE;
}
if(trim($_POST['name'])=='' ¦¦ trim($_POST['event_date'])=='' ¦¦ $dateerror==TRUE) {
$error='Unable to add event. Please be sure you have entered an event name and proper date(s).';
}
else {
$name=mysql_real_escape_string($_POST['name']);
$values="VALUES(NULL, '$name'";
$event_date=date("Y-m-d", strtotime($_POST['event_date']));
$values.=", '$event_date'";
if(trim($_POST['start_time'])=='') {
$values.=", NULL";
}
else {
$start_time=date("H:i:s", strtotime($_POST['start_time']));
$values.=", '$start_time'";
}
if(trim($_POST['end_time'])=='') {
$values.=", NULL";
}
else {
$end_time=date("H:i:s", strtotime($_POST['end_time']));
$values.=", '$end_time'";
}
if(trim($_POST['description'])=='') {
$values.=", NULL";
}
else {
$description=mysql_real_escape_string($_POST['description']);
$values.=", '$description'";
}
$values.=", NULL"; //(this for a category id variable that I have yet to set. until then, all categories are NULL)
if(trim($_POST['event_end'])=='') {
$values.=", NULL";
}
else {
$event_end=date("Y-m-d", strtotime($_POST['event_end']));
$values.=", '$event_end'";
}
if(trim($_POST['location'])=='') {
$values.=", NULL";
}
else {
$location=mysql_real_escape_string($_POST['location']);
$values.=", '$location'";
}
if($_POST['private']=='private') {
$private=1;
}
else {
$private=0;
}
$values.=", '$private')";
$query="INSERT INTO k9_events ".$values;
$insert=mysql_query($query) or die(mysql_error());
}
}