Jump to content

Recommended Posts

Hello everyone,

 

I'm trying to learn how to use a form to put the data into phpMyAdmin. I found a site here:
http://www.html-form-guide.com/php-form/php-form-processing.html

 

that shows you how to do it. I use Xampp. Both Apache and SQL are running. I use Aptana to edit it and use Chrome for my browser. The downloaded file is in my root htdocs folder. I set up my database like they said. Heres a picture:
phpmyadmin.png
 

And when I run myform2, I see this:
phperror.png

 

"Notice: Undefined index: formSubmit in C:\xampp\htdocs\myform2.php on line 2" 

I didn't even mess with the code yet. 

Here is my code for my myform2.php:

<?php
if($_POST['formSubmit'] == "Submit") 
    {
$errorMessage = "";


if(empty($_POST['formMovie'])) 
        {
$errorMessage .= "<li>You forgot to enter a movie!</li>";
}
if(empty($_POST['formName'])) 
        {
$errorMessage .= "<li>You forgot to enter a name!</li>";
}
if(empty($_POST['formGender'])) 
        {
$errorMessage .= "<li>You forgot to select your Gender!</li>";
}


        $varMovie = $_POST['formMovie'];
$varName = $_POST['formName'];
$varGender = $_POST['formGender'];


if(empty($errorMessage)) 
        {
$db = mysql_connect("servername","username","password");
if(!$db) die("Error connecting to MySQL database.");
mysql_select_db("databasename" ,$db);


$sql = "INSERT INTO movieformdata (moviename, yourname, Gender) VALUES (".
PrepSQL($varMovie) . ", " .
PrepSQL($varName) . ", " .
PrepSQL($varGender) . ")";
mysql_query($sql);


header("Location: thankyou.html");
exit();
}
}
            
    // function: PrepSQL()
    // use stripslashes and mysql_real_escape_string PHP functions
    // to sanitize a string for use in an SQL query
    //
    // also puts single quotes around the string
    //
    function PrepSQL($value)
    {
        // Stripslashes
        if(get_magic_quotes_gpc()) 
        {
            $value = stripslashes($value);
        }


        // Quote
        $value = "'" . mysql_real_escape_string($value) . "'";


        return($value);
    }
?>






<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>PHP Form processing example</title>
<!-- define some style elements-->
<style>
label,a 
{
font-family : Arial, Helvetica, sans-serif;
font-size : 12px; 
}


</style> 
</head>


<body>


       <?php
   if(!empty($errorMessage)) 
   {
   echo("<p>There was an error with your form:</p>\n");
   echo("<ul>" . $errorMessage . "</ul>\n");
            }
        ?>


<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<p>
<label for='formMovie'>Which is your favorite movie?</label><br/>
<input type="text" name="formMovie" maxlength="50" value="<?=$varMovie;?>" />
</p>
<p>
<label for='formName'>What is your name?</label><br/>
<input type="text" name="formName" maxlength="50" value="<?=$varName;?>" />
</p>
<p>
<label for='formGender'>What is your Gender?</label><br/>
<select name="formGender">
<option value="">Select...</option>
<option value="M"<? if($varGender=="M") echo(" selected=\"selected\"");?>>Male</option>
<option value="F"<? if($varGender=="F") echo(" selected=\"selected\"");?>>Female</option>
</select>
</p>
<input type="submit" name="formSubmit" value="Submit" />
</form>


<p>
<a href='http://www.html-form-guide.com/php-form/php-form-processing.html'
>'PHP form processing' article page</a>
</p>


</body>
</html>

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/301647-putting-data-into-database-with-php/
Share on other sites

I imagine the error is displayed when you first visit the form...before submitting it? Is that correct?

 

Basically, the $_POST variable is only populated after the form is submitted. To determine if a form (set to POST) is submitted, you can use the following:

if($_SERVER['REQUEST_METHOD'] == 'POST') {
    $errorMessage = "";
   
    //...
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.