$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Databases - Submitting data from inputs to database|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Databases - Submitting data from inputs to database

matteradmin8PV0评论
Closed. This question is off-topic. It is not currently accepting answers.

Questions that are too localized (such as syntax errors, code with restricted access, hacked sites, hosting or support issues) are not in scope. See how do I ask a good question?

Closed 6 years ago.

Improve this question

I am attempting to submit data to a table I have created within my database.

For sanity sake I have called it 'wp_email_subscribers'. Including the wp_ prefix.

I know there is a way to insert data using the $wpdb but I would ideally be able to do this in a way that will allow me to copy this form over to other sites that are not WordPress powered. So was hoping to use a more traditional connect and insert / query to the DB.

At this point I cannot get the data to submit using either the $wpdb or a more classic method. Can anyone see where this is going wrong as I do not receive any error messages other than a boolean false value for $result. All other fields look like they should.

Enough rambling - here is the code:

<style>
#sign-up-form {
    width: 100%;
    max-width: 640px;
    margin: 20px auto;
    padding: 10px;
    border-radius: 15px;
    background-color: rgba(7,30,68,1);
    text-align: center
}
    @media screen and (max-width: 767px) {
        #sign-up-form {
            width: calc(100% - 40px);
            margin: 20px;
        }
    }

    #sign-up-form span,
    #sign-up-form input {
        width: 100%;
    }

    #sign-up-form input {
        margin-top: 10px;
        font-size: 1rem;
    }

    #sign-up-form .form-title {
        font-size: 1.25rem;
    }

    #sign-up-form .form-title,
    #sign-up-form label {
        color: #ffc90f;
    }

    #sign-up-form input[name=first-name],
    #sign-up-form input[name=last-name]{
        width: calc(50% - 5px);
        float: left;
    }
    #sign-up-form input[name=first-name] {
        margin-right: 10px;
    }
        @media screen and (max-width: 767px) {
            #sign-up-form input[name=first-name],
            #sign-up-form input[name=last-name]{
                width: 100%;
            }
            #sign-up-form input[name=first-name] {
                margin-right: 0;
            }
        }

    #sign-up-form label[for=gdpr-consent]{
        font-size: 0.6rem;
    }

    #sign-up-form input[type=checkbox] {
        width: 20px;
    }

    #sign-up-form input[type=submit] {
        width: 100%;
        max-width: 210px;
        padding: 5px;
        margin: 20px auto 0;
        position: relative;

        border-radius: 15px;
        border: none;

        -webkit-transition: all .7s;
        transition: all .7s;

        background-color: #007dbe;
        color: white;
        font-size: 1.875rem;

        cursor: pointer;
        outline: 0;
    }
        #sign-up-form input[type=submit]:hover {
            background-color: #00295b;
        }

    .error {
        color: red;
    }

    .success-log {
        margin-top: 20px;
        color: #4BB543;
        font-size: 16px;
    }
</style>

<?php
    // show errors
    error_reporting(E_ALL);
    ini_set('display_errors', 1);

    $servername = "****";
    $username = "****";
    $password = "****";
    $dbname = "****";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 


    // define variables and set to empty values
    $firstNameErr = $lastNameErr = $emailErr = $gdprConsentErr = "";
    $firstname = $lastname = $email = $gdprconsent = "";

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        if (empty($_POST["first-name"])) {
            $firstNameErr = "First name is required";
        } else {
            $firstname = test_input($_POST["first-name"]);

            // check if name only contains letters and whitespace
            if (!preg_match("/^[a-zA-Z ]*$/",$firstname)) {
                $firstNameErr = "Only letters and white space allowed"; 
            }
        }

        if (empty($_POST["last-name"])) {
            $lastNameErr = "Last name is required";
        } else {
            $lastname = test_input($_POST["last-name"]);

            // check if name only contains letters and whitespace
            if (!preg_match("/^[a-zA-Z ]*$/",$lastname)) {
                $lastNameErr = "Only letters and white space allowed"; 
            }
        }

        if (empty($_POST["email"])) {
            $emailErr = "Email is required";
        } else {
            $email = test_input($_POST["email"]);

            // check if e-mail address is well-formed
            if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { 
                $emailErr = "Invalid email format"; 
            }
        }

        if (empty($_POST["gdpr-consent"])) {
            $gdprconsent = "You must give consent";
        } else {
            $gdprconsent = test_input($_POST["gdpr-consent"]);
        }
    }

    function test_input($data) {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }

    if (!empty($firstname) && !empty($lastname) && !empty($email) && !empty($gdprconsent)) {
        $sql = "INSERT INTO 'wp_email_subscribers' ('first_name', 'last_name', 'email', 'gdpr_consent') VALUES ($firstname, $lastname, $email, $gdprconsent)";
        $result = $conn->query($sql);
        $conn->query($sql);

        var_dump($firstname);
        var_dump($lastname);
        var_dump($email);
        var_dump($gdprconsent);
        var_dump($sql);
        var_dump($result);
    }
