PHP Coding
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsPHP RelatedPHP Coding

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
  #1  
Old August 7th, 2002, 03:06 AM
olamide olamide is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Plymouth,IN ,USA
Posts: 19 olamide User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to olamide Send a message via Yahoo to olamide
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);


Reply With Quote
  #2  
Old August 7th, 2002, 03:13 AM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 25
RE: insert into mysql Table

I don't see where you are inserting a date into your table?


Reply With Quote
  #3  
Old August 7th, 2002, 03:32 AM
olamide olamide is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Plymouth,IN ,USA
Posts: 19 olamide User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to olamide Send a message via Yahoo to olamide
RE: insert into mysql Table

show me how please. I am new to PHP.

Reply With Quote
  #4  
Old August 7th, 2002, 09:02 AM
siteworkspro.com siteworkspro.com is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Sydney, Australia
Posts: 92 siteworkspro.com User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 3
Send a message via ICQ to siteworkspro.com Send a message via AIM to siteworkspro.com
RE: insert into mysql Table

Could you please post the structure of your database?

Reply With Quote
  #5  
Old August 7th, 2002, 08:07 PM
olamide olamide is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Plymouth,IN ,USA
Posts: 19 olamide User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to olamide Send a message via Yahoo to olamide
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


--------------------------------------------------------------------------------

Reply With Quote
  #6  
Old August 7th, 2002, 08:38 PM
EvilivE EvilivE is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Milwaukee, WI USA
Posts: 291 EvilivE User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 3
Send a message via Yahoo to EvilivE
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

Reply With Quote
  #7  
Old August 8th, 2002, 01:42 AM
olamide olamide is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Plymouth,IN ,USA
Posts: 19 olamide User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to olamide Send a message via Yahoo to olamide
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?

Reply With Quote
  #8  
Old August 8th, 2002, 05:30 AM
bakertrg's Avatar
bakertrg bakertrg is offline
Contributing User
Codewalkers Regular (2000 - 2499 posts)
 
Join Date: Apr 2007
Location: Scottsdale AZ, US
Posts: 2,252 bakertrg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 48 m 45 sec
Reputation Power: 5
Send a message via Yahoo to bakertrg
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:
Original - php Code
  1.  
  2. mysql_query("INSERT INTO database VALUES ('', '$username', '$password', '$userfirstname', '$middlename', '$lastname', '$yourtitle', '$streetaddress', '$streetaddresscontinue', '$city', '$state', '$zipcode', '$country', '$homephone', '$workphone', '$fax', '$email', '$organisation', '$maritalstatus', '$occupation', '$religion', '$homepage', '$others')");


should work,
B

Reply With Quote
  #9  
Old August 8th, 2002, 01:32 PM
DjangoDane DjangoDane is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Atlanta, GA USA
Posts: 1 DjangoDane User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to DjangoDane Send a message via Yahoo to DjangoDane
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.

Reply With Quote
  #10  
Old August 8th, 2002, 05:31 PM
olamide olamide is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Plymouth,IN ,USA
Posts: 19 olamide User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to olamide Send a message via Yahoo to olamide
RE: insert into mysql Table

thanks. I did all waht you guys told me, but the insert still wont work.

Reply With Quote
  #11  
Old August 8th, 2002, 06:02 PM
olamide olamide is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Plymouth,IN ,USA
Posts: 19 olamide User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to olamide Send a message via Yahoo to olamide
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';


Reply With Quote
  #12  
Old August 9th, 2002, 01:05 AM
postalcow postalcow is offline
Codewalkers Beginner (1000 - 1499 posts)
 
Join Date: Apr 2007
Location: Ford CIty, PA USA
Posts: 1,267 postalcow User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 4
Send a message via Yahoo to postalcow
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.

Reply With Quote
  #13  
Old August 9th, 2002, 04:40 AM
olamide olamide is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Plymouth,IN ,USA
Posts: 19 olamide User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to olamide Send a message via Yahoo to olamide
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);

Reply With Quote
  #14  
Old August 9th, 2002, 10:14 AM
EvilivE EvilivE is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Milwaukee, WI USA
Posts: 291 EvilivE User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 3
Send a message via Yahoo to EvilivE
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.

Reply With Quote
  #15  
Old August 9th, 2002, 05:00 PM
olamide olamide is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Plymouth,IN ,USA
Posts: 19 olamide User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to olamide Send a message via Yahoo to olamide
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?

Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP RelatedPHP Coding > insert into mysql Table


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
Create the Optimal Architecture for your Critical Applications
Warburton's the largest independently owned bakery in the UK faced a number of difficult challenges in providing the most robust yet efficient IT infrastructure for their organization's success. IBM's services combined with their xSeries servers created the perfect platform for their SAP environment with sufficient flexibility, and did so in very time effective fashion.

Request Your Free Technology Downloads!
 
Five Best Practices for Deploying a Successful Service-Oriented Architecture
This white paper describes the benefits you can expect with SOA, and how IBM can help take your business there.

Request Your Free Technology Downloads!
 
Gartner Magic Quadrant for Application Delivery Controllers
Gartner summarizes its view on Application Delivery Controllers, evaluates strengths and weaknesses of solutions, and provides Magic Quadrant reporting for a quick comparison across all vendors. Learn from Gartner how you can benefit from an all-in-one device like Citrix NetScaler that delivers the highest levels of availability, performance and security.

Request Your Free Technology Downloads!
 
Knowledge is Power
What you don't know can hurt you, and is likely costing you money and increasing your security risks during an era of scarce resources. This white paper proposes six key strategies that enterprise security managers can use to improve their network defense posture.

Request Your Free Technology Downloads!
 
Rationalizing the Multi-Tool Environment
The rationalized multi-tool approach is flexible, scalable and cost effective. It provides the necessary input to the IT service management business processes. It preserves prior investments in monitoring tools, empowers technologists to select the best tools with which to do their jobs, and enhances effective response to incidents.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2010 by Developer Shed. All rights reserved. DS Cluster 10 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek