Adding Records to Database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
vicphillips
Forum Newbie
Posts: 2
Joined: Mon Oct 27, 2008 8:59 am

Adding Records to Database

Post by vicphillips »

I will admit that i am just learning and do not have much knowledge....I have setup a database and i am trying to add records to it using a php page i created.... Below. The table is Main Ammonia and the fields are all setup in the table...I can go into the control panel and update records but not by this page... Can someone tell me where i have gone wrong .... It does not give me an error so my dbinfo.inc.php is working correctly.. I have verified this by changing it and then i get the error Unable to Select Database. Thanks



<?
include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");


mysql_query("INSERT INTO Main Ammonia VALUES ('Jack', '2008-10-26', '3', '2')");




mysql_close();
Paul Arnold
Forum Contributor
Posts: 141
Joined: Fri Jun 13, 2008 10:09 am
Location: Newcastle Upon Tyne

Re: Adding Records to Database

Post by Paul Arnold »

Code: Select all

 
mysql_query("INSERT INTO Main Ammonia ('field1', 'field2', 'field3', 'field4') VALUES ('Jack', '2008-10-26', '3', '2')");
 
You can't have spaces in the table name either.
vicphillips
Forum Newbie
Posts: 2
Joined: Mon Oct 27, 2008 8:59 am

Re: Adding Records to Database

Post by vicphillips »

It will still not update.......Any other ideas.....I can go to phpadmin and add records but not by clicking on this php update page.
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Adding Records to Database

Post by papa »

User avatar
Chalks
Forum Contributor
Posts: 447
Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana

Re: Adding Records to Database

Post by Chalks »

vicphillips wrote:i get the error Unable to Select Database.

Code: Select all

@mysql_select_db($database) or die( "Unable to select database");
$database is probably not set, or set to an incorrect value. Try this:

Code: Select all

echo $database;
@mysql_select_db($database) or die(mysql_error());
Last edited by Chalks on Mon Oct 27, 2008 11:04 am, edited 1 time in total.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Adding Records to Database

Post by aceconcepts »

Code: Select all

mysql_query("INSERT INTO Main Ammonia ('field1', 'field2', 'field3', 'field4') VALUES ('Jack', '2008-10-26', '3', '2')") or die(mysql_error());
vidyabhushan
Forum Newbie
Posts: 16
Joined: Mon Oct 20, 2008 4:58 pm

Re: Adding Records to Database

Post by vidyabhushan »

is any of the field in database table is primary key???? if yes then make use of table update function....
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Adding Records to Database

Post by aceconcepts »

But if he's trying to INSERt a record then UPDATE won't be of any help at all.
vidyabhushan
Forum Newbie
Posts: 16
Joined: Mon Oct 20, 2008 4:58 pm

Re: Adding Records to Database

Post by vidyabhushan »

if insert fails to execute instead of using die function there we can make use of update function there right???
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Adding Records to Database

Post by aceconcepts »

No
jarnail
Forum Newbie
Posts: 7
Joined: Thu Oct 30, 2008 8:44 am

Re: Adding Records to Database

Post by jarnail »

<?
include("dbinfo.inc.php");
mysql_connect("localhost",$username,$password);
mysql_select_db($database) or die( "Unable to select database");


mysql_query("INSERT INTO Main Ammonia VALUES ('Jack', '2008-10-26', '3', '2')")
or die(mysql_error());



//check fields types. If still not work then send exported table. Everything will be fixed.
Post Reply