0

For some reason, I'm having an issue getting the following code to work. I can't get it to post to my db. I'm sure I'm missing something simple but I'm an amateur, so what else would you expect.

Here is the code:

<?php
    $connect = mysql_connect('localhost', 'bestseni_dev', 'masterkey');
    if (!$connect) {
        die('Could not connect: ' . mysql_error());
    }
    mysql_select_db('bestseni_ssw', $connect);


    if (isset($_POST['send'])) {
        $errors = array();
        if (!strlen(@$_POST['name'])) {
            $errors['name'] = 'This field is required';
        }
        if (!strlen(@$_POST['BusinessName'])) {
            $errors['BusinessName'] = 'This field is required';

        }
        if (!strlen(@$_POST['ContactPref'])) {
            $errors['ContactPref'] = 'This field is required';


       }
        if (!strlen(@$_POST['phone'])) {
            $errors['phone'] = 'This field is required';
        }
        if (!strlen(@$_POST['email'])) {
            $errors['email'] = 'This field is required';
        } else if (eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", @$_POST['email'])) {
            $errors['email'] = 'Email is not valid';

        }
        if (!count($errors)) {
            $fields = array('name', 'BusinessName', 'ContactPref', 'phone', 'email');

            $sqlValuesArr = array();
            $sql = 'INSERT INTO form (' . implode(', ', $fields) . ') VALUES ';
            foreach ($fields as $field) {
                $sqlValuesArr[] = "'" . (@$_POST[$field]) . "'";
            }
            $sql .= '(' . implode(', ', $sqlValuesArr) . ')';

            mysql_query($sql, $connect);
            mysql_close($connect);


        }

    }

    mysql_close($connect);
?>

<form action="" method="post">
    <div class="form-top"></div>
    <input type="hidden" name="send" value="1" />
    <div class="form">
    <div class="text">


    </div>
    <fieldset>
        <div class="field1">
            <div class="label"><label for="name">Name </label></div>
            <div><input name="first_name" id="first_name" value="" type="text" /></div>
        </div>
        <br/>
        <div class="field1">
            <div class="label"><label for="email">Email </label></div>
            <div><input name="email" id="email" value="" type="text" /></div>

        </div><br/>
        <div class="field1">
            <div class="label"><label for="phone">Phone </label></div>
            <div><input name="phone" id="phone" value="" type="text" /></div>

        </div><br/>
        <div class="field1">
            <div class="label"><label for="BusinessName">Business Name </label></div>
        <div><input name="BusinessName" id="BusinessName" value="" type="text" /></div>

        </div><br/><br/>
        <div class="row">
            <div class="label"><label for="ContactPref">Preferred Method of Contact, Phone or Email?</label></div>
            <div><input name="ContactPref" id="ContactPref" value="" type="text" /></div>

        </div><br/>

        <div class="wrapper"><a href="http://www.Training4search.com/index-2.html" class="link1" >Submit</a><input type="submit" value="" /></div>
        </fieldset>
    </form>

Edited by Reverend Jim: Fixed formatting

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by HemantPHP
0

For some reason, I'm having an issue getting the following code to work. I can't get it to post to my db. I'm sure I'm missing something simple but I'm an amateur, so what else would you expect.

Here is the code:

<?php
    $connect = mysql_connect('localhost', 'bestseni_dev', 'masterkey');
    if (!$connect) {
        die('Could not connect: ' . mysql_error());
    }
    mysql_select_db('bestseni_ssw', $connect);


    if (isset($_POST['send'])) {
        $errors = array();
        if (!strlen(@$_POST['name'])) {
            $errors['name'] = 'This field is required';
        }
        if (!strlen(@$_POST['BusinessName'])) {
            $errors['BusinessName'] = 'This field is required';

        }
        if (!strlen(@$_POST['ContactPref'])) {
            $errors['ContactPref'] = 'This field is required';


       }
        if (!strlen(@$_POST['phone'])) {
            $errors['phone'] = 'This field is required';
        }
        if (!strlen(@$_POST['email'])) {
            $errors['email'] = 'This field is required';
        } else if (eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", @$_POST['email'])) {
            $errors['email'] = 'Email is not valid';

        }
        if (!count($errors)) {
            $fields = array('name', 'BusinessName', 'ContactPref', 'phone', 'email');

            $sqlValuesArr = array();
            $sql = 'INSERT INTO form (' . implode(', ', $fields) . ') VALUES ';
            foreach ($fields as $field) {
                $sqlValuesArr[] = "'" . (@$_POST[$field]) . "'";
            }
            $sql .= '(' . implode(', ', $sqlValuesArr) . ')';

            mysql_query($sql, $connect);
            mysql_close($connect);


        }

    }

    mysql_close($connect);
?>

<form action="" method="post">
    <div class="form-top"></div>
    <input type="hidden" name="send" value="1" />
    <div class="form">
    <div class="text">


    </div>
    <fieldset>
        <div class="field1">
            <div class="label"><label for="name">Name </label></div>
            <div><input name="first_name" id="first_name" value="" type="text" /></div>
        </div>
        <br/>
        <div class="field1">
            <div class="label"><label for="email">Email </label></div>
            <div><input name="email" id="email" value="" type="text" /></div>

        </div><br/>
        <div class="field1">
            <div class="label"><label for="phone">Phone </label></div>
            <div><input name="phone" id="phone" value="" type="text" /></div>

        </div><br/>
        <div class="field1">
            <div class="label"><label for="BusinessName">Business Name </label></div>
        <div><input name="BusinessName" id="BusinessName" value="" type="text" /></div>

        </div><br/><br/>
        <div class="row">
            <div class="label"><label for="ContactPref">Preferred Method of Contact, Phone or Email?</label></div>
            <div><input name="ContactPref" id="ContactPref" value="" type="text" /></div>

        </div><br/>

        <div class="wrapper"><a href="http://www.Training4search.com/index-2.html" class="link1" >Submit</a><input type="submit" value="" /></div>
        </fieldset>
    </form>

end quote.

Hello,

Can you please print that insert query using echo before that execution and use die(). And please send to back that printed insert query with value.. that will be more unstable for your code error. :-)

Edited by mike_2000_17: Fixed formatting

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.