Guest Posted February 18, 2012 Share Posted February 18, 2012 <?php require "global.php"; if ($_POST) { $name = $_POST['name']; $data = sprintf("INSERT INTO forums VALUES (DEFAULT,'$name')"); mysql_query($data); $fid = mysql_insert_id(); header( 'Location: viewforum.php?fid='.$fid); exit; } echo ' <form action="" method="POST"> <table> <tr><td>Forum Name: </td><td><input name="name" /></td></tr> </table> <input type="submit" value=" Add Forum " /> </form>'; ?> This added forums whenever I made one to my MySQL database in the table "forums" For whatever reason, whenever I make a forum, it makes it end in ".php?fid=0" and does not create the forum on the database. Any ideas? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/257257-code-wont-work-anymore/ Share on other sites More sharing options...
gizmola Posted February 18, 2012 Share Posted February 18, 2012 This code is naive and never checks the result of the query to determine if it actually succeeded. If I was placing a bet, my bet would be that a connection to the database is not occurring and this is why it not longer functions. I have a link in my signature to a page that describes how to check database results properly for mysql. Quote Link to comment https://forums.phpfreaks.com/topic/257257-code-wont-work-anymore/#findComment-1318653 Share on other sites More sharing options...
Guest Posted February 18, 2012 Share Posted February 18, 2012 I added the code from your signature, tried making a forum, and got this: Could not successfully run query () from DB: Column count doesn't match value count at row 1 So do I have to edit something on DB or the code to add a forum again? Quote Link to comment https://forums.phpfreaks.com/topic/257257-code-wont-work-anymore/#findComment-1318656 Share on other sites More sharing options...
gizmola Posted February 18, 2012 Share Posted February 18, 2012 What is the database structure. When you do an insert query without specifying columns as you are doing, then the column count must exactly match the values list. insert into table foo values ('a', 'b') By specifying a column list you don't have that issue when omitting values for certain columns: insert into foo (col1, col3) values ('col1 value', 'col3 value') Quote Link to comment https://forums.phpfreaks.com/topic/257257-code-wont-work-anymore/#findComment-1318663 Share on other sites More sharing options...
Guest Posted February 18, 2012 Share Posted February 18, 2012 -- phpMyAdmin SQL Dump -- version 3.4.9 -- http://www.phpmyadmin.net -- -- Host: localhost -- Generation Time: Feb 18, 2012 at 02:17 PM -- Server version: 5.1.56 -- PHP Version: 5.2.9 SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; SET time_zone = "+00:00"; /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8 */; -- -- Database: `(DATABASE)` -- -- -------------------------------------------------------- -- -- Table structure for table `forums` -- CREATE TABLE IF NOT EXISTS `forums` ( `fid` int(10) NOT NULL AUTO_INCREMENT, `name` text NOT NULL, `descs` text, PRIMARY KEY (`fid`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ; -- -- Dumping data for table `forums` -- INSERT INTO `forums` (`fid`, `name`, `descs`) VALUES (1, 'Requests', 'Have a request for TG? Forums, graphics, and anything else.'); /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; Quote Link to comment https://forums.phpfreaks.com/topic/257257-code-wont-work-anymore/#findComment-1318666 Share on other sites More sharing options...
gizmola Posted February 20, 2012 Share Posted February 20, 2012 Yes, as you can see, there is a 'desc' column in the table, but no value is being passed for that. The insert statement needs to be modified accordingly. Quote Link to comment https://forums.phpfreaks.com/topic/257257-code-wont-work-anymore/#findComment-1319054 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.