coop Posted December 9, 2006 Share Posted December 9, 2006 Hi all,still finding my feet with php and the basic stuff. I have this code that connects to the database and creates a table then closes the connection. Is it right to then open the connection and populate the tables with infomation. If I try to fill the tables within the code that creates the tables the database is created but the table isn't.I know this is a really basic question, but I'm using phpmyAdmin where I can see the database is created and the tables, but is it possible to see the information I inserted.[CODE]<?php$dbhost ='localhost';$dbusername ='root';$dbpassword = 'root';$dbname = 'first';//connect to the mysql database server.$link = mysql_connect($dbhost,$dbusername,$dbpassword);if(!$link){ die("could bot connect:".mysql_error());}else{ echo"connected to server <br>";}//create database$database = $dbusername."_".$dbname;$query = mysql_query("CREATE DATABASE $database");if(!$query){ die("could not create database:".mysql_error());}else{ echo"database created <br>";}//create tablesmysql_select_db($database)or die("unable to select database".mysql_error());$query="CREATE TABLE contacts (id int(6) NOT NULL auto_increment,first VARCHAR(15) NOT NULL,last VARCHAR(15) NOT NULL,email VARCHAR(30) NOT NULL,web VARCHAR(30) NOT NULL,PRIMARY KEY (id),UNIQUE id (id),KEY id_2 (id))";mysql_query($query);mysql_close();?>////<?php$link = mysql_connect($dbhost,$dbusername,$dbpassword);if(!$link){ die("could bot connect:".mysql_error());}else{ echo"connected to server <br>";}mysql_select_db($database)or die("unable to select database".mysql_error());$query ="INSERT INTO contacts VALUES('John','Smith','[email protected]','http://www.somewhere.com')";mysql_query($query);mysql_close();?>[/CODE] Link to comment https://forums.phpfreaks.com/topic/30037-php-usage/ Share on other sites More sharing options...
hitman6003 Posted December 9, 2006 Share Posted December 9, 2006 Your insert query is generating an error...change:[code]mysql_query($query);[/code]to[code]mysql_query($query) or die(mysql_error());[/code]to see what it is....Look at your table creation, you have these fields: id first last email web. Now look at your insert statement:[code]INSERT INTO contacts VALUES('John','Smith','[email protected]','http://www.somewhere.com')[/code]You have five columns in the table, but you are only inserting four. Change your query to:[code]INSERT INTO contacts VALUES('', 'John','Smith','[email protected]','http://www.somewhere.com')[/code]or[code]INSERT INTO contacts (first, last, email, web) VALUES ('John', 'Smith', '[email protected]', 'http://www.somewhere.com')[/code] Link to comment https://forums.phpfreaks.com/topic/30037-php-usage/#findComment-138135 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.