I am trying to teach myself MySQL/PHP from the very beginning. The following code was lifted from various tutorial sites. I'm using phpMyAdmin provided by my webhost.
I made a table with an auto-incrementing field called "ID" and another field called "first" (varchar, limit 30, not null). Then I made a simple form with one text field named "first" and a Submit button. I type my name into the box and click Submit. This does create a row in the database with an ID, but the "first" field is always blank.
I tried replacing '$_POST[first]' with some straight-up words, and that worked - the words appeared in the table with an ID number just fine. That's how I know it is indeed managing to talk to the database, it's just not picking up the text field
After the INSERT statement runs, I have it display all the records in the table. It shows all of the ID numbers and then blank where "first" should be.
I also have it echo the INSERT statement. This is what the echo displays: INSERT INTO tblHurray(ID, first) VALUES ('','')
When I substitute words for '$_POST[first]', the echo looks like this: INSERT INTO tblHurray(ID, first) VALUES ('','words')
This is my first question so please let me know if I've left out any pertinent information! And thanks in advance for your help.
This is the form:
<form action="run_input.php" method="post">
Name: <input type="text" name="first">
<input type="Submit">
</form>
This is what runs when "Submit" is clicked:
<?
include("run_connect.php"); // this connects to the database, this works
$step1 = "INSERT INTO tblHurray(ID, first) VALUES ('','$_POST[first]')";
mysql_query($step1);
echo "$step1";
echo "<b><center>Database Output</center></b><br><br>";
$step2=mysql_query("SELECT * FROM tblHurray");
$num=mysql_numrows($step2);
$i=0;
while ($i < $num) {
$firstname=mysql_result($step2,$i,"first");
$ID=mysql_result($step2,$i,"ID");
echo "$ID: $firstname<br />";
$i++;
}
?>