?>

<div class="form-title">Sign up to our newsletter!</div>

<?php if (!isset($_POST["submit"])): ?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER['REQUEST_URI']);?>" class="email-sub-form">
        <input type="text" name="first-name" value="<?php echo $firstname;?>" placeholder="First Name *" required>
        <input type="text" name="last-name" value="<?php echo $lastname;?>" placeholder="Last Name *" required>
        <input type="email" name="email" value="<?php echo $email;?>" placeholder="Email *" required>
        <input type="checkbox" name="gdpr-consent" id="gdpr-consent" value="1" required><label for="gdpr-consent">I consent to $$$$ using my data for marketing purposes.</label>

        <div class="error-log">
            <span class="error"><?php if (!empty($firstNameErr)) { echo $firstNameErr; }?></span>
            <span class="error"><?php if (!empty($lastNameErr)) { echo $lastNameErr; }?></span>
            <span class="error"><?php if (!empty($emailErr)) { echo $emailErr; }?></span>
            <span class="error"><?php if (!empty($gdprConsentErr)) { echo $gdprConsentErr; }?></span>
        </div>

        <input type="submit" name="submit" value="Submit">
    </form>
<?php endif; ?>

<?php if (isset($_POST["submit"])): ?>
<div class="success-log">
    <div class="success-inner">
        Thank you  for subscribing to our e-news.<br/>
        You have been added into our mail list.
    </div>
</div>
<?php endif; ?>

This is me trying to use the $wpdb:

<style>
#sign-up-form {
    width: 100%;
    max-width: 640px;
    margin: 20px auto;
    padding: 10px;
    border-radius: 15px;
    background-color: rgba(7,30,68,1);
    text-align: center
}
    @media screen and (max-width: 767px) {
        #sign-up-form {
            width: calc(100% - 40px);
            margin: 20px;
        }
    }

    #sign-up-form span,
    #sign-up-form input {
        width: 100%;
    }

    #sign-up-form input {
        margin-top: 10px;
        font-size: 1rem;
    }

    #sign-up-form .form-title {
        font-size: 1.25rem;
    }

    #sign-up-form .form-title,
    #sign-up-form label {
        color: #ffc90f;
    }

    #sign-up-form input[name=first-name],
    #sign-up-form input[name=last-name]{
        width: calc(50% - 5px);
        float: left;
    }
    #sign-up-form input[name=first-name] {
        margin-right: 10px;
    }
        @media screen and (max-width: 767px) {
            #sign-up-form input[name=first-name],
            #sign-up-form input[name=last-name]{
                width: 100%;
            }
            #sign-up-form input[name=first-name] {
                margin-right: 0;
            }
        }

    #sign-up-form label[for=gdpr-consent]{
        font-size: 0.6rem;
    }

    #sign-up-form input[type=checkbox] {
        width: 20px;
    }

    #sign-up-form input[type=submit] {
        width: 100%;
        max-width: 210px;
        padding: 5px;
        margin: 20px auto 0;
        position: relative;

        border-radius: 15px;
        border: none;

        -webkit-transition: all .7s;
        transition: all .7s;

        background-color: #007dbe;
        color: white;
        font-size: 1.875rem;

        cursor: pointer;
        outline: 0;
    }
        #sign-up-form input[type=submit]:hover {
            background-color: #00295b;
        }

    .error {
        color: red;
    }

    .success-log {
        margin-top: 20px;
        color: #4BB543;
        font-size: 16px;
    }
</style>

