|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
Quote:
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. ![]() |
|
#3
|
||||
|
||||
|
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. |
|
#4
|
|||
|
|||
|
Ugh!
Can't believe I missed that. dhouston is right. Try these ideas and let us know if they work. |
|
#5
|
|||
|
|||
|
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 |
![]() |
| Viewing: Dev Articles Community Forums > Programming > PHP Development > PHP inserting blank row |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|