Web Hosting Talk







View Full Version : Simple PHP scripit.. why wont it work! :'(


JDF
04-23-2002, 10:56 AM
Here it is.. quite simple. Taking into consideration the following example url, the paragraph below it should make sense.. :p Example url: http://www.mysite.com/out.php?url=www.yoursite.com .

What I'm trying to get it to do is if the url supplied in the url exists, update the clicks column, else create a new record with the url provided in the url.. hope that makes sense! It works fine if you supply a url already in the database, but does nothing if you try to pass it a url that's not in the database. Anyway, here goes:


$select_query="SELECT url FROM $db_table WHERE url='$url'";
$update_query="UPDATE $db_table SET clicks=clicks+1 WHERE url='$url'";
$insert_query="INSERT INTO $db_table ( url,category,clicks ) values( '$url', 'regular link','1' )";

mysql_connect( $db_host, $db_user, $db_pass ) or die( "Something in the connect phase" );
mysql_select_db( $db_name ) or die( "Something in the select db phase" );

if(!($select_result=mysql_query( $select_query )))
{

if(!($insert_query=mysql_query( $insert_query )))
{

echo "The insert didnt work! :(";

}
} else
{

if(!($update_result=mysql_query( $update_query )))
{

echo "Something went wrong when trying to update the record in the database.";

}
}

mysql_close();


Any ideas? I get no error messages what-so-ever so I have nothing to work with.

oodie
04-23-2002, 11:30 AM
Even when $url is not in the DB, your query is still valid but it returns 0 (empty) record. That's why your insert query never runs. Try to change it to


$select_result = mysql_query($select_query);
if (mysql_num_rows($select_result) == 0) {

// means $url is not in the database
// insert url

} else {

// update url

}

Noldar
04-23-2002, 11:32 AM
mysql_query always returns TRUE if the query was executed successfully even if no rows were returned by the query. You'll need to do a mysql_fetch to determine if the record exists or not.

<edit>
or use mysql_num_rows as oodie suggested. Got beat on the submit button :)
</edit>

Richard

erapid
04-23-2002, 12:03 PM
Originally posted by JDF
Here it is.. quite simple. Taking into consideration the following example url, the paragraph below it should make sense.. :p Example url: http://www.mysite.com/out.php?url=www.yoursite.com .

It works fine if you supply a url already in the database, but does nothing if you try to pass it a url that's not in the database.


Absolutely right. Your mistake is that $select_query always "not equal 0 or false". $select_query is a "resource". So, you get else block anyway.


$select_query="SELECT count(url) FROM $db_table WHERE url='$url'"; << more speedy

$update_query="UPDATE $db_table SET clicks=clicks+1 WHERE url='$url'";
$insert_query="INSERT INTO $db_table ( url,category,clicks ) values( '$url', 'regular link','1' )";

...
$select_result = mysql_query( $select_query );
if ( mysql_num_rows ( $select_result) == 0 ) {

$insert_query=mysql_query( $insert_query ) or die( "The insert didnt work! :(");
}
else {

$update_result=mysql_query( $update_query ) or die("Something went wrong when trying to update the record in the database.");
}



I get no error messages what-so-ever so I have nothing to work with. :)

JDF
04-23-2002, 12:42 PM
There we go! Thank you all! It works and I learned something :D.

smacx
04-23-2002, 01:20 PM
Next time check out phpbuilder.com
their forum is great for this kinda stuff