0

I used this code before. Now i used the same code for an other script but now i'm getting a notice. I want to put this information in a database but i keep getting undefined index. No matter what i do.

 <pre><code>
    <?php
    define('DB_NAME', 'videonaardvd');
    define('DB_USER', 'root');
    define('DB_PASSWORD', 'usbw');
    define('DB_HOST', 'localhost');

$Type = $_POST['Type'];
$Aantal = $_POST['Aantal'];
$ExtraKopie = $_POST['ExtraKopie'];
$Naam = $_POST['Naam'];
$Adres = $_POST['Adres'];
$Postcode = $_POST['Postcode'];
$Plaats = $_POST['Plaats'];
$Telefoon = $_POST['Telefoon'];
$Email = $_POST['Email'];
$Datum = $_POST['Datum'];
        $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

        if(!$link){
            die('could not connect:' . mysql_error()); 
            }
        $db_selected = mysql_select_db (DB_NAME, $link);

        if (!$db_selected) {
            die('Can\'t use ' . DB_NAME . ': ' . mysql_error ());
            }

        $sql = "INSERT INTO gegevens (Type,Aantal,ExtraKopie,Naam,Adres,Postcode,Plaats,Telefoon,Email,Datum)  
                VALUES ('$Type','$Aantal','$ExtraKopie','$Naam','$Adres','$Postcode','$Plaats','$Telefoon','$Email','$Datum'";

        if(!mysql_query($sql)){
            die('Er is iets fout gegaan. Probeer het later opnieuw.: '. mysql_error());
            }

?>
Bedankt voor het versturen van je opdracht!!!
<br />
<input class="terug" type=button onClick="location.href='index.html'" value='Terug'>

--------------------------- The html ----------------------------------------

<fieldset>
    <div id="type">
        <form><div id="namen"> <h4 class="namen"> Type...........................Aantal...............Extra kopie</h4> </div>
            <input type="text" class="aantal" name="Aantal">    <input type="checkbox" class="extra" name="ExtraKopie">     <input type="checkbox" name="Type" >VH / S-VHS                  <br>
            <input type="text" class="aantal" name="Aantal">    <input type="checkbox" class="extra" name="ExtraKopie">     <input type="checkbox" name="Type">Video2000 / VCC          <br>
            <input type="text" class="aantal" name="Aantal">    <input type="checkbox" class="extra" name="ExtraKopie">     <input type="checkbox" name="Type">Mini DV                   <br>
            <input type="text" class="aantal" name="Aantal">    <input type="checkbox" class="extra" name="ExtraKopie">     <input type="checkbox" name="Type">Video 8, HI8 & Digital 8      <br>
            <input type="text" class="aantal" name="Aantal">    <input type="checkbox" class="extra" name="ExtraKopie">     <input type="checkbox" name="Type">Betamax                  <br>
            <input type="text" class="aantal" name="Aantal">    <input type="checkbox" class="extra" name="ExtraKopie">     <input type="checkbox" name="Type">VHS-C                         <br>
            <input type="text" class="aantal" name="Aantal">    <input type="checkbox" class="extra" name="ExtraKopie">     <input type="checkbox" name="Type">Filmspoelen              <br>
        </form>
    </div>
    <div id="gegevens">
        <form>
            <input type="text" name="Naam" placeholder="De heer/mevrouw"><br>
            <input type="text" name="Adres" placeholder="Adres"><br>
            <input type="text" name="Postcode" placeholder="Postcode">
            <input type="text" name="Plaats" placeholder="Plaats"><br>
            <input type="text" name="Telefoon" placeholder="Telefoon"><br>
            <input type="text" name="Email" placeholder="Email"><br> <br><br>
            <input type="text" name="Datum" placeholder="Datum:"><br>
        </form> 
    </div>
    <input type="submit" href="collect.php" class="versturen">
    </fieldset>

1 Answers1

0

Have not tried this so I will let you give it a go.

$user = 'root';
$pass = 'usbw';
$dbname = 'videonaardvd';
$host = 'localhost';
$opt = array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION );
$dsn = "mysql:host=$host;dbname=$dbname";
if ($_SERVER['REQUEST_METHOD'] == "POST") {
    function died($error){ 
        echo "We are very sorry, but there were error(s) found with the form you submitted. "; 
        echo "These errors appear below.<br /><br />"; 
        echo $error."<br /><br />"; 
        echo "Please go back and fix these errors.<br /><br />"; 
        die();
    }
    if (!$_POST['Type'] || !$_POST['Aantal'] || !$_POST['ExtraKopie'] || !$_POST['Naam'] || !$_POST['Adres'] || !$_POST['Postcode'] || !$_POST['Plaats'] || !$_POST['Telefoon'] || !$_POST['Email'] || !$_POST['Datum']) {
        died('<p>Please remember to supply all of the requested information! You may press your back button to attempt again!</p>');
        exit;
    }
    $Type = $_POST['Type'];
    $Aantal = $_POST['Aantal'];
    $ExtraKopie = $_POST['ExtraKopie'];
    $Naam = $_POST['Naam'];
    $Adres = $_POST['Adres'];
    $Postcode = $_POST['Postcode'];
    $Plaats = $_POST['Plaats'];
    $Telefoon = $_POST['Telefoon'];
    $Email = $_POST['Email'];
    $Datum = $_POST['Datum'];

    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
    if(!preg_match($email_exp,$Email)){ 
        $error_message .= 'The Email Address you entered does not appear to be valid.<br />'; 
    } 
    $string_exp = "/^[A-Za-z .'-]+$/"; 
    if(!preg_match($string_exp,$Naam)){ 
        $error_message .= 'The Name you entered does not appear to be valid.<br />'; 
    }
    if(strlen($error_message) > 0){ 
        died($error_message); 
    }
    else {
        try {        
            $DBH = new PDO($dsn, $user, $pass, $opt);
            $STH = $DBH->prepare("INSERT INTO gegevens (Type,Aantal,ExtraKopie,Naam,Adres,Postcode,Plaats,Telefoon,Email,Datum) VALUES (:Type, :Aantal, :ExtraKopie, :Naam, :Adres, :Postcode, :Plaats, :Telefoon, :Email, :Datum)");
            $STH->bindParam(':Type', $_POST['Type']);
            $STH->bindParam(':Aantal', $_POST['Aantal']);
            $STH->bindParam(':ExtraKopie', $_POST['ExtraKopie']);
            $STH->bindParam(':Naam', $_POST['Naam']);
            $STH->bindParam(':Adres', $_POST['Adres']);
            $STH->bindParam(':Postcode', $_POST['Postcode']);
            $STH->bindParam(':Plaats', $_POST['Plaats']);
            $STH->bindParam(':Telefoon', $_POST['Telefoon']);
            $STH->bindParam(':Email', $_POST['Email']);
            $STH->bindParam(':Datum', $_POST['Datum']);
            $STH->execute();
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
        echo "<p>Submitted!</p>";
    }
}
$DBH = null;
  • Gave it a go. I have now 2 more notice:. Undefined variable: host on line 7. Undefined index: Type on line 16. – tim de hart Aug 22 '15 at 12:58
  • @timdehart For line 7 need to add `$host = 'localhost';` under the `$dbname = 'videonarrdvd'; Corrected this part in the above code. As for line 16 check the form and see if Type is being called in one of your inputs, or show me your html form. – Joshua Nightingale Aug 22 '15 at 13:13
  • Wouldn't know what could be wrong. Everything is typed good. I've put the html under the php script. Maybe you see something – tim de hart Aug 22 '15 at 13:37
  • You have a bunch of checkboxes with the name "Type" you could probably use value="CHECKBOXNAME" and add that instead. The query is seeing to many Type or something like this [http://stackoverflow.com/questions/23292983/how-insert-the-multiple-checkbox-values-into-database-using-php](http://stackoverflow.com/questions/23292983/how-insert-the-multiple-checkbox-values-into-database-using-php) – Joshua Nightingale Aug 22 '15 at 14:15