Forum Moderators: coopster

Message Too Old, No Replies

php mysql query inserts my data multiple times

         

desertwebdesigns

8:27 am on Sep 24, 2007 (gmt 0)

10+ Year Member



I've been trying to execute a php script that inserts data into my MySQL database. The script works fine and inserts the data, but the problem is it inserts the data 3 times (and I've had it insert up to 5). The query is correct as I have had it 'echo' out and it shows the query properly and when I copy it directly into my phpmyadmin SQL section, it executes only one time, so I'm not sure why it enters multiple times when executed through php. The other weird thing is that I have an Apache server running on my computer for testing and sometimes the same script will insert multiple times on my computer, and only one time on my host server and vice versa. Has anyone else ever experienced this problem? Any help would be appreciated. If you'd like to see a snippet of the code, let me know and I can paste it because it is a bit lengthy.

phparion

8:35 am on Sep 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



please post your RELATIVE code only, most probably there is logical problem with your php code and nothing wrong with query, your code calls the query multiple times.

Habtom

8:52 am on Sep 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi desertwebdesigns, Welcome to WebmasterWorld.

Yes, post the relevant part of the code here.

In the mean time, you might need to check if you the query is not in any kind of loop (while, for).

Habtom

desertwebdesigns

5:44 pm on Sep 24, 2007 (gmt 0)

10+ Year Member



I'll have to post the code when I get home from work. I tried to send it to myself at work, but it sent as an embedded file and not as a text attachment. I can tell you this that there is no loop in the code, that was the first thing I checked. I'm not sure where the logic problem lies in my code. The code basically executes on an if statement:

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.

desertwebdesigns

3:43 am on Sep 25, 2007 (gmt 0)

10+ Year Member



Here is a copy of the pertinent code throwing the error.

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());
}
}

desertwebdesigns

4:05 am on Sep 25, 2007 (gmt 0)

10+ Year Member



Here's another weird anomaly. This exact same script is running fine on my test Apache server on my computer. I did not make any changes to the script and it inserts the data only once like I expect it should.

desertwebdesigns

5:13 am on Sep 25, 2007 (gmt 0)

10+ Year Member



Alright, well, don't ask me how the heck this happened, but I reuploaded the script, and it's working fine. I have no idea what's happened or what's changed, but it's working. Thanks anyways for those of you who were going to help out.

phparion

5:30 am on Sep 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



just a small tip for future debugging. It is always witty to print your query. The merit is two folds, first you can see the actual shape of your query as you are dynamically creating query so there could be possible logical errors in your query making process. Secondly if the query is called multiple times it will be printed multiple times and you will be confirmed to track that problem.

desertwebdesigns

6:58 am on Sep 25, 2007 (gmt 0)

10+ Year Member



Thanks for the tip. I mentioned that in the second post I made, that was actually the second thing I tried after looking for a loop. I figured it would echo 3 times if it was entering into the database three times, but it wasn't. It would only echo out once, and the query was fine because I could paste it directly into my SQL console and it would work fine. I have no idea what happened, but it's fixed so I guess I'm happy, lol. Here's hoping it doesn't happen again.