PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old March 20th, 2004, 04:33 PM
Olster Olster is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 2 Olster User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
PHP inserting blank row

Hi,

I am a newbie to this so please have patience with me

I am trying to write some code in PHP that will insert data into a mysql database. The problem I am encountering is that my code inserts a blank row into the db. Only the auto_increment ID field gets a value. Here is my code:

<<add.html>>

<form action="insert.php" method="post">
Last Name: <input type="text" name="$Name"><br>
First Name: <input type="text" name="$FirstName"><br>
PrimaryPosition: <input type="text" name="$PrimaryPosition"><br>
SecondaryPosition: <input type="text" name="$SecondaryPosition"><br>
<input type="Submit" name="Submit">
</form>


<<insert.php>>

<?
include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query = "INSERT INTO players (Name, FirstName,PrimaryPosition,SecondaryPosition)
VALUES ('$Name','$FirstName','$PrimaryPosition','$Seconda ryPosition')";
$result = mysql_query($query)
or die(mysql_error());
//$result = mysql_query($sql)
echo Done;
mysql_close();
?>


dbinfo.inc.php only contains the username, password for the server as well as the db name.

Somebody help please.

olster

Reply With Quote
  #2  
Old March 22nd, 2004, 11:26 AM
pentapenguin pentapenguin is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 51 pentapenguin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 36 m 7 sec
Reputation Power: 6
Quote:
Originally Posted by Olster
Hi,

I am a newbie to this so please have patience with me

I am trying to write some code in PHP that will insert data into a mysql database. The problem I am encountering is that my code inserts a blank row into the db. Only the auto_increment ID field gets a value. Here is my code:

<<add.html>>

<form action="insert.php" method="post">
Last Name: <input type="text" name="$Name"><br>
First Name: <input type="text" name="$FirstName"><br>
PrimaryPosition: <input type="text" name="$PrimaryPosition"><br>
SecondaryPosition: <input type="text" name="$SecondaryPosition"><br>
<input type="Submit" name="Submit">
</form>


<<insert.php>>

<?
include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query = "INSERT INTO players (Name, FirstName,PrimaryPosition,SecondaryPosition)
VALUES ('$Name','$FirstName','$PrimaryPosition','$Seconda ryPosition')";
$result = mysql_query($query)
or die(mysql_error());
//$result = mysql_query($sql)
echo Done;
mysql_close();
?>


dbinfo.inc.php only contains the username, password for the server as well as the db name.

Somebody help please.

olster


1. "@mysql_select_db($database) or die( "Unable to select database");" needs to have the "@" removed since it has a "die" command
2. all info from the form is accessed by $_POST["variablename"]

Don't take this wrong, but please be sure to search http://php.net before asking a question since they have a ton of good stuff there.

Reply With Quote
  #3  
Old March 22nd, 2004, 12:22 PM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
Also, you're naming your fields in add.html with dollar signs. These won't match up to the fields you're using in insert.php. Remove the dollar signs from add.html and be sure you use the $_POST superglobal in insert.php. In short, rather than using $FirstName, you'll want to use $_POST["FirstName"].
__________________
Please don't PM me asking for solutions outside the scope of a thread.
Keeping all responses in a thread stands to help others who come along later,
which is after all what this forum's all about.

Reply With Quote
  #4  
Old March 22nd, 2004, 02:37 PM
pentapenguin pentapenguin is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 51 pentapenguin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 36 m 7 sec
Reputation Power: 6
Ugh!
Can't believe I missed that.
dhouston is right.
Try these ideas and let us know if they work.

Reply With Quote
  #5  
Old March 22nd, 2004, 03:21 PM
Olster Olster is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 2 Olster User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Working

Hi Guys,

Thanks for the help. I have been trawling the internet for the solution. I have tried a number of different ways of coding it. I even copied and pasted code to no avail, php.net included.

This ended up working:

<<insert.php>>
<?
include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
mysql_select_db($database) or die( "Unable to select database");
$query = "INSERT INTO players (Name, FirstName,PrimaryPosition,SecondaryPosition)
VALUES ('$_POST[Name]','$_POST[FirstName]','$_POST[PrimaryPosition]','$_POST[SecondaryPosition]')";
$result = mysql_query($query)
or die(mysql_error());
echo Done;
mysql_close();
?>

Olster

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingPHP Development > PHP inserting blank row


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


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





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT