TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Basic If Statement Help (http://www.talkphp.com/absolute-beginners/2347-basic-if-statement-help.html)

StevenF 02-26-2008 11:52 PM

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

TlcAndres 02-26-2008 11:53 PM

I believe mysql_affected_rows() is what you're looking for, look it up on php.net

xenon 02-27-2008 12:01 AM

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.

StevenF 02-27-2008 12:03 AM

Quote:

Originally Posted by TlcAndres (Post 11492)
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?

Wildhoney 02-27-2008 12:04 AM

As Andres rightfully pointed out:

php Code:
if(mysql_affected_rows() > 0)
{
    echo ("A new row has been added to the database");
}

StevenF 02-27-2008 12:12 AM

Quote:

Originally Posted by Wildhoney (Post 11497)
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.

Wildhoney 02-27-2008 12:35 AM

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.

StevenF 02-27-2008 12:46 AM

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).

maZtah 02-27-2008 10:21 AM

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.

StevenF 02-27-2008 11:43 AM

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. :-!

freenity 02-27-2008 12:25 PM

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">

StevenF 02-27-2008 12:52 PM

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!

maZtah 02-27-2008 11:55 PM

Another option would be:

PHP Code:

if ($_POST)
{
    
// query shizzle


;-)

Gareth 02-28-2008 08:49 AM

You could also do:

PHP Code:

$qQuery mysql_query(" QUERY ") or die();

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




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