iGrudge.net

2

WordPress php sql running twice

njalk 12/01/2011

Wordpress logoI was making this small feature in php where I want a script to collect links. What I want to do is have a text input box and a submit button. When you hit submin, the link gets added to a database and all the links in the database gets loaded on the same page as the form.

I’m using the plugin Exec-PHP to run a php file as an include on a page. When I run the included file as standalone, without it being integrated in a WordPress page, the script runs fine and does exactly what I want it to do. All that is performed is a simple SELECT statement to extract all the links from the database and show them. When the user press submit, the script checks that the text in the input box is an URL, and adds it to the database with an INSERT statement.

This is where I’ve run into problems. When run as standalone it all works fine, but when I run it inside WordPress it runs the INSERT statement twice. The SELECT statements are only run once though.

For a live demonstration take a look at links inside WordPress and standalone outside WordPress

Is there a reason for this? I was thinking anyway to write this as a plugin to WordPress instead as I’m guessing there are various hooks in WordPress that makes it run twice. We’ll see if that solves the problem.

—————————-

Update:

I thought maybe using WordPress‘s builtin database functions would help me solve the problem, but still the INSERT queries keep being run twice. I came up with a dirty hack, checking if the link was already in the database before I inserted it, wich I was going to implement either way so there is no double posted links. But that deprived me of the ability to give the user feedback on the link being added, or the link existing already in the database. So, I have to come up with another solution to this problem to make it work correctly like I want it to.

Tagged with: , , ,

Comments (2)

  1. PetrUnofAmarmavom 17/10/2011 at 17:25

    Sorry that tuple , but the message is sent to user and reset captcha fails to send, why?

    Reply

Leave a Reply

Login to your account

Can't remember your Password ?

Register for this site!