TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 02-26-2008, 11:52 PM   #1 (permalink)
The Contributor
 
Join Date: Jan 2008
Posts: 87
Thanks: 49
StevenF is on a distinguished road
Default 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:
//Creating a query that enters the data into a database
    
$query 'INSERT INTO products SET    cd_artist = "'.mysql_real_escape_string($cd_artist).'",
                                        cd_title = "'
.mysql_real_escape_string($cd_title).'",
                                        cd_price = "'
.mysql_real_escape_string($cd_price).'",
                                        cd_image_name = "'
.mysql_real_escape_string($cd_image_name).'"';
    
    
//execcute a query on a MySQL database
    
mysql_query($query);
    
    
//Message
    
if (mysql_query($query) == ) {
        echo (
"A new row has been added to the database");

    } 
I also tried:

PHP Code:
//Message
    
if (mysql_query($query)) {
        echo (
"A new row has been added to the database");

    } 
No luck. I know this is something very basic, but I can't get it to work

Thanks,
Steven
__________________
My Personal and Photo Blog
StevenF is offline  
Reply With Quote
Old 02-26-2008, 11:53 PM   #2 (permalink)
The Addict
 
Join Date: Nov 2007
Posts: 264
Thanks: 2
TlcAndres is on a distinguished road
Default

I believe mysql_affected_rows() is what you're looking for, look it up on php.net
TlcAndres is offline  
Reply With Quote
The Following User Says Thank You to TlcAndres For This Useful Post:
StevenF (02-27-2008)
Old 02-27-2008, 12:01 AM   #3 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

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 
    mysql_query($query); // first time
     
    //Message 
    if (mysql_query($query) == 1 ) { // and here you go again
, the result being 2 identical rows inserted into the database.
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
The Following User Says Thank You to xenon For This Useful Post:
StevenF (02-27-2008)
Old 02-27-2008, 12:03 AM   #4 (permalink)
The Contributor
 
Join Date: Jan 2008
Posts: 87
Thanks: 49
StevenF is on a distinguished road
Default

Quote:
Originally Posted by TlcAndres View Post
I believe mysql_affected_rows() is what you're looking for, look it up on php.net
I don't think that is what I'm looking for.

Quote:
Originally Posted by xenon
mysql_query returns a resource on success, and false in case of a failure. It doesn't return 1 in any circumstance.
Oh OK. I wasn't sure how it worked, so I thought I would try 1 = true and 0 = false but that didn't work. Do I have the right idea on the second example?

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?
__________________
My Personal and Photo Blog
StevenF is offline  
Reply With Quote
Old 02-27-2008, 12:04 AM   #5 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

As Andres rightfully pointed out:

php Code:
if(mysql_affected_rows() > 0)
{
    echo ("A new row has been added to the database");
}
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
StevenF (02-27-2008)
Old 02-27-2008, 12:12 AM   #6 (permalink)
The Contributor
 
Join Date: Jan 2008
Posts: 87
Thanks: 49
StevenF is on a distinguished road
Default

Quote:
Originally Posted by Wildhoney View Post
As Andres rightfully pointed out:

php Code:
if(mysql_affected_rows() > 0)
{
    echo ("A new row has been added to the database");
}
When I try that, it echos' "A new row has been added to the database" weather the form has been submitted or not.
__________________
My Personal and Photo Blog
StevenF is offline  
Reply With Quote
Old 02-27-2008, 12:35 AM   #7 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
StevenF (02-27-2008)
Old 02-27-2008, 12:46 AM   #8 (permalink)
The Contributor
 
Join Date: Jan 2008
Posts: 87
Thanks: 49
StevenF is on a distinguished road
Default

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).
__________________
My Personal and Photo Blog
StevenF is offline  
Reply With Quote
Old 02-27-2008, 10:21 AM   #9 (permalink)
The Acquainted
 
Join Date: Oct 2007
Posts: 170
Thanks: 18
maZtah is an unknown quantity at this point
Default

You could also do:

PHP Code:
mysql_query($szQuery) or die(mysql_error());
echo 
'Query succesful'
If the query fails it will die with echoing the mysql error, if it succeeds it will echo the message.
maZtah is offline  
Reply With Quote
The Following User Says Thank You to maZtah For This Useful Post:
StevenF (02-27-2008)
Old 02-27-2008, 11:43 AM   #10 (permalink)
The Contributor
 
Join Date: Jan 2008
Posts: 87
Thanks: 49
StevenF is on a distinguished road
Default

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.
__________________
My Personal and Photo Blog
StevenF is offline  
Reply With Quote
Old 02-27-2008, 12:25 PM   #11 (permalink)
The Acquainted
 
freenity's Avatar
 
Join Date: Feb 2008
Posts: 119
Thanks: 17
freenity is on a distinguished road
Default

or just add:

PHP Code:
if (!isset($_POST['submit']))
{
//show form
}
else
{
//insert

and you will need to name your submit button as submit:

<input type="submit" name="submit">
__________________
http://feudal-times.net - My PBB Game
http://gwphp.feudal-times.net - My Blog "Gaming With PHP"
freenity is offline  
Reply With Quote
The Following User Says Thank You to freenity For This Useful Post:
StevenF (02-27-2008)
Old 02-27-2008, 12:52 PM   #12 (permalink)
The Contributor
 
Join Date: Jan 2008
Posts: 87
Thanks: 49
StevenF is on a distinguished road
Default

Thanks Freenity, that really helped! I done something very similar:

PHP Code:
if (isset($_POST['submit']))
{
//execute query 
echo ("A new row has been added to the database");

Thanks for pointing out the isset function to me, it will prove to be very useful!
__________________
My Personal and Photo Blog
StevenF is offline  
Reply With Quote
Old 02-27-2008, 11:55 PM   #13 (permalink)
The Acquainted
 
Join Date: Oct 2007
Posts: 170
Thanks: 18
maZtah is an unknown quantity at this point
Default

Another option would be:

PHP Code:
if ($_POST)
{
    
// query shizzle

maZtah is offline  
Reply With Quote
The Following User Says Thank You to maZtah For This Useful Post:
StevenF (02-27-2008)
Old 02-28-2008, 08:49 AM   #14 (permalink)
The Acquainted
 
Gareth's Avatar
 
Join Date: Jan 2008
Posts: 136
Thanks: 4
Gareth is on a distinguished road
Default

You could also do:

PHP Code:
$qQuery mysql_query(" QUERY ") or die();

if ( 
$qQuery ){
echo 
'inserted';
}{
echo 
'not inserted';

Gareth is offline  
Reply With Quote
The Following User Says Thank You to Gareth For This Useful Post:
StevenF (02-28-2008)
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 09:35 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design