简体   繁体   中英

HTML mySQL form not working

I am trying to set up a form on my website, but it is giving me an error, I'm sure I have the code right but it is still not working.

I am banging my head trying to find the problem any help would be appreciated. This is the php script:

<?php

$host=""; // Host name 
$username=""; // Mysql username 
$password=""; // Mysql password 
$db_name=""; // Database name 
$tbl_name="orders_mysql"; // Table name 

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// Get values from form 
$name=$_POST["name"];
$lastname=$_POST["lastname"];
$address=$_POST["address"];
$address2=$_POST["address2"];
$city=$_POST["city"];
$state=$_POST["state"];
$zip=$_POST["zip"];
$country=$_POST["country"];
$phone=$_POST["phone"];
$bra_size=$_POST["bra_size"];
$bra_color=$_POST["bra_color"];
$cami_size=$_POST["cami_size"];
$cami_color=$_POST["cami_color"];
$email=$_POST["email"];

// Insert data into mysql 
$sql="INSERT INTO orders_mysql(name, lastname, address, address2, city, state, zip, country, phone, bra_size, bra_color, cami_size, cami_color, email)VALUES('$name', '$lastname', '$address' '$address2', '$city', '$state', '$zip', '$country', '$phone', '$bra_size', '$bra_color', '$cami_size', '$cami_color', '$email')";
$result=mysql_query($sql);

// if successfully insert data into database, displays message "Successful". 
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='index.php'>Back to main page</a>";
}

else {
echo "ERROR";
}
?> 

<?php 
// close connection 
mysql_close();
?>

And this is the HTML:

        <form action="insert_ac.php" id="order" method="post" >
            <center><span class="products_title">Select Quantity, Size and Color</span>
            <img id="product" src="/images/seamlesscami.jpg" alt="Seamless Cami">
                <label for="selectList">Size</label>
                <select id="selectList">
                    <option value="Option 1">Small</option>
                    <option value="Option 2">Medium</option>
                    <option value="Option 3">Large</option>
                </select>
            <ul>
            <li>
            <fieldset>

            <legend id="title1" class="desc">
            Select a Choice
            </legend>

            <div>
            <span>
                <input id="Field1_0" name="Field1" type="radio" class="field radio"  checked="checked">
                <label class="choice" for="Field1_0" style="background:beige; height:40px; width: 50px; float: left;"></label>
            </span>
            <span>
                <input id="Field1_1" name="Field1" type="radio" class="field radio">
                <label class="choice" for="Field1_1" style="background:black; height:40px; width: 50px; float: left;"></label>
            </span>
            </div>
            </fieldset>
            </li>
            </ul>
            </center>
            </div>
        <div class="one-third column form_pos">
            <center><span class="products_title">Select Quantity, Size and Color</span>
            <img id="product" src="/images/seamlesscontrolbrief.jpg" alt="Seamless Control Brief">
                                <label for="selectList">Size</label>
                <select id="selectList">
                    <option value="Option 1">Small</option>
                    <option value="Option 2">Medium</option>
                    <option value="Option 3">Large</option>
                </select>
            <ul>
            <li>
            <fieldset>

            <legend id="title2" class="desc">
            Select a Choice
            </legend>

            <div>
            <span>
            <input id="Field1_3" name="Field2" type="radio" class="field radio"  checked="checked">
            <label class="choice" for="Field1_3" style="background:beige; height:40px; width: 50px; float: left;"></label>
            </span>
            <span>
            <input id="Field1_4" name="Field2" type="radio" class="field radio">
            <label class="choice" for="Field1_4" style="background:black; height:40px; width: 50px; float: left;"></label>
            </span>
            </div>
            </fieldset>
            </li>
            </ul></center>
            </div>
        <div class="fifteen columns">
            <p id="order">*Your free bonus offer will be automatically deducted at checkout with each BraWizard ordered. All you will be charged is 7.99 for additional processing and handling. New Jersey Residents add tax. 60 day gaurentee less S/H.</p>
        </div>
        <center>
        <div class="fourteen columns" style="background: lightgrey; margin: 0 50% 0 7%;">

        <div class="seven columns">
        <center>
        First name:* <input type="text" name="name" required>
        Address 1:* <input type="text" name="address" required>
        City:* <input type="text" name="city" required>
        Zip Code:* <input type="text" name="zip" required>
        Email Address:* <input type="text" name="email" required>
        </center>
        </div>


        <div class="six columns">
        <center>
        Last name:* <input type="text" name="lastname" required>
        Address 2:* <input type="text" name="address2" required>
        State:* <input type="text" name="state" required>
        Country:* <input type="text" name="country" required>
        Phone Number:* <input type="text" name="phone" required>
        </center>
        </div>


        </div>
        </center>
        <div class="sixteen columns">
        <button type="submit" value="submit" name="Submit" style="margin-left:38%;"><img src="/images/order.jpg" alt="order"></button>  
        </form>
        </div>
        </div>

    </div><!-- container -->


