kyknos Posted February 11, 2008 Share Posted February 11, 2008 Hello, I have programmed a form script in HTML and now I want to save the information written in the form on the HTML page as a variable in PHP - that is the first problem that doesn't seem to work. In a second step I want to use the variables to post the information to a data base with MySQL. I tried that with "insert into...." - and that doesn't seem to work either - maybe it's just because there is something wrong with the PHP variables - but I don't know - I'm still learning PHP and MySQL - so I can't be sure... Would be great if anyone of you can help me with that or send me a script which I can use as an example. Greetings, Kyknos. Quote Link to comment https://forums.phpfreaks.com/topic/90500-reading-information-out-of-a-html-form-and-post-it-to-data-base/ Share on other sites More sharing options...
peranha Posted February 11, 2008 Share Posted February 11, 2008 <?php // set database server access variables: $host = "localhost"; $user = "user"; $pass = "pass"; $db = "dbname"; ?> <?php include ("includes.php"); include ("pagelog.php"); ?> <?php if (!isset($_POST['submit'])) { // form not submitted ?> <form action="name.php" method="post"> First Name: <input type="text" name="fname"><BR> Last Name: <input type="text" name="lname"><BR> Party: <input type="text" name="party"><BR> address: <input type="text" name="address"><BR> city: <input type="text" name="city"><BR> state: <input type="text" name="state"><BR> zip: <input type="text" name="zip"><BR> misc: <input type="text" name="misc"><BR> <input type="submit" name="submit"><BR> </form> <?php } else { // form submitted // open connection $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); // get form input // check to make sure it's all there // escape input values for greater safety $fname = mysql_escape_string($_POST['fname']); $lname = mysql_escape_string($_POST['lname']); $party = mysql_escape_string($_POST['party']); $address = mysql_escape_string($_POST['address']); $city = mysql_escape_string($_POST['city']); $state = mysql_escape_string($_POST['state']); $zip = mysql_escape_string($_POST['zip']); $misc = mysql_escape_string($_POST['misc']); // select database mysql_select_db($db) or die ("Unable to select database!"); // create query $query = "INSERT INTO user (firstname, lastname, numparty, address, city, state, zip, misc) VALUES ('$fname', '$lname', '$party', '$address', '$city', '$state', '$zip', '$misc')"; // execute query $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); // close connection mysql_close($connection); } ?> The above is an example of what I believe you want. Quote Link to comment https://forums.phpfreaks.com/topic/90500-reading-information-out-of-a-html-form-and-post-it-to-data-base/#findComment-464007 Share on other sites More sharing options...
kyknos Posted February 11, 2008 Author Share Posted February 11, 2008 Well, thank you - I managed to get a little bit forward - there is still some problem, but I think I can manage now... Quote Link to comment https://forums.phpfreaks.com/topic/90500-reading-information-out-of-a-html-form-and-post-it-to-data-base/#findComment-464037 Share on other sites More sharing options...
kyknos Posted February 12, 2008 Author Share Posted February 12, 2008 Okay, I tried to remodel my script according to your example, but somehow it doesn't really work - the variables don't seem to save the information from the html form. I get a new line in the data base but it isn't filled with information. Maybe someone has a tip how I can read information out of a HTML form and save it to a PHP variable? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/90500-reading-information-out-of-a-html-form-and-post-it-to-data-base/#findComment-464973 Share on other sites More sharing options...
trq Posted February 12, 2008 Share Posted February 12, 2008 Maybe someone has a tip how I can read information out of a HTML form and save it to a PHP variable? <?php if (isset($_POST['submit'])) { // <-- the name of your submit button $var = $_POST['var']; // <-- the name of the form element that has been posted. } ?> Quote Link to comment https://forums.phpfreaks.com/topic/90500-reading-information-out-of-a-html-form-and-post-it-to-data-base/#findComment-464977 Share on other sites More sharing options...
haku Posted February 12, 2008 Share Posted February 12, 2008 I would suggest starting here: http://www.w3schools.com/php/default.asp and moving onwards. You are asking basic php questions, for which there are hundreds of tutorials on the internet. Asking on this forum will get your short term answers, but the tutorials at the link I posted will help you a lot more in the long run. After you start figuring some stuff out and are having troubles making it work, we are always here to help! Good luck. Quote Link to comment https://forums.phpfreaks.com/topic/90500-reading-information-out-of-a-html-form-and-post-it-to-data-base/#findComment-464980 Share on other sites More sharing options...
kyknos Posted February 12, 2008 Author Share Posted February 12, 2008 Well, I looked for a sample script but it didn't help me... ...here is the script and I cant figure out why it isn't working: <html> <?php include("dbconnect.php"); if (!isset($_POST['submit'])) { ?> <h1>Please fill out the form</h1> <form method="post" action="index.php"> <table> <tr bgcolor="#cccccc"> <td>First Name</td> <td><input type="text" name="fname" /></td> </tr> <tr bgcolor="#cccccc"> <td>Last Name</td> <td><input type="text" name="lname" /></td> </tr> <tr bgcolor="#cccccc"> <td>User Name</td> <td><input type="text" name="uname" /></td> </tr> <tr bgcolor="#cccccc"> <td>Password</td> <td><input type="text" name="pass" /></td> </tr> <tr bgcolor="#dddddd"> <td colspan="2" align="center"> <input type="submit" value="Login" /> </td> </tr> </table> </form> <?php } else { $fname = mysql_escape_string($_POST['fname']); $lname = mysql_escape_string($_POST['lname']); $uname = mysql_escape_string($_POST['uname']); $pass = mysql_escape_string($_POST['pass']); $sql = "insert into kunden (firstname, lastname, username, password) values ('$fname', '$lname', '$uname', '$pass')"; mysql_query($sql) or die(mysql_error()); } ?> </html> Quote Link to comment https://forums.phpfreaks.com/topic/90500-reading-information-out-of-a-html-form-and-post-it-to-data-base/#findComment-465056 Share on other sites More sharing options...
haku Posted February 12, 2008 Share Posted February 12, 2008 I didn't point you at a sample script, I pointed you at php lessons. I.E. you need to learn php. Quote Link to comment https://forums.phpfreaks.com/topic/90500-reading-information-out-of-a-html-form-and-post-it-to-data-base/#findComment-465059 Share on other sites More sharing options...
kyknos Posted February 12, 2008 Author Share Posted February 12, 2008 Well, I just try to do that... ...and I think that I already know a lot - I just can't figure out, why this script doesn't write the information in the table... Quote Link to comment https://forums.phpfreaks.com/topic/90500-reading-information-out-of-a-html-form-and-post-it-to-data-base/#findComment-465158 Share on other sites More sharing options...
haku Posted February 12, 2008 Share Posted February 12, 2008 change: <input type="submit" value="Login" /> to <input type="submit" name="submit" value="Login" /> Quote Link to comment https://forums.phpfreaks.com/topic/90500-reading-information-out-of-a-html-form-and-post-it-to-data-base/#findComment-465164 Share on other sites More sharing options...
kyknos Posted February 13, 2008 Author Share Posted February 13, 2008 Thanks again for all your help. Finally I found the mistake. After experimenting further with the script I found out, that the action="index.php" in the form tag somehow caused the script not to write the information in the data base. Now, after removing it, it seems to work. I don't really know why, but anyway, it's working now. Greetings, Kyknos. Quote Link to comment https://forums.phpfreaks.com/topic/90500-reading-information-out-of-a-html-form-and-post-it-to-data-base/#findComment-465826 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.