-1

I don't know if it can be, I need to know how save a email into another db only when in the form the email input have data(the email)..let me explain me better.

I have a form for save users in my site, the form have:

name lastname address birthday email others...

and I have another db which is for newsletter, is apart and I need save only email's for sending offers.. so I need save in my newsletter db the email only if in the form exist email if the user don't insert email the newsletter can't save nothing...

here is my form:

<?php
    include 'include/configs.php';
    $action = isset( $_POST['action'] ) ? $_POST['action'] : "";
    if($action == "create")
    {
        try{
            $stmt = $conn->prepare('INSERT INTO USERS (nombre,apellido,de_casada,estado_civil,genero,referido,f_nacimiento,direccion,departamento,ciudad,IDPAIS,telefono,celular,email) 
            VALUES (:nombre,:apellido,:de_casada,:estado_civil,:genero,:referido,:f_nacimiento,:direccion,:departamento,:ciudad,:IDPAIS,:telefono,:celular,:email)');
                            $stmt->bindParam(':nombre', $_POST['nombre']);
                            $stmt->bindParam(':apellido', $_POST['apellido']);
                            $stmt->bindParam(':de_casada', $_POST['de_casada']);
                            $stmt->bindParam(':estado_civil', $_POST['estado_civil']);
                            $stmt->bindParam(':genero', $_POST['genero']);
                            $stmt->bindParam(':referido', $_POST['referido']);
                            $stmt->bindParam(':f_nacimiento', $_POST['f_nacimiento']);
                            $stmt->bindParam(':direccion', $_POST['direccion']);
                            $stmt->bindParam(':departamento', $_POST['departamento']);
                            $stmt->bindParam(':ciudad', $_POST['ciudad']);
                            $stmt->bindParam(':IDPAIS', $_POST['IDPAIS']);
                            $stmt->bindParam(':telefono', $_POST['telefono']);
                            $stmt->bindParam(':celular', $_POST['celular']);
                            $stmt->bindParam(':email', $_POST['email']);
                            $stmt->execute();

    echo 'Registro ingresado correctamente.';                               
            }
            catch (PDOException $e) 
            {
                error_log($e->getMessage());
                die($e->getMessage());
            }

$stmt = $conn->prepare('INSERT INTO NEWS (email) 
VALUES (:email)');
$stmt->bindParam(
        $stmt->bindParam(':email', $_POST['email']);
        $stmt->execute();

            $dbh = null;   
            echo "<script>";
            echo "window.location.href= 'index.php'";
            echo "</script>";                                   
    }                                   
?>
<form class='form-horizontal' method='post' action='' enctype='multipart/form-data'>
    <fieldset>
        <legend><i class='icon32 icon-square-plus'></i> <?php $translate->__('Add new User to Database'); ?></legend>
        <div class='control-group'>
            <label class='control-label' for='typeahead'><?php $translate->__("User's name"); ?></label>
            <div class='controls'>
                <input type='text' class='span6 typeahead' name='nombre' placeholder='' value='' required/>
            </div>
        </div>
        <div class='control-group'>
            <label class="control-label" for='typeahead'><?php $translate->__('Last Name'); ?></label>
            <div class='controls'>
                <input type='text' class='span6 typeahead' name='apellido' placeholder='' value='' required/>
            </div>
        </div>
        <div class='control-group'>
            <label class='control-label' for='typeahead'><?php $translate->__('Married Name'); ?></label>
            <div class='controls'>
                <input type='text' class='span6 typeahead' name='de_casada' placeholder='' value='' />
            </div>
        </div>
        <div class='control-group'>
            <label class='control-label' for='typeahead'><?php $translate->__('Marital Status'); ?></label>
            <div class='controls'>
                <select id='' name='estado_civil' required>
                    <option value=''><?php $translate->__('Select'); ?></option>
                    <option value='<?php $translate->__('Single'); ?>'><?php $translate->__('Single'); ?></option>
                    <option value='<?php $translate->__('Married'); ?>'><?php $translate->__('Married'); ?></option>
                    <option value='<?php $translate->__('Divorced'); ?>'><?php $translate->__('Divorced'); ?></option>
                    <option value='<?php $translate->__('Widower'); ?>'><?php $translate->__('Widower'); ?></option>
                    <option value='<?php $translate->__('Living Together'); ?>'><?php $translate->__('Living Together'); ?></option>
                </select>
            </div>
        </div>
        <div class='control-group'>
            <label class='control-label' for='typeahead'><?php $translate->__('Gender'); ?></label>
            <div class='controls'>
                <select id='' name='genero' required>
                    <option value=''><?php $translate->__('Select'); ?></option>
                    <option value='<?php $translate->__('Male'); ?>'><?php $translate->__('Male'); ?></option>
                    <option value='<?php $translate->__('Female'); ?>'><?php $translate->__('Female'); ?></option>
                </select>
            </div>
        </div>
        <div class='control-group'>
            <label class='control-label' for='typeahead'><?php $translate->__('Referred by'); ?></label>
            <div class='controls'>
                <input type='text' class='span6 typeahead' name='referido'  placeholder='' />      
            </div>
        </div>
        <div class='control-group'>
            <label class='control-label' for='date01'><?php $translate->__('Date of Birth'); ?></label>
            <div class='controls'>
                <input type='date' class='span6' name='f_nacimiento' placeholder='YYYY-MM-DD' />
            </div>
        </div>
        <div class='control-group'>
            <label class='control-label' for='typeahead'><?php $translate->__('Address'); ?></label>
            <div class='controls'>
                <textarea class='span6 autogrow' name='direccion'></textarea>
            </div>
        </div>
        <div class='control-group'>
            <label class='control-label' for='typeahead'><?php $translate->__('Country'); ?></label>
            <div class="controls">
                <select name="IDPAIS" id="select1" required>    
                    <?php
                        $sql = $conn->prepare("SELECT * FROM lista_paises");
                        $sql->execute();
                        while($row = $sql->fetch(PDO::FETCH_ASSOC)) {
                        echo 
                        '<option value='".$row['id']."'>".$row['opcion']."</option>';
                        }
                    ?> 
                </select>
            </div>
        </div>
        <div class='control-group'>
            <label class='control-label' for='typeahead'><?php $translate->__('State'); ?></label>
            <div class="controls">
                <select name="departamento" id="select2" required></select>
            </div>
        </div>
        <div class='control-group'>
             <label class='control-label' for='typeahead'><?php $translate->__('City'); ?></label>
            <div class='controls'>
                <select name="ciudad" id="select3" required></select>
            </div>
        </div>
        <div class='control-group'>
            <label class='control-label' for='typeahead'><?php $translate->__('Telephone'); ?></label>
            <div class='controls'>
                <input type='text' class='span6 typeahead' name='telefono' placeholder='' value='' />
            </div>
        </div>
        <div class='control-group'>
            <label class='control-label' for='typeahead'><?php $translate->__('Cellphone'); ?></label>
            <div class='controls'>
                <input type='text' class='span6 typeahead' name='celular' placeholder='' value='' />
            </div>
        </div>
        <div class='control-group'>
            <label class='control-label' for='typeahead'><?php $translate->__('E-mail'); ?></label>
            <div class='controls'>
                <input type='email' class='span6 typeahead' name='email' placeholder='' value='' />
            </div>
        </div>
        <div class='form-actions'>
            <input type='hidden' name='action' value='create'  />
            <input type='submit' class='btn btn-primary' value='<?php $translate->__('Save User'); ?>' />
            <input type='reset' class='btn' value='<?php $translate->__('Reset Form'); ?>' />
        </div>
    </fieldset>
</form>

I think I need in the part of email a hidden input or a checkbox for that part(save to another db)...but I don't know how to do it..

sorry for my bad english...is not my mother language

Andrés
  • 85
  • 2
  • 14
  • Can you just check if `$_POST['email']` has a valid value and, if it does, perform your second database insert? – David Sep 12 '13 at 18:51
  • remove the extra `$stmt->bindParam` in your code and quote your key names like: `$row['id']` – Black Sheep Sep 12 '13 at 18:56
  • I already do it @aldanux, thank you – Andrés Sep 12 '13 at 19:03
  • @Andrés - This answer your question: [how-do-you-connect-to-multiple-mysql-databases-on-a-single-webpage](http://stackoverflow.com/questions/274892/how-do-you-connect-to-multiple-mysql-databases-on-a-single-webpage) – Black Sheep Sep 12 '13 at 19:16
  • thank you @aldanux, I can connect right now to both db, but I have a error in validate email... `Fatal error: Call to undefined function ValidateEmail() in /home/xxxxx/public_html/xxxxx/user_create.php on line 45` the line 45 is this : `if($_POST['email']!="" && ValidateEmail($_POST['email'])) {` – Andrés Sep 12 '13 at 20:11
  • @Andrés - First where is your function ValidateEmail(); ?? It can not call the function – Black Sheep Sep 12 '13 at 21:13
  • Hi @aldanux sorry for my late respond..I was searching for a nice validate mail and I found this one: mailcheck.js in this url: [mailcheck](https://github.com/Kicksend/mailcheck) . is working in my site but I don't understand how implement with the ValidateEmail()... – Andrés Sep 13 '13 at 16:18
  • @Andrés - I post an answer (solution) below – Black Sheep Sep 13 '13 at 16:37

1 Answers1

-1

why dont you just ask before doing the second update? im not sure why whould you need a checkbox. And you dont have to say sorry for your bad english, just your bad code haha...

if($_POST['email']!="" && ValidateEmail($_POST['email'])) {
$stmt = $conn->prepare('INSERT INTO NEWS (email) 

other insert code here... 


}
Luis Tellez
  • 2,785
  • 1
  • 20
  • 28
  • thank you for your code and humor..hehe so the code need to be like this: `prepare('INSERT INTO NEWS (email) VALUES (:email)'); $stmt = $conn->prepare('INSERT INTO USERS........ }}` – Andrés Sep 12 '13 at 19:10
  • I say it because you should not have the SQL, html , PHP on the same file, some layers separation makes your code more maintainable. – Luis Tellez Sep 12 '13 at 19:12