phpBeginner06 Posted January 25, 2007 Share Posted January 25, 2007 I am trying to automatically insert a new row into data table; if there is no row with specific field value. But the code I have written below is not doing anything at all.Where Am I Going Wrong With This Code?[code]<?phpmysql_connect("localhost","username","password");mysql_select_db("Statistics");$page = $_SERVER['PHP_SELF'];$look4 = "SELECT * FROM hitsTable WHERE page = '$page'";$check = mysql_query($look4);if(mysql_num_rows($check) == 0){mysql_query("INSERT INTO `hitsTable` (visits, page) VALUES ('1', '$page')");}else {echo "logged";} }?>[/code] Link to comment https://forums.phpfreaks.com/topic/35642-how-to-insert-new-row-if-conditions-are-meet/ Share on other sites More sharing options...
trq Posted January 25, 2007 Share Posted January 25, 2007 You should always check your query actually succeeds before trying to use any result. (Also, note the indentation)[code]<?php mysql_connect("localhost","username","password"); mysql_select_db("Statistics"); $page = $_SERVER['PHP_SELF']; $sql = "SELECT * FROM hitsTable WHERE page = '$page'"; if ($result = mysql_query($sql)) { if (!mysql_num_rows($result)) { if ($result = mysql_query("INSERT INTO hitsTable (visits, page) VALUES (1, '$page')")) { echo "Insert success"; } else { echo "Insert failed ".mysql_error(); } } } else { echo "Select failed ".mysql_error(); }?>[/code] Link to comment https://forums.phpfreaks.com/topic/35642-how-to-insert-new-row-if-conditions-are-meet/#findComment-168803 Share on other sites More sharing options...
phpBeginner06 Posted January 25, 2007 Author Share Posted January 25, 2007 [color=blue]Thank You Thorpe that worked out just the way I wanted it to.[/color] Link to comment https://forums.phpfreaks.com/topic/35642-how-to-insert-new-row-if-conditions-are-meet/#findComment-168810 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.