<?php
    // show errors
    error_reporting(E_ALL);
    ini_set('display_errors', 1);

    global $wpdb;

    /*
    $servername = "****";
    $username = "****";
    $password = "****";
    $dbname = "****";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 
    */


    // define variables and set to empty values
    $firstNameErr = $lastNameErr = $emailErr = $gdprConsentErr = "";
    $firstname = $lastname = $email = $gdprconsent = "";

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        if (empty($_POST["first-name"])) {
            $firstNameErr = "First name is required";
        } else {
            $firstname = test_input($_POST["first-name"]);

            // check if name only contains letters and whitespace
            if (!preg_match("/^[a-zA-Z ]*$/",$firstname)) {
                $firstNameErr = "Only letters and white space allowed"; 
            }
        }

        if (empty($_POST["last-name"])) {
            $lastNameErr = "Last name is required";
        } else {
            $lastname = test_input($_POST["last-name"]);

            // check if name only contains letters and whitespace
            if (!preg_match("/^[a-zA-Z ]*$/",$lastname)) {
                $lastNameErr = "Only letters and white space allowed"; 
            }
        }

        if (empty($_POST["email"])) {
            $emailErr = "Email is required";
        } else {
            $email = test_input($_POST["email"]);

            // check if e-mail address is well-formed
            if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { 
                $emailErr = "Invalid email format"; 
            }
        }

        if (empty($_POST["gdpr-consent"])) {
            $gdprconsent = "You must give consent";
        } else {
            $gdprconsent = test_input($_POST["gdpr-consent"]);
        }
    }

    function test_input($data) {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }

    $table = 'wp_email_subscribers';
    $data = array(
        'first_name' => $firstname, 
        'last_name' => $lastname, 
        'email'=> $email , 
        'gdpr_consent'=>$gdprconsent 
    );
    $format = array('%s','%s', '%s', '%s');
    $wpdb->insert($table,$data,$format);
    var_dump($wpdb->insert_id);

    /*if (!empty($firstname) && !empty($lastname) && !empty($email) && !empty($gdprconsent)) {
        $sql = "INSERT INTO `wp_email_subscribers` (`first_name`, `last_name`, `email`, `gdpr_consent`) VALUES ($firstname, $lastname, $email, $gdprconsent)";
        $result = $conn->query($sql);
        $conn->query($sql);

        var_dump($firstname);
        var_dump($lastname);
        var_dump($email);
        var_dump($gdprconsent);
        var_dump($sql);
        var_dump($result);
    }*/
?>

<div class="form-title">Sign up to our newsletter!</div>

<?php if (!isset($_POST["submit"])): ?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER['REQUEST_URI']);?>" class="email-sub-form">
        <input type="text" name="first-name" value="<?php echo $firstname;?>" placeholder="First Name *" required>
        <input type="text" name="last-name" value="<?php echo $lastname;?>" placeholder="Last Name *" required>
        <input type="email" name="email" value="<?php echo $email;?>" placeholder="Email *" required>
        <input type="checkbox" name="gdpr-consent" id="gdpr-consent" value="1" required><label for="gdpr-consent">I consent to $$$$ using my data for marketing purposes.</label>

        <div class="error-log">
            <span class="error"><?php if (!empty($firstNameErr)) { echo $firstNameErr; }?></span>
            <span class="error"><?php if (!empty($lastNameErr)) { echo $lastNameErr; }?></span>
            <span class="error"><?php if (!empty($emailErr)) { echo $emailErr; }?></span>
            <span class="error"><?php if (!empty($gdprConsentErr)) { echo $gdprConsentErr; }?></span>
        </div>

        <input type="submit" name="submit" value="Submit">
    </form>
<?php endif; ?>

<?php if (isset($_POST["submit"])): ?>
<div class="success-log">
    <div class="success-inner">
        Thank you <?php echo $firstname . ' ' . $lastname; ?> for subscribing to our e-news.<br/>
        You have been added into our mail list.
    </div>
</div>
<?php endif; ?>
Closed. This question is off-topic. It is not currently accepting answers.

Questions that are too localized (such as syntax errors, code with restricted access, hacked sites, hosting or support issues) are not in scope. See how do I ask a good question?

Closed 6 years ago.

Improve this question

I am attempting to submit data to a table I have created within my database.

For sanity sake I have called it 'wp_email_subscribers'. Including the wp_ prefix.

I know there is a way to insert data using the $wpdb but I would ideally be able to do this in a way that will allow me to copy this form over to other sites that are not WordPress powered. So was hoping to use a more traditional connect and insert / query to the DB.