<!-- End Document
================================================== -->
</body>
</html>

Ok, so here is the deal, What I can see in your script are the following concerns:

First, try to make the connection as follows:

mysql_connect($host, $username, $password)

(remove the " from the variables).

Second, I see you are inserting a new register to the database, And I could not see any "id" column, is your database table working with an index? Do you have an auto increment value? If yes, You should format your SQL as follows:

"INSERT INTO orders_mysql(id, name, lastname, address, address2, city, state, zip, country, phone, bra_size, bra_color, cami_size, cami_color, email)VALUES(NULL, '$name', '$lastname', '$address' '$address2', '$city', '$state', '$zip', '$country', '$phone', '$bra_size', '$bra_color', '$cami_size', '$cami_color', '$email')";

Other thing that you could try in order to tackle down your problem, would be as follows: Copy your SQL statement and replace the variables with real values, just write randon words, something like:

"INSERT INTO orders_mysql(name, lastname, address, address2, city, state, zip, country, phone, bra_size, bra_color, cami_size, cami_color, email)VALUES('asd', 'asd', 'asd' 'asd', 'asd', 'asd', 'asd', 'asd', 'asd', 'asd', 'asd', asd', 'asd', 'asd')";

having this, using phpMyAdmin, paste the SQL statement in your database SQL Query box and try to add it, this will give you an error or a success, and if it's an error, you will get the description on why your statement is failing.

Other thing that ussually makes statements fail, is that you have to add the "´" to the column names:

"INSERT INTO orders_mysql(´name´, ´lastname´, ´address´, ´address2´, ´city´, ´state´, ´zip´, ´country´, ´phone´, ´bra_size´, ´bra_color´, ´cami_size´, ´cami_color´, ´email´)VALUES('$name', '$lastname', '$address' '$address2', '$city', '$state', '$zip', '$country', '$phone', '$bra_size', '$bra_color', '$cami_size', '$cami_color', '$email')";

Lets try any of these options and keep us updated with the errors you get in order to try to solve this with some more info.

Remember that the API provided by mysql extension will be deprecated and functions like mysql_connect (or any mysql_*) will be no longer supported. Please take a look at these links:

Mysql - Choosing an API - http://us2.php.net/manual/en/mysqlinfo.api.choosing.php

PHP: The Right Way - Databases - http://www.phptherightway.com/#databases

Work with PDO is very simple. To connect with a MySql database we can do something like that:

<?php
$hostname = 'localhost';
$username = 'username';
$password = 'password';

try {
    $db_host = new PDO("mysql:host=$hostname;dbname=mysql", $username, $password);
    echo 'Connected!';
}
catch(PDOException $e) {
    echo $e->getMessage();
}

Now that we are already connected, maybe some INSERTs:

<?php
try {
    $cons = $db_host->exec("INSERT INTO orders_mysql(name,
                            lastname) VALUES('foo', 'bar', 'baz', 'bar2')");

    echo $cons; // number of affected rows
}
catch(PDOException $e) {
    echo $e->getMessage();
}

Even better we can prepare our SQL to before its execution:

<?
$name     = 'foo';
$lastname = 'bar';
// [...]
$email    = 'qux';

// constructing 
$query = $db_host->prepare("SELECT * FROM orders_mysql 
                            WHERE name = :name 
                            AND lastname = :lastname");
// binding params
$query->bindParam(':name', $name, PDO::PARAM_STR, 50);
$query->bindParam(':lastname', $lastname, PDO::PARAM_STR, 50);
// executing
$query->execute();
// fetching results
$results = $query->fetchAll();

// displaying
foreach($results as $tuple) {
    echo "Name: {$tuple['name']} - Lastname: {$tuple['name']}";
}

Bye!

=)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM