widget Posted June 9, 2007 Share Posted June 9, 2007 Site uses php and mysql. I have an avatar system where certain avatars are locked until the user unlocks them by going to certain pages of the site. So far ( being the newb I am ) I only know how to INSERT data from a form. Is there a way to do it on page load or similar? I also need it to first check if the data already exists and if not it inserts the data and then displays a message to the user that they have unlocked the avatar. ( not working ) The current code is... $query = mysql_query("SELECT * FROM avatar_blank WHERE 'user_name' = '$username' AND 'avatar_name' = 'ecard'"); $num = mysql_numrows($query); if($num < 1){ mysql_query("INSERT INTO avatar_blank (user_name, avatar_name) VALUES ('$user_name','ecard')"); echo "<table width=300 border=1 cellpadding=4 cellspacing=0 bordercolor=#000000 bgcolor=white><tr><td><img src=/images/avatars/ecard.gif width=80 height=80 hspace=10 align=left><font color=#FF3399 size=3 face=Arial, Helvetica, sans-serif><b>Congratulations</b></font><font size=2 face=Arial, Helvetica, sans-serif><br><br>You can now use ECard as an avatar on the chat forum </font></td></tr></table>"; } Link to comment https://forums.phpfreaks.com/topic/54885-solved-insert-data-on-page-load-please-reply-asap/ Share on other sites More sharing options...
chocopi Posted June 9, 2007 Share Posted June 9, 2007 You have it basically right The only thing you need to change is $num = mysql_numrows($query); to $num = mysql_num_rows($query); to have it on page load change this $num = mysql_num_rows($query); to mysql_num_rows($query); Hope that helps ~ Chocopi Link to comment https://forums.phpfreaks.com/topic/54885-solved-insert-data-on-page-load-please-reply-asap/#findComment-271428 Share on other sites More sharing options...
widget Posted June 9, 2007 Author Share Posted June 9, 2007 ok now im trying this after reading many tutorials and examples... $sql = mysql_query("SELECT * FROM avatar_blank WHERE username = '$username' AND avatar_name = 'ecard'"); $result = mysql_num_rows($sql); if($result=="0") { exit; } else { $sql = mysql_query("INSERT INTO avatar_blank (user_name, avatar_name) VALUES ('$user_name','ecard')"); if(!$sql) { echo "Error performing query: ".mysql_error(); } else { echo "<table width=300 border=1 cellpadding=4 cellspacing=0 bordercolor=#000000 bgcolor=#ffffff><tr><td><img src=/images/ecard.gif width=80 height=80 hspace=10 align=left><font color=#FF3399 size=3 face=Arial, Helvetica, sans-serif><b>Congratulations $username</b></font><font size=2 face=Arial, Helvetica, sans-serif><br><br>You can now use ECard as an avatar on the chat forum </font></td></tr></table>"; } } I get the error... Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/chicka/public_html/cards/processCompose.php on line 34 Link to comment https://forums.phpfreaks.com/topic/54885-solved-insert-data-on-page-load-please-reply-asap/#findComment-271435 Share on other sites More sharing options...
widget Posted June 9, 2007 Author Share Posted June 9, 2007 I just went into phpmyadmin and found it was posting the information to the database but not taking the $username. Thats something im going to have to work out from SESSION or something Link to comment https://forums.phpfreaks.com/topic/54885-solved-insert-data-on-page-load-please-reply-asap/#findComment-271439 Share on other sites More sharing options...
widget Posted June 9, 2007 Author Share Posted June 9, 2007 Ok now tearing my hair out!!! Im desperate here $sql = mysql_query("SELECT * FROM avatar_blank WHERE username = '$username' AND avatar_name = 'ecard'"); $result = mysql_num_rows($sql); if($result=="0") { $sql = mysql_query("INSERT INTO avatar_blank (user_name, avatar_name) VALUES ('$username','ecard')"); if(!$sql) { echo "Error performing query: ".mysql_error(); } else { echo "<table width=300 border=1 cellpadding=4 cellspacing=0 bordercolor=#000000 bgcolor=#ffffff><tr><td><img src=/images/ecard.gif width=80 height=80 hspace=10 align=left><font color=#FF3399 size=3 face=Arial, Helvetica, sans-serif><b>Congratulations $username</b></font><font size=2 face=Arial, Helvetica, sans-serif><br><br>You can now use ECard as an avatar on the chat forum </font></td></tr></table>"; } } ERROR Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/chicka/public_html/cards/processCompose.php on line 34 I always get this num rows error - is there another option? Link to comment https://forums.phpfreaks.com/topic/54885-solved-insert-data-on-page-load-please-reply-asap/#findComment-271450 Share on other sites More sharing options...
chocopi Posted June 9, 2007 Share Posted June 9, 2007 I dont know why it does this take a look at the comments in the manual they might help The Manual Link to comment https://forums.phpfreaks.com/topic/54885-solved-insert-data-on-page-load-please-reply-asap/#findComment-271453 Share on other sites More sharing options...
widget Posted June 9, 2007 Author Share Posted June 9, 2007 Ok solved for those who ever need it heres the code <? $sql = mysql_query("SELECT * FROM avatar_blank WHERE user_name = '$username' AND avatar_name = 'ecard'"); $result = mysql_num_rows($sql); if($result=="0") { $sql = mysql_query("INSERT INTO avatar_blank (user_name, avatar_name) VALUES ('$username','ecard')"); if(!$sql) { echo "Error performing query: ".mysql_error(); } else { echo "<table width=300 border=1 cellpadding=4 cellspacing=0 bordercolor=#000000 bgcolor=#ffffff><tr><td><img src=/avatars/ecard.gif width=80 height=80 hspace=10 align=left><font color=#FF3399 size=3 face=Arial, Helvetica, sans-serif><b>Congratulations $username</b></font><font size=2 face=Arial, Helvetica, sans-serif><br><br>You can now use ECard as an avatar on the chat forum </font></td></tr></table>"; } } ?> Link to comment https://forums.phpfreaks.com/topic/54885-solved-insert-data-on-page-load-please-reply-asap/#findComment-271454 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.