At this point I cannot get the data to submit using either the $wpdb or a more classic method. Can anyone see where this is going wrong as I do not receive any error messages other than a boolean false value for $result. All other fields look like they should.

Enough rambling - here is the code:

<style>
#sign-up-form {
    width: 100%;
    max-width: 640px;
    margin: 20px auto;
    padding: 10px;
    border-radius: 15px;
    background-color: rgba(7,30,68,1);
    text-align: center
}
    @media screen and (max-width: 767px) {
        #sign-up-form {
            width: calc(100% - 40px);
            margin: 20px;
        }
    }

    #sign-up-form span,
    #sign-up-form input {
        width: 100%;
    }

    #sign-up-form input {
        margin-top: 10px;
        font-size: 1rem;
    }

    #sign-up-form .form-title {
        font-size: 1.25rem;
    }

    #sign-up-form .form-title,
    #sign-up-form label {
        color: #ffc90f;
    }

    #sign-up-form input[name=first-name],
    #sign-up-form input[name=last-name]{
        width: calc(50% - 5px);
        float: left;
    }
    #sign-up-form input[name=first-name] {
        margin-right: 10px;
    }
        @media screen and (max-width: 767px) {
            #sign-up-form input[name=first-name],
            #sign-up-form input[name=last-name]{
                width: 100%;
            }
            #sign-up-form input[name=first-name] {
                margin-right: 0;
            }
        }

    #sign-up-form label[for=gdpr-consent]{
        font-size: 0.6rem;
    }

    #sign-up-form input[type=checkbox] {
        width: 20px;
    }

    #sign-up-form input[type=submit] {
        width: 100%;
        max-width: 210px;
        padding: 5px;
        margin: 20px auto 0;
        position: relative;

        border-radius: 15px;
        border: none;

        -webkit-transition: all .7s;
        transition: all .7s;

        background-color: #007dbe;
        color: white;
        font-size: 1.875rem;

        cursor: pointer;
        outline: 0;
    }
        #sign-up-form input[type=submit]:hover {
            background-color: #00295b;
        }

    .error {
        color: red;
    }

    .success-log {
        margin-top: 20px;
        color: #4BB543;
        font-size: 16px;
    }
</style>

<?php
    // show errors
    error_reporting(E_ALL);
    ini_set('display_errors', 1);

    $servername = "****";
    $username = "****";
    $password = "****";
    $dbname = "****";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 


    // define variables and set to empty values
    $firstNameErr = $lastNameErr = $emailErr = $gdprConsentErr = "";
    $firstname = $lastname = $email = $gdprconsent = "";

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        if (empty($_POST["first-name"])) {
            $firstNameErr = "First name is required";
        } else {
            $firstname = test_input($_POST["first-name"]);

            // check if name only contains letters and whitespace
            if (!preg_match("/^[a-zA-Z ]*$/",$firstname)) {
                $firstNameErr = "Only letters and white space allowed"; 
            }
        }

        if (empty($_POST["last-name"])) {
            $lastNameErr = "Last name is required";
        } else {
            $lastname = test_input($_POST["last-name"]);

            // check if name only contains letters and whitespace
            if (!preg_match("/^[a-zA-Z ]*$/",$lastname)) {
                $lastNameErr = "Only letters and white space allowed"; 
            }
        }

        if (empty($_POST["email"])) {
            $emailErr = "Email is required";
        } else {
            $email = test_input($_POST["email"]);

            // check if e-mail address is well-formed
            if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { 
                $emailErr = "Invalid email format"; 
            }
        }

        if (empty($_POST["gdpr-consent"])) {
            $gdprconsent = "You must give consent";
        } else {
            $gdprconsent = test_input($_POST["gdpr-consent"]);
        }
    }

    function test_input($data) {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }

    if (!empty($firstname) && !empty($lastname) && !empty($email) && !empty($gdprconsent)) {
        $sql = "INSERT INTO 'wp_email_subscribers' ('first_name', 'last_name', 'email', 'gdpr_consent') VALUES ($firstname, $lastname, $email, $gdprconsent)";
        $result = $conn->query($sql);
        $conn->query($sql);

        var_dump($firstname);
        var_dump($lastname);
        var_dump($email);
        var_dump($gdprconsent);
        var_dump($sql);
        var_dump($result);
    }
?>

<div class="form-title">Sign up to our newsletter!</div>

