pollysal Posted March 29, 2010 Share Posted March 29, 2010 i have a form named:"RegistrationForm.php". when i click the "submit button", there's a data inserted in the database (the prove is, there a new row in the database), but there's no value from user textfield(ic_no,name,email...) that have been inserted. It means like there's a row of data inserted but without value.. p/s- however, when i click the submit button, it successfully directed me to the "BookingForm.php" with all the session value...it's just that there's no data inserted into the database. can someone straighten this up for me? am i doing wrong with the coding with the submit button? here's the code <?php session_start(); ?> <html> <body> <form action="BookingForm.php" method="post"> <p><strong>REGISTRATION FORM</strong></p> <table width="285" border="1"> <tr> <th width="120" scope="row">Ic No :</th> <td width="149"><label> <input type="text" name="ic_no" id="ic_no" value="<?php $ic_no; ?>"> </label></td> </tr> <tr> <th scope="row">Name :</th> <td><label> <input type="text" name="name" id="name" value="<?php $name; ?>"> </label></td> </tr> <tr> <th scope="row">Address :</th> <td><label> <input type="text" name="address" id="address" value="<?php $address; ?>" > </label></td> </tr> <tr> <th scope="row">Contact No :</th> <td><label> <input type="text" name="tel_no" id="tel_no" value="<?php $tel_no; ?>"> </label></td> </tr> <tr> <th scope="row">Email :</th> <td><label> <input type="text" name="email" id="email" value="<?php $email; ?>"> </label></td> </tr> </table> <input type="submit" name="submit" id="submit" value="Submit"> <?php $con = mysql_connect("localhost","root","password"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("ambos", $con); mysql_query("INSERT INTO customer (ic_no, name, address, tel_no, email) VALUES ('$_POST[ic_no]', '$_POST[name]', '$_POST[address]','$_POST[tel_no]','$_POST[email]')"); mysql_close($con); $_SESSION['ic_no']=$ic_no; $_SESSION['name']=$name; $_SESSION['address']=$address; $_SESSION['tel_no']=$tel_no; $_SESSION['email']=$email; ?> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/196853-no-data-inserted-into-database-am-i-wrongdoing-it/ Share on other sites More sharing options...
ChemicalBliss Posted March 29, 2010 Share Posted March 29, 2010 To simplify your script, create the session variables before you insert the database, then use the session variables to insert into the database. That way you can sanitize just he session variables and dont have to worry about doing it again for mysql. -CB- Quote Link to comment https://forums.phpfreaks.com/topic/196853-no-data-inserted-into-database-am-i-wrongdoing-it/#findComment-1033415 Share on other sites More sharing options...
Deoctor Posted March 29, 2010 Share Posted March 29, 2010 changed ur code a little bit try this one.. <?php session_start(); ?> <html> <body> <form action="BookingForm.php" method="post"> <p><strong>REGISTRATION FORM</strong></p> <table width="285" border="1"> <tr> <th width="120" scope="row">Ic No :</th> <td width="149"><label> <input type="text" name="ic_no" id="ic_no" value="<?php $ic_no; ?>"> </label></td> </tr> <tr> <th scope="row">Name :</th> <td><label> <input type="text" name="name" id="name" value="<?php $name; ?>"> </label></td> </tr> <tr> <th scope="row">Address :</th> <td><label> <input type="text" name="address" id="address" value="<?php $address; ?>" > </label></td> </tr> <tr> <th scope="row">Contact No :</th> <td><label> <input type="text" name="tel_no" id="tel_no" value="<?php $tel_no; ?>"> </label></td> </tr> <tr> <th scope="row">Email :</th> <td><label> <input type="text" name="email" id="email" value="<?php $email; ?>"> </label></td> </tr> </table> <input type="submit" name="submit" id="submit" value="Submit"> <?php if(isset($_POST['submit'])) { $ic_no=$_POST['ic_no']; $name=$_POST['name']; $address=$_POST['address']; $tel_no=$_POST['tel_no']; $email=$_POST['email']; $con = mysql_connect("localhost","root","password"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("ambos", $con); mysql_query("INSERT INTO customer (ic_no, name, address, tel_no, email) VALUES ('$ic_no', '$name', '$address','$tel_no','$email')"); mysql_close($con); $_SESSION['ic_no']=$ic_no; $_SESSION['name']=$name; $_SESSION['address']=$address; $_SESSION['tel_no']=$tel_no; $_SESSION['email']=$email; echo "Post Values=> "; print_r($_POST); echo "<br>Session Values=> "; print_r($_SESSION); } ?> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/196853-no-data-inserted-into-database-am-i-wrongdoing-it/#findComment-1033437 Share on other sites More sharing options...
litebearer Posted March 29, 2010 Share Posted March 29, 2010 perhaps being an 'old fart' I just am missing something; however... the form page is "RegistrationForm.php" which POSTS to "BookingForm.php". Since the page is not posting to itself, storing the values to the database should be done in "BookingForm.php"? Make sense? Quote Link to comment https://forums.phpfreaks.com/topic/196853-no-data-inserted-into-database-am-i-wrongdoing-it/#findComment-1033447 Share on other sites More sharing options...
pollysal Posted March 29, 2010 Author Share Posted March 29, 2010 thanks to those who reply. ym_chaitu, i did use the code you give as reference, but it still not work out. Plus, it seems that it doesn't even insert any record into the database. do actually a button can only make a function. I mean that, one button for submit data to the database, and one button to go to the next page? this question might sound stupid, but i'm relatively just learn. so, if anyone please straighten me out if i'm wrong.. Quote Link to comment https://forums.phpfreaks.com/topic/196853-no-data-inserted-into-database-am-i-wrongdoing-it/#findComment-1033450 Share on other sites More sharing options...
PFMaBiSmAd Posted March 29, 2010 Share Posted March 29, 2010 Your original problem is because the php code you have placed in your form is executed unconditionally when the form is displayed. this is what inserts a blank row. When you submit that form, either your code in BookingForm.php is setting the $_SESSION variables or register_globals are on and the $_POST variables with the same name as the $_SESSION variables are automatically setting the $_SESSION variables (which is why register_globals where turned off by default a really long time ago to prevent hacker from being able to set $_SESSION variables.) Since you are using two different pages for the form and the form processing code, the php code that you have in the form page should be removed. The php code in the form processing page is what needs to perform the INSERT query. Quote Link to comment https://forums.phpfreaks.com/topic/196853-no-data-inserted-into-database-am-i-wrongdoing-it/#findComment-1033458 Share on other sites More sharing options...
Deoctor Posted March 29, 2010 Share Posted March 29, 2010 thanks to those who reply. ym_chaitu, i did use the code you give as reference, but it still not work out. Plus, it seems that it doesn't even insert any record into the database. do actually a button can only make a function. I mean that, one button for submit data to the database, and one button to go to the next page? this question might sound stupid, but i'm relatively just learn. so, if anyone please straighten me out if i'm wrong.. if u want to submit the values and redirect to other page u can do some thing like this.. 1.html <html> <body> <form action="BookingForm.php" method="post"> <p><strong>REGISTRATION FORM</strong></p> <table width="285" border="1"> <tr> <th width="120" scope="row">Ic No :</th> <td width="149"><label> <input type="text" name="ic_no" id="ic_no" value="<?php $ic_no; ?>"> </label></td> </tr> <tr> <th scope="row">Name :</th> <td><label> <input type="text" name="name" id="name" value="<?php $name; ?>"> </label></td> </tr> <tr> <th scope="row">Address :</th> <td><label> <input type="text" name="address" id="address" value="<?php $address; ?>" > </label></td> </tr> <tr> <th scope="row">Contact No :</th> <td><label> <input type="text" name="tel_no" id="tel_no" value="<?php $tel_no; ?>"> </label></td> </tr> <tr> <th scope="row">Email :</th> <td><label> <input type="text" name="email" id="email" value="<?php $email; ?>"> </label></td> </tr> </table> <input type="submit" name="submit" id="submit" value="Submit"> </form> </body> </html> and the php file which is used to store values should be a separate one. BookingForm.php <?php if(isset($_POST['submit'])) { $ic_no=$_POST['ic_no']; $name=$_POST['name']; $address=$_POST['address']; $tel_no=$_POST['tel_no']; $email=$_POST['email']; $con = mysql_connect("localhost","root","admin"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("develop", $con); mysql_query("INSERT INTO customer (ic_no, name, address, tel_no, email) VALUES ('$ic_no', '$name', '$address','$tel_no','$email')"); mysql_close($con); echo $_SESSION['ic_no']=$ic_no; $_SESSION['name']=$name; $_SESSION['address']=$address; $_SESSION['tel_no']=$tel_no; $_SESSION['email']=$email; echo "<br>Post Values=> "; print_r($_POST); echo "<br>Session Values=> "; print_r($_SESSION); } ?> this way u are using the one form for getting values and the other one for posting values Quote Link to comment https://forums.phpfreaks.com/topic/196853-no-data-inserted-into-database-am-i-wrongdoing-it/#findComment-1033460 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.