Jump to content

php usage


coop

Recommended Posts

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 tables
mysql_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','johnsmith@somewhere.com','http://www.somewhere.com')";
mysql_query($query);
mysql_close();
?>
[/CODE]
Link to comment
Share on other sites

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','johnsmith@somewhere.com','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','johnsmith@somewhere.com','http://www.somewhere.com')[/code]

or

[code]INSERT INTO contacts (first, last, email, web) VALUES ('John', 'Smith', 'johnsmith@somewhere.com', 'http://www.somewhere.com')[/code]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.