Jump to content

Form values return blank to database


Johnnyboy123

Recommended Posts

Hey all. I'm a noob so bare with me. Just started studying php and have an assignment due. At this phase of the project I have to create a form where a user can register data and I should retrieve that data and store it in my database. Now I have already created the form (student_reg.php) and a separate file with php code (demo.php)

 

My database is name "registration" and I am trying to put this info in the "student" table. It has about 15 fields but for now I am only trying trying to insert the 1 field of data to test if it works. Dont know if that may cause problems?

 

So far my demo.php connects to the database successfully. The problem I am having is the information I submit does not showing in the database field. However, I have an id field in my case a student number field which is set to auto increment so it automatically updates (1,2,3,4) with each new sumbitted info. When I submit the info the id field gets updated each time with a new row ( so i assume some information is somehow going through ) however the info I entered will remain blank in its field. :banghead:

 

Heres my form (student_reg.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 to retrieve the form info

 

<?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 also recieved a notice "undefined index" or something like that from this part of the code "$value=(isset($_POST['input']));" before I inserted "isset" to the code. I'm not sure if what I did fixed the problem or only hid it, or whether that notice was related to the problem I'm having of not retrieving the info.

 

Haha sorry guys know this is probably childs play for you but I just started out and really need to fix this so I can catch up with this project. Feel free to add tips and comments on other areas. Thanks :mrgreen:

Link to comment
Share on other sites

<?php
if (isset($_POST['submit'])) {

// place $_POST['input'] into a variable
$value = $_POST['input'];

$sql = "INSERT INTO student (sname) VALUES ('$value')";

if (!mysql_query($sql)) {
die ('Error: ' . mysql_error());
    }
}
?>

 

make the following adjustment and you should be good to go.

Link to comment
Share on other sites

When you say "place $_POST['input'] into a variable" we're you just indicating the next line of code or referring that that's what I have to do? :D Cause isn't $_POST['input'] already in variable "$value" and $_POST is a command used to POST or GET the info where as ['input'] indicates the form name of where the info will be retrieved from?

 

( Note I'm not arguing with your methods I'm just checking to see If I understand my code correctly, as I mentioned I'm still very new and used a video tutorial to aid me with this code. Although the code in the tutorial works fine, and even though it's the same, mine doesn't)

 

How will the variable data go about if I put it in one? If that's what you meant.

Otherwise I changed the rest of the code as you presented and it doesn't send any info to the database.

 

Well here's what I did:

<?php
if (isset($_POST['submit'])) {


$value = $_POST['input']; // Isn't this correct? $_POST command to retrieve data and {'input'] to specify name of the form? How did you mean put it into a variable?

$sql = "INSERT INTO student (sname) VALUES ('$value')";

if (!mysql_query($sql)) {
die ('Error: ' . mysql_error());
    }
}
?>

 

Also noticed you didn't close the mysql connection. Is it best in this case not to close it?

 

Thanks for the help, appreciate it :D

Link to comment
Share on other sites

<?php
// The form is using a submit button named submit so we have to check to see if that was triggered by using
// the below if statment with isset()

if (isset($_POST['submit'])) {

// place $_POST['input'] into a variable
// Your form input field is named input so when the submit button is clicked above (if) statment is triggered
// and $_POST contains the information for that input, I'm simply placing it into a variable so I can keep it organized
// and then pass it to the query

$value = mysql_real_escape_string($_POST['input']); // Added some security

$sql = "INSERT INTO student (sname) VALUES ('".$value."')";  // I'm not an expert at sql syntax but I'm going to say it's safe to say $value needs to be escaped.
          mysql_query($sql) or die('Error:' . mysql_error()); // You don't need an IF for mysql_query you can just do it like this

}
mysql_close($link); // Close the connection to the database link
?>

 

I just didn't put the mysql_close in the code I posted, it's always a good practice to close connections when your finished with them.

 

In your code $value = (isset($_POST['input'])) would do the samething but your just dumping whatever the user types into your database and that's a NO NO. By breaking it down you can apply security and error checking much easier. I applied some security to the $value variable in the above code, nothing fancy though. I hope I answered all your questions, if I missed something just let me know!

Link to comment
Share on other sites

Thanks kadeous, you're really giving me a better understanding of the code.

 

I made the adjustments you provided and copied the code. Thanks for the added security. I was going to add the security in the later steps, for now I just want to get this damn thing working and get the info in those fields haha.

 

So heres my whole code now:

 

 
<?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

if (isset($_POST['submit'])) {


$value = mysql_real_escape_string($_POST['input']); 

$sql = "INSERT INTO student (sname) VALUES ('".$value."')";  
          mysql_query($sql) or die('Error:' . mysql_error()); 

}
mysql_close($link); 
  

?>

 

Is this correct? Haha first time I actually attempt to make something with php other than just practicing lines of code and different commands. php is quite difficult, or maybe it's just me haha.

 

Glad to see I'm not getting the undefined index error anymore. It still doesn't send anything through to the database though.

 

@ Zanus the field is set to varchar(40). I assume the fields should be correct as it's a php course I'm doing so they provided us the sql code for setting up our database and fields. So I doubt the problem could be there. Thanks though :D

Link to comment
Share on other sites

errr scratch that still not wroking haha :'(. I added the <INPUT TYPE = "Submit" name="submit" VALUE = "Submit"/>

in the form. Still not working though. Doesn't give through any info in the fields nor does it update the id field. So yeah this my my new form code:

 

<?php
div id="apdiv3">

<FORM action = "demo.php" method ="post ">

<p>Course name:</p>
<INPUT TYPE = "text"  name="input"/> 
<INPUT TYPE = "Submit" name="submit" VALUE = "Submit"/>
</div>
</FORM>
?>

 

 

And my php code is still the same as the last post in which I provided it in.

 

 

Link to comment
Share on other sites

Also. The table I am trying to send this info to has about 15 fields. For now I only tried one field to test if it works correctly. The sname field I am trying to send the info to is the 3rd field. "sno"(the id field) , "cname" , "sname" ....

Just thought I'd share, not sure if this would affect the situation in any way.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.