|
|
|
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
insert into mysql Table
Could someone help me with insertion into mysql table?
Bellow is my code. I want to be able to insert data into my table and them display the inserted date. $dbh=mysql_connect ("localhost", "bizhive_database", "pass") or die ('I cannot connect to the database.'); mysql_select_db ("bizhive_nigeriangreenpage",$dbh); mysql_query("INSERT INTO `database` (`userid`, `username`, `password`, `firstname`, `middlename`, `lastname`, `yourtitle`, `streetaddress`, `streetaddresscontinue`, `city`, `state`, `zipcode`, `country`, `homephone`, `workphone`, `fax`, `email`, `organisation`, `maritalstatus`, `occupation`, `religion`, `others`) VALUES ('', $username,$password,$userfirstname,$middlename,$la stname,$yourtitle,$streetaddress,$streetaddresscon tinue,$city,$state,$zipcode,$country,$homephone,$w orkphone,$fax,$email,$organisation,$maritalstatus, $occupation,$religion,$homepage,$others)"); $result= mysql_query("SELECT * FROM `database` WHERE username=$username,$dbh "); if (!$result) { die("Registration not succed"); } $myrow = msql_fetch_array($result); |
|
#2
|
|||
|
|||
|
RE: insert into mysql Table
I don't see where you are inserting a date into your table?
|
|
#3
|
|||
|
|||
|
RE: insert into mysql Table
show me how please. I am new to PHP.
|
|
#4
|
|||
|
|||
|
RE: insert into mysql Table
Could you please post the structure of your database?
|
|
#5
|
|||
|
|||
|
RE: insert into mysql Table
Am not really sure of what you mean. I am new to database and PHP.
But here are the information I could get bizhive_nigeriangreenpage Users in bizhive_nigeriangreenpage: bizhive_database Database bizhive_nigeriangreenpage - table database running on localhost Database bizhive_nigeriangreenpage - table database running on localhost [ Browse ] [ Select ] [ Insert ] [ Empty ] [ Drop ] Nigerians Database Field Type Attributes Null Default Extra Action userid int(60) No auto_increment Change Drop Primary Index Unique Fulltext username varchar(60) No Change Drop Primary Index Unique Fulltext password varchar(60) No Change Drop Primary Index Unique Fulltext firstname varchar(60) No Change Drop Primary Index Unique Fulltext middlename varchar(60) No Change Drop Primary Index Unique Fulltext lastname varchar(60) No Change Drop Primary Index Unique Fulltext yourtitle varchar(60) No Change Drop Primary Index Unique Fulltext streetaddress varchar(60) No Change Drop Primary Index Unique Fulltext streetaddresscontinue varchar(60) No Change Drop Primary Index Unique Fulltext city varchar(60) No Change Drop Primary Index Unique Fulltext state varchar(60) No Change Drop Primary Index Unique Fulltext zipcode varchar(60) No Change Drop Primary Index Unique Fulltext country varchar(60) No Change Drop Primary Index Unique Fulltext homephone varchar(60) No Change Drop Primary Index Unique Fulltext workphone varchar(60) No Change Drop Primary Index Unique Fulltext fax varchar(60) No Change Drop Primary Index Unique Fulltext email varchar(60) No Change Drop Primary Index Unique Fulltext organisation varchar(60) No Change Drop Primary Index Unique Fulltext maritalstatus varchar(60) No Change Drop Primary Index Unique Fulltext occupation varchar(60) No Change Drop Primary Index Unique Fulltext religion varchar(60) No Change Drop Primary Index Unique Fulltext others varchar(60) No Change Drop Primary Index Unique Fulltext With selected: Or [ Browse ] [ Select ] [ Insert ] [ Empty ] [ Drop ] Indexes : [Documentation] Keyname Type Cardinality Action Field PRIMARY PRIMARY 3 Drop Edit userid username UNIQUE 0 Drop Edit username username_2 INDEX 3 Drop Edit username firstname INDEX 1 Drop Edit firstname middlename INDEX 1 Drop Edit middlename Create an index on columns Space usage : Type Usage Data 148 Bytes Index 6,144 Bytes Total 6,292 Bytes Row Statistic : Statements Value Format dynamic Rows 3 Row length ø 49 Row size ø 2,097 Bytes Next Autoindex 4 -------------------------------------------------------------------------------- |
|
#6
|
|||
|
|||
|
RE: insert into mysql Table
If I counted them correctly, it looks like you have a total of 22 fields in your db and you are inserting all 22 values with your query. If this is true then there is no need to specify the fields as you have done, your query can look like this:
insert into table_name values('one', 'two', 'etc') I also noticed that you have a comma 3 characters after the "S" in VALUES ... it should not be there. HTH |
|
#7
|
|||
|
|||
|
RE: insert into mysql Table
Thanks for your response. I did what you told me, but the code still wont write to my database table.
Any help? |
|
#8
|
|||||
|
|||||
|
RE: insert into mysql Table
The comma should be there, as I assume those are two single quotes to signify sending nothing to the auto-increment field. I also recommend putting single ticks around all the values. Not sure if this is a carryover from ASP but I know it works for me.
php Code:
should work, B |
|
#9
|
|||
|
|||
|
RE: insert into mysql Table
I'm pretty sure you need those ticks because the values you're passing through the variables are strings. If you try to run an INSERT query in mysql you need quotes around strings.
|
|
#10
|
|||
|
|||
|
RE: insert into mysql Table
thanks. I did all waht you guys told me, but the insert still wont work.
|
|
#11
|
|||
|
|||
|
RE: insert into mysql Table
here is my database structure
# # Table structure for table `database` # DROP TABLE IF EXISTS `database`; CREATE TABLE `database` ( `userid` int(60) NOT NULL auto_increment, `username` varchar(60) NOT NULL default '', `password` varchar(60) NOT NULL default '', `firstname` varchar(60) NOT NULL default '', `middlename` varchar(60) NOT NULL default '', `lastname` varchar(60) NOT NULL default '', `yourtitle` varchar(60) NOT NULL default '', `streetaddress` varchar(60) NOT NULL default '', `streetaddresscontinue` varchar(60) NOT NULL default '', `city` varchar(60) NOT NULL default '', `state` varchar(60) NOT NULL default '', `zipcode` varchar(60) NOT NULL default '', `country` varchar(60) NOT NULL default '', `homephone` varchar(60) NOT NULL default '', `workphone` varchar(60) NOT NULL default '', `fax` varchar(60) NOT NULL default '', `email` varchar(60) NOT NULL default '', `organisation` varchar(60) NOT NULL default '', `maritalstatus` varchar(60) NOT NULL default '', `occupation` varchar(60) NOT NULL default '', `religion` varchar(60) NOT NULL default '', `others` varchar(60) NOT NULL default '', PRIMARY KEY (`userid`), UNIQUE KEY `username` (`username`), KEY `username_2` (`username`), KEY `firstname` (`firstname`), KEY `middlename` (`middlename`) ) TYPE=MyISAM COMMENT='Nigerians Database'; |
|
#12
|
|||
|
|||
|
RE: insert into mysql Table
Try it this way. Forget a blank for the auto increment. It will increment on its own forget about it. Also forget about the die message stuff use mysql_error() like I did this will help you debug.
$db_name = "the_db_name"; $tablename = "the_table_name"; $connection = mysql_connect ("localhost", "your_username", "your_password") or die (mysql_error()); $db = mysql_selectdb($db_name, $connection) or die (mysql_error()); $sql = "INSERT INTO $tablename ( field_one, field_two, field_three, field_four )VALUES( "$var_one", "$var_two", "$var_three", "$field_four" )"; $result = mysql_query($sql,$connection) or die (mysql_error()); Let me know if this works for you. |
|
#13
|
|||
|
|||
|
RE: insert into mysql Table
Thanks so much. I could write to the database but I still keep getting error Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource on line 118 with my fetch.
Bellow is my code now. $db_name = "bizhive_nigeriangreenpage"; $tablename = "bizhive_nigeriangreenpage.database"; $connection = mysql_connect ("localhost", "bizhive_database", "pass") or die (mysql_error()); $db = mysql_selectdb($db_name, $connection) or die (mysql_error()); $sql = "INSERT INTO $tablename ( username, password, firstname, middlename, lastname, yourtitle, streetaddress, streetaddresscontinue, city, state, zipcode, country, homephone, workphone, fax, email, organisation, maritalstatus, occupation, religion, homepage, others )VALUES( "$username", "$password", "$firstname", "$middlename", "$lastname", "$yourtitle", "$streetaddress", "$streetaddresscontinue", "$city", "$state", "$zipcode", "$country", "$homephone", "$workphone", "$fax", "$email", "$organisation", "$maritalstatus", "$occupation", "$religion", "$homepage", "$others" )"; $result = mysql_query($sql,$connection) or die (mysql_error()); if (!$result) { die("Registration not succed"); } $myrow = mysql_fetch_array($result); |
|
#14
|
|||
|
|||
|
RE: insert into mysql Table
I recounted your fields from your CREATE TABLE and came up with 22.
As pointed out in a previous reply, the first field is set to AUTO_INCREMENT. Which means that you do not pass it a value the db will take care of it for you. Therefore you should only be trying to insert 21 values into your table (22 fields - 1 auto_increment). However, you are still trying to insert 22 <-- problem isolated! Now where is this extra value that your trying to insert? drum roll please homepage So either stop trying to insert homepage or add that field to the table. To add a field to an existing table, try this: ALTER TABLE table_name ADD homepage varchar(M) NOT NULL Replace 'M' with the amount of characters you would like to use. Hopefully that solves this one. |
|
#15
|
|||
|
|||
|
RE: insert into mysql Table
Thanks. The problem I am having now is fetching from my database. I could now write to the table. But I want to fetch the inserted data for display, but I could not.
Any help? |
![]() |
| Viewing: Codewalkers Forums > PHP Related > PHP Coding > insert into mysql Table |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|