![]() |
Basic If Statement Help
Hi guys,
I have what I think is a very basic question: I have a form which inserts data into a database. When the form is submitted (when mysql_query($query) = true) I want to echo "A new row has been added". Here's the code: PHP Code:
PHP Code:
Thanks, Steven |
I believe mysql_affected_rows() is what you're looking for, look it up on php.net
|
mysql_query returns a resource on success, and false in case of a failure. It doesn't return 1 in any circumstance. Also, watch out. You're executing the same query twice:
Code:
//execcute a query on a MySQL database |
Quote:
Quote:
EDIT: Just seen you've added a little to your post. I see... so I'm executing the query twice and inserting the same row twice, DOH!. How else can I check that the query has been executed? |
As Andres rightfully pointed out:
php Code:
|
Quote:
|
It works from the
mysql_query function call. If the mysql_query function call is not false, then mysql_affected_rows will return an integer of how many rows have been affected. If you inserted one row into the database successfully, then the integer 1 would be returned. |
I think I get how it works now:
mysql_affected_rows() checks that mysql_query($query) has been executed. If it has, then the value for mysql_affected_rows() becomes 1. Therefore (mysql_affected_rows() is going to be greater than 0 and should echo "You have inserted a new row into the database". But for some reason it is echoing before the submit button is presses (value should be 0). |
You could also do:
PHP Code:
|
Oh I know what's happening! My PHP and MySQL code is on the same page as the form. So when the page loads, the query is automatically being exexuted and adding blank data into the database, hence why it's always echoing "You have successfully added a new row". I put it all on the one page to keep everything neat and to cut down on pages. I can see now that I will have to have a seperate page for the PHP and MySQL code. :-!
|
or just add:
PHP Code:
<input type="submit" name="submit"> |
Thanks Freenity, that really helped! I done something very similar:
PHP Code:
|
Another option would be:
PHP Code:
|
You could also do:
PHP Code:
|
| All times are GMT. The time now is 09:00 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0