Jump to content

READING INFORMATION OUT OF A HTML FORM AND POST IT TO DATA BASE


kyknos

Recommended Posts

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.

Link to comment
Share on other sites

 

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.
  }

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

 

Link to comment
Share on other sites

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.

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.