<?php if (!isset($_POST["submit"])): ?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER['REQUEST_URI']);?>" class="email-sub-form">
        <input type="text" name="first-name" value="<?php echo $firstname;?>" placeholder="First Name *" required>
        <input type="text" name="last-name" value="<?php echo $lastname;?>" placeholder="Last Name *" required>
        <input type="email" name="email" value="<?php echo $email;?>" placeholder="Email *" required>
        <input type="checkbox" name="gdpr-consent" id="gdpr-consent" value="1" required><label for="gdpr-consent">I consent to $$$$ using my data for marketing purposes.</label>

        <div class="error-log">
            <span class="error"><?php if (!empty($firstNameErr)) { echo $firstNameErr; }?></span>
            <span class="error"><?php if (!empty($lastNameErr)) { echo $lastNameErr; }?></span>
            <span class="error"><?php if (!empty($emailErr)) { echo $emailErr; }?></span>
            <span class="error"><?php if (!empty($gdprConsentErr)) { echo $gdprConsentErr; }?></span>
        </div>

        <input type="submit" name="submit" value="Submit">
    </form>
<?php endif; ?>

<?php if (isset($_POST["submit"])): ?>
<div class="success-log">
    <div class="success-inner">
        Thank you  for subscribing to our e-news.<br/>
        You have been added into our mail list.
    </div>
</div>
<?php endif; ?>

This is me trying to use the $wpdb:

<style>
#sign-up-form {
    width: 100%;
    max-width: 640px;
    margin: 20px auto;
    padding: 10px;
    border-radius: 15px;
    background-color: rgba(7,30,68,1);
    text-align: center
}
    @media screen and (max-width: 767px) {
        #sign-up-form {
            width: calc(100% - 40px);
            margin: 20px;
        }
    }

    #sign-up-form span,
    #sign-up-form input {
        width: 100%;
    }

    #sign-up-form input {
        margin-top: 10px;
        font-size: 1rem;
    }

    #sign-up-form .form-title {
        font-size: 1.25rem;
    }

    #sign-up-form .form-title,
    #sign-up-form label {
        color: #ffc90f;
    }

    #sign-up-form input[name=first-name],
    #sign-up-form input[name=last-name]{
        width: calc(50% - 5px);
        float: left;
    }
    #sign-up-form input[name=first-name] {
        margin-right: 10px;
    }
        @media screen and (max-width: 767px) {
            #sign-up-form input[name=first-name],
            #sign-up-form input[name=last-name]{
                width: 100%;
            }
            #sign-up-form input[name=first-name] {
                margin-right: 0;
            }
        }

    #sign-up-form label[for=gdpr-consent]{
        font-size: 0.6rem;
    }

    #sign-up-form input[type=checkbox] {
        width: 20px;
    }

    #sign-up-form input[type=submit] {
        width: 100%;
        max-width: 210px;
        padding: 5px;
        margin: 20px auto 0;
        position: relative;

        border-radius: 15px;
        border: none;

        -webkit-transition: all .7s;
        transition: all .7s;

        background-color: #007dbe;
        color: white;
        font-size: 1.875rem;

        cursor: pointer;
        outline: 0;
    }
        #sign-up-form input[type=submit]:hover {
            background-color: #00295b;
        }

    .error {
        color: red;
    }

    .success-log {
        margin-top: 20px;
        color: #4BB543;
        font-size: 16px;
    }
</style>

