user1257518
user1257518

Reputation: 147

PHP form doesn't send data to MySQL

This is the shortest of two forms that wont send data to the database, but I have one that does work. When I press submit the form refreshes, clears the page and doesn't send the data to my table. I've included my HTML incase that is a problem.

include 'dbc.php';

$err = array();

if(@$_POST['doPersonal'] == 'Personal') 
{

This code filters harmful script code and escapes data of all POST data from the user submitted form.

foreach($_POST as $key => $value) {
    $data[$key] = filter($value);
}

Automatically collects the hostname or domain like example.com)

$host  = $_SERVER['HTTP_HOST'];
$host_upper = strtoupper($host);
$path   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');

if(empty($err)) {

SQL Insert

$sql_insert = "INSERT into `personal`
    (`sex`,`aux_citizen`,`birth_place`,`birth_country`,`children`)
    VALUES
    ('$data[sex]','$data[aux_citizen]','$data[birth_place]','$data[birth_country]',
    '$data[children]')";

mysql_query($sql_insert,$link) or die("Insertion Failed:" . mysql_error());

header("Location: thankyou.php");  
exit();
} 
}       
?>

HTML code for the form:

<html>
<head>
    <title>title</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script language="JavaScript" type="text/javascript"
            src="file:///js/jquery.validate.js"></script>
    <tr>
    </tr>
    <script>
        $("#regForm").validate();
        })
        ;
    </script>
    <link href="styles.css" rel="stylesheet" type="text/css">
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="5" class="main">
    <tr>
        <td colspan="3">&nbsp;</td>
    </tr>
    <tr>
        <td width="160" valign="top"><p>&nbsp;</p>

            <p>&nbsp; </p>

            <p>&nbsp;</p>

            <p>&nbsp;</p>

            <p>&nbsp;</p></td>
        <td width="732" valign="top"><p>
            <?php
            if (!empty($err)) {
                echo "<div class=\"msg\">";
                foreach ($err as $e) {
                    echo "* $e <br>";
                }
                echo "</div>";
            }
            ?>
            <br>

            <form action="personal.php" method="post" name="regForm" id="regForm">
                <table width="95%" border="0" cellpadding="3" cellspacing="3" class="forms">
                    <tr>
                        <td>Sex <font color="#CC0000">*</font></span></td>
                        <td><select name="sex" class="required" id="sex">
                            <option value="" selected></option>
                            <option value="M">Male</option>
                            <option value="F">Female</option>
                        </td>
                    </tr>
                    </select>

I wont put the rest of the HTML code as it will go on forever, but this is the submit function

<input name="doPersonal" type="submit" id="doPersonal" value="Submit">

Can anyone see the problem? It has worked once but now it won't Thanks in advance

Upvotes: 1

Views: 760

Answers (1)

Michael Berkowski
Michael Berkowski

Reputation: 270599

It looks like the name attribute and value attribute of your submit button are inconsistent with your check in the processor form:

// Submit's value should be 'Personal'
if(@$_POST['doPersonal'] == 'Personal') 


// But your value on the button is 'Submit'
<input name="doPersonal" type="submit" id="doPersonal" value="Submit">

// You can change this to:
if($_POST['doPersonal'] == 'Submit') 
// But see the improved version below...

Instead of using the @ to suppress errors, check for the presence of the array key before attempting to use it:

if (!empty($_POST['doPersonal']) && $_POST['doPersonal'] == 'Submit') {
   // Handle your form
}

Upvotes: 1

Related Questions