Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help, post form insert empty data.

Status
Not open for further replies.

nanao550

Technical User
Nov 8, 2002
12
US
Hello,
I am not sure what is wrong but the message from the post is successful but fields in table are empty. Here is a short version of the script:

If i hardcode the values in as in the line with //$sql = 'INSERT INTO ...' then the field is not empty.

----myinsert.php----
<HTML>
<HEAD><TITLE> </TITLE></HEAD><BODY>
<?PHP
$connection=mysql_connect(&quot;localhost&quot;,&quot;root&quot;,&quot;password&quot;) or die('Could not connect to the database server');
$db = mysql_select_db(&quot;mytestdb&quot;, $connection) or die (&quot;Unable to select database.&quot;);
$sql = &quot;INSERT INTO userdb1 (fname,lname) VALUES ('$fname', '$lname')&quot;;
//$sql = 'INSERT INTO `userdb1` (`fname`, `lname`) VALUES (\'user1f\', \'user1l\');';
$sql_result = mysql_query($sql,$connection) or die ('Could not insert data');
echo('Data inserted successfully.');
mysql_close($connection);
?>
</BODY>
</HTML>

-----myinsert.html-----
<HTML>
<HEAD><TITLE>INSERT Data via PHP</TITLE></HEAD><BODY>
<FORM action=&quot;myinsert.php&quot; method=&quot;post&quot;>
First Name: <input type=&quot;text&quot; name=&quot;fname&quot;><BR>
Last Name: <input type=&quot;text&quot; name=&quot;lname&quot;><BR>
<input type=&quot;submit&quot; value=&quot;Submit to myinsert.php&quot;>
</FORM>
</BODY>
</HTML>

Could someone please tell me why the fields are empty when I post via the form, is there something wrong with my file(s)?

Many thanks.

 
If you print $sql to the browser, do the values appear in the string? ______________________________________________________________________
TANSTAAFL!
 
No, the values are blank:

INSERT INTO forsomedb (fname,lname) VALUES ('','')Data inserted successfully.
 
You probably have the PHP configuration directive &quot;register_globals&quot; set to &quot;off&quot;.

Change the line that constructs your query to read:

$sql = &quot;INSERT INTO userdb1 (fname,lname) VALUES ($_POST['$fname'], $_POST['$lname'])&quot;;


Then print out the query string and see if it looks any better. ______________________________________________________________________
TANSTAAFL!
 
Yes, my register_globals = Off in my php.ini file.
I will check it again with the $_POST.

Thank you
 
Now I am getting a parse error:

Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in...

All I did was copy the line you wrote and paste it in my file and comment out my line.
 
There's multiple PHP errors and at least one SQL error in the snippet I gave you. Sorry about that.

Try:

$sql = &quot;INSERT INTO userdb1 (fname,lname) VALUES ('&quot; . $_POST['fname'] . &quot;', '&quot; . $_POST['lname'] . &quot;')&quot;;
______________________________________________________________________
TANSTAAFL!
 
Wonderful, it is working.
Thanks you for your support.
 
In the last syntax that you've given:

$sql = &quot;INSERT INTO userdb1 (fname,lname) VALUES ('&quot; . $_POST['fname'] . &quot;', '&quot; . $_POST['lname'] . &quot;')&quot;;

If the field of the input is a different datatype would there needs to be any changes or just specifying the type in the .html file only is enough?
 
I find that using $_POST, $_GET, etc makes my code more readable and easier to maintain. It is very explicit in letting me know the source of a variable's data.

If in a complex PHP script I haven't visited in several months, I see a variable $foo, I have to puzzle out where the data in the variable comes from. But if I see $_POST, I know it came from a POST-method form. ______________________________________________________________________
TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top