Welcome! Log In Create A New Profile

Advanced

Form values return blank

Posted by Johnny - 72985186 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
avatar Form values return blank
April 19, 2011 12:47PM
Haha left it for awhile and did other stuff now I have to sort this out. Pretty stuck here. I created the form and database etc. I created the php code to connect and retrieve the information, however the values return blank. Although some sort of info is still submitted as the sno field gets updated everytime I submit. Only the sno field with its auto increment has a value the rest of the fields remain blank.
For now I have a seperate form (student_reg.php) and a file with the code (demo.php) know its not the right structure but its only for now until I get it working.

Heres my html form student_reg.php
Language: PHP
<div id="apdiv3">   <FORM action = "demo.php" method ="post ">   <p>Course name:</p> <INPUT TYPE = "text" name="input"/> <INPUT TYPE = "Submit" VALUE = "Submit"/> </div> </FORM>


and heres my php code. The connection to the database is successful, it just doesn't return the values I submit in the form to the database but picks up that something is being submitted as the sno field gets updated each time I submit.
Language: PHP
<?PHP     include ';includes/config.php';;   //connect to database $link=mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);       //error check if (!$link) { die(';could not connect: '; . mysql_error()); }   $db_selected=mysql_select_db(DB_NAME, $link);       // error check if (!$db_selected) { die(';can\t use '; . DB_NAME . ';: '; . mysql_error()); } // Connected to database       // retrieve form data $value=(isset($_POST[';input';]));   $sql = "INSERT INTO student (sname) VALUES (';$value';)";     if (!mysql_query($sql)) { die (';Error: '; . mysql_error()); } mysql_close();     ?>

I used to get a notice "undefined index" originating from my "$value=(isset($_POST['input']));" line. However, that dissapeared
when adding "isset" to the line. Don't know If that solved a problem or is just hiding it but just thought I'd share.

Hope someone can help kinda stuck here.
avatar
Mac
Re: Form values return blank
April 19, 2011 01:01PM
Always do a test for yourself

Language: PHP
$value=(isset($_POST[';input';])); echo "test $value"; //if the value is received and set then it will echo test + value, else just test +. Then you know where you shoud check.

I always say use a full query

Language: PHP
$sql = "INSERT INTO student VALUES (';';, ';$sname';, $fname';, etc.......)"; //ensure you include each column, with '; '; for the auto-increment
avatar Re: Form values return blank
April 19, 2011 08:09PM
Thanks Mac for the tips, yeah I plan on using a full query. For now I just wanted to use 1 field to see if the information is sent through and working properly. I spent so much time trying to figure out the problem on my php code, basicly redid the whole "retrieve data" part of my code so many times and ended up that my problem was in my html form of all places. Forgot to add a name="submit" and had a space in my method e.g method="POST ". Although I never knew html was that case sensitive and picked up on spaces :/ But yeah should be fine.. for now grinning smiley
avatar Re: Form values return blank
April 20, 2011 08:50PM
Hi Mac,

Just catching up on forum posts I missed. So my current code for inserting goes something like this

Language: PHP
$sql = INSERT INTO student (sname, fname, etc...) VALUES (';$sname';, ';$fname';, etc...);

I see you suggest adding a value for every column, even the auto increments. Should it rather be something like this?

Language: PHP
$sql = INSERT INTO student (sno, sname, fname, etc...) VALUES ('; ';, ';$sname';, ';$fname';, etc...);
avatar
Mac
Re: Form values return blank
April 21, 2011 09:22AM
Yes. I would drop the (sno, sname etc) as well. It is a personal preference, but it has served me well over the years.

Language: PHP
$sql = INSERT INTO student VALUES ('; ';, ';$sname';, ';$fname';, etc...);
Sorry, only registered users may post in this forum.

Click here to login