<?php
    // show errors
    error_reporting(E_ALL);
    ini_set('display_errors', 1);

    global $wpdb;

    /*
    $servername = "****";
    $username = "****";
    $password = "****";
    $dbname = "****";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 
    */


    // define variables and set to empty values
    $firstNameErr = $lastNameErr = $emailErr = $gdprConsentErr = "";
    $firstname = $lastname = $email = $gdprconsent = "";

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        if (empty($_POST["first-name"])) {
            $firstNameErr = "First name is required";
        } else {
            $firstname = test_input($_POST["first-name"]);

            // check if name only contains letters and whitespace
            if (!preg_match("/^[a-zA-Z ]*$/",$firstname)) {
                $firstNameErr = "Only letters and white space allowed"; 
            }
        }

        if (empty($_POST["last-name"])) {
            $lastNameErr = "Last name is required";
        } else {
            $lastname = test_input($_POST["last-name"]);

            // check if name only contains letters and whitespace
            if (!preg_match("/^[a-zA-Z ]*$/",$lastname)) {
                $lastNameErr = "Only letters and white space allowed"; 
            }
        }

        if (empty($_POST["email"])) {
            $emailErr = "Email is required";
        } else {
            $email = test_input($_POST["email"]);

            // check if e-mail address is well-formed
            if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { 
                $emailErr = "Invalid email format"; 
            }
        }

        if (empty($_POST["gdpr-consent"])) {
            $gdprconsent = "You must give consent";
        } else {
            $gdprconsent = test_input($_POST["gdpr-consent"]);
        }
    }

    function test_input($data) {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }

    $table = 'wp_email_subscribers';
    $data = array(
        'first_name' => $firstname, 
        'last_name' => $lastname, 
        'email'=> $email , 
        'gdpr_consent'=>$gdprconsent 
    );
    $format = array('%s','%s', '%s', '%s');
    $wpdb->insert($table,$data,$format);
    var_dump($wpdb->insert_id);

    /*if (!empty($firstname) && !empty($lastname) && !empty($email) && !empty($gdprconsent)) {
        $sql = "INSERT INTO `wp_email_subscribers` (`first_name`, `last_name`, `email`, `gdpr_consent`) VALUES ($firstname, $lastname, $email, $gdprconsent)";
        $result = $conn->query($sql);
        $conn->query($sql);

        var_dump($firstname);
        var_dump($lastname);
        var_dump($email);
        var_dump($gdprconsent);
        var_dump($sql);
        var_dump($result);
    }*/
?>

<div class="form-title">Sign up to our newsletter!</div>

<?php if (!isset($_POST["submit"])): ?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER['REQUEST_URI']);?>" class="email-sub-form">
        <input type="text" name="first-name" value="<?php echo $firstname;?>" placeholder="First Name *" required>
        <input type="text" name="last-name" value="<?php echo $lastname;?>" placeholder="Last Name *" required>
        <input type="email" name="email" value="<?php echo $email;?>" placeholder="Email *" required>
        <input type="checkbox" name="gdpr-consent" id="gdpr-consent" value="1" required><label for="gdpr-consent">I consent to $$$$ using my data for marketing purposes.</label>

        <div class="error-log">
            <span class="error"><?php if (!empty($firstNameErr)) { echo $firstNameErr; }?></span>
            <span class="error"><?php if (!empty($lastNameErr)) { echo $lastNameErr; }?></span>
            <span class="error"><?php if (!empty($emailErr)) { echo $emailErr; }?></span>
            <span class="error"><?php if (!empty($gdprConsentErr)) { echo $gdprConsentErr; }?></span>
        </div>

        <input type="submit" name="submit" value="Submit">
    </form>
<?php endif; ?>

<?php if (isset($_POST["submit"])): ?>
<div class="success-log">
    <div class="success-inner">
        Thank you <?php echo $firstname . ' ' . $lastname; ?> for subscribing to our e-news.<br/>
        You have been added into our mail list.
    </div>
</div>
<?php endif; ?>
Share Improve this question edited Jan 28, 2019 at 16:42 Jason Is My Name asked Jan 28, 2019 at 16:21 Jason Is My NameJason Is My Name 3782 gold badges7 silver badges21 bronze badges 7
  • Is that a WordPress site, or just plain PHP? – Krzysiek Dróżdż Commented Jan 28, 2019 at 16:28
  • @KrzysiekDróżdż - Yes a WordPress website. c: – Jason Is My Name Commented Jan 28, 2019 at 16:32
  • OK, so why are you creating custom mysqli connection? Should this data be stored in WP database? – Krzysiek Dróżdż Commented Jan 28, 2019 at 16:33
  • Yes please. But I would like to be able to copy this to a website that is not using WordPress too (obviously would require different DB log in and DB names). – Jason Is My Name Commented Jan 28, 2019 at 16:35
  • Sorry, but it's not possible. In WP you should use WP api to do this properly. You shouldn't create custom DB connections, and so on... – Krzysiek Dróżdż Commented Jan 28, 2019 at 16:36
 |  Show 2 more comments

1 Answer 1

Reset to default -1

I had a spelling error in my database that I for the life of me couldn't see...

I popped this handy line in to help me diagnose:

$wpdb->show_errors();

Thank you for your patience!

Post a comment

comment list (0)

  1. No comments so far