Imaulle Posted May 9, 2011 Share Posted May 9, 2011 Hello, The following code does not return any errors, but nothing is ever inserted into the database. I'm sure it's something super simple :\ $result = mysql_query("SELECT filename FROM p_headerimages ORDER BY ordernum") or die(mysql_error()); $newcount = count($result) +1; $query2 = "INSERT INTO `psitez_wrdp12`.`p_headerimages` (`filename`, `ordernum`) VALUES (\''$file_name'\', \''$newcount'\');"; $result2= mysql_query($query2); Here is the database stuff -- -- Table structure for table `p_headerimages` -- CREATE TABLE IF NOT EXISTS `p_headerimages` ( `filename` varchar(64) NOT NULL, `ordernum` tinyint(3) unsigned NOT NULL, PRIMARY KEY (`ordernum`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1; -- -- Dumping data for table `p_headerimages` -- INSERT INTO `p_headerimages` (`filename`, `ordernum`) VALUES ('slideshow.jpg', 1), ('slideshow1.jpg', 2), ('slideshow2.jpg', 3), ('slideshow3.jpg', 4), ('slideshow4.jpg', 5), ('slideshow5.jpg', 6), ('slideshow6.jpg', 7); any ideas?? Thank you for your help! Quote Link to comment https://forums.phpfreaks.com/topic/235969-phpsql-problem/ Share on other sites More sharing options...
fugix Posted May 9, 2011 Share Posted May 9, 2011 try this $result = mysql_query("SELECT filename FROM p_headerimages ORDER BY ordernum") or die(mysql_error()); $newcount = count($result) +1; $query2 = "INSERT INTO `psitez_wrdp12`.`p_headerimages` (`filename`, `ordernum`) VALUES ('$file_name', '$newcount')"; $result2= mysql_query($query2) or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/235969-phpsql-problem/#findComment-1213110 Share on other sites More sharing options...
Imaulle Posted May 9, 2011 Author Share Posted May 9, 2011 nope :\ I'm thinking it's how I have the table rows set up... argh Quote Link to comment https://forums.phpfreaks.com/topic/235969-phpsql-problem/#findComment-1213113 Share on other sites More sharing options...
fugix Posted May 9, 2011 Share Posted May 9, 2011 you dont receive any errors though? Quote Link to comment https://forums.phpfreaks.com/topic/235969-phpsql-problem/#findComment-1213114 Share on other sites More sharing options...
Imaulle Posted May 10, 2011 Author Share Posted May 10, 2011 as far as I can tell no errors Quote Link to comment https://forums.phpfreaks.com/topic/235969-phpsql-problem/#findComment-1213116 Share on other sites More sharing options...
Imaulle Posted May 10, 2011 Author Share Posted May 10, 2011 this php file is getting ran by some javascript so if the 'or die' is killing the php script I won't see that output correct? Quote Link to comment https://forums.phpfreaks.com/topic/235969-phpsql-problem/#findComment-1213117 Share on other sites More sharing options...
fugix Posted May 10, 2011 Share Posted May 10, 2011 correct, but you would receive an error as well Quote Link to comment https://forums.phpfreaks.com/topic/235969-phpsql-problem/#findComment-1213120 Share on other sites More sharing options...
Imaulle Posted May 10, 2011 Author Share Posted May 10, 2011 When I set ordernum as 'auto-increment' and then do just $query2 = "INSERT INTO p_headerimages (`filename`) VALUES ('$file_name')"; it works, but then the number that gets put into ordernum is not what I want it to be. Do I need to add another value like just id or something? Quote Link to comment https://forums.phpfreaks.com/topic/235969-phpsql-problem/#findComment-1213122 Share on other sites More sharing options...
Imaulle Posted May 10, 2011 Author Share Posted May 10, 2011 oh cool! I removed ordernum as the primary key and it's now getting put into the table! except now '2' is getting put in as ordernum. How do I get count to count the number of entries and not rows? Quote Link to comment https://forums.phpfreaks.com/topic/235969-phpsql-problem/#findComment-1213124 Share on other sites More sharing options...
fugix Posted May 10, 2011 Share Posted May 10, 2011 show me what you have now please Quote Link to comment https://forums.phpfreaks.com/topic/235969-phpsql-problem/#findComment-1213125 Share on other sites More sharing options...
Imaulle Posted May 10, 2011 Author Share Posted May 10, 2011 $result = mysql_query("SELECT filename FROM p_headerimages ORDER BY ordernum") or die(mysql_error()); $newcount = mysql_num_rows($result)+1; $query2 = "INSERT INTO p_headerimages (`filename`, `ordernum`) VALUES ('$file_name', '$newcount')"; $result2= mysql_query($query2) or die(mysql_error()); This is now working. Except the database does not seem to have a key or nothing is sorted. Do I need to worry about that? Quote Link to comment https://forums.phpfreaks.com/topic/235969-phpsql-problem/#findComment-1213127 Share on other sites More sharing options...
fugix Posted May 10, 2011 Share Posted May 10, 2011 its not a huge deal, however it is a good practice to get into to use an auto-incrementing primary key field...for neatness. I normally set a field called "id" as my primary key, and make it auto-incrementing as well Quote Link to comment https://forums.phpfreaks.com/topic/235969-phpsql-problem/#findComment-1213131 Share on other sites More sharing options...
fugix Posted May 10, 2011 Share Posted May 10, 2011 and honestly i would get rid of the ordernum field...i dont see the purpose for it Quote Link to comment https://forums.phpfreaks.com/topic/235969-phpsql-problem/#findComment-1213132 Share on other sites More sharing options...
Imaulle Posted May 10, 2011 Author Share Posted May 10, 2011 ok thank you very much! Quote Link to comment https://forums.phpfreaks.com/topic/235969-phpsql-problem/#findComment-1213133 Share on other sites More sharing options...
fugix Posted May 10, 2011 Share Posted May 10, 2011 ok thank you very much! glad i could help... Quote Link to comment https://forums.phpfreaks.com/topic/235969-phpsql-problem/#findComment-1213134 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.