0

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>
4

2 に答える 2

0

mysql 拡張機能によって提供される API は廃止され、mysql_connect (または任意の mysql_*) などの関数はサポートされなくなることに注意してください。次のリンクをご覧ください。

Mysql - API の選択 - http://us2.php.net/manual/en/mysqlinfo.api.choosing.php

PHP: 正しい方法 - データベース - http://www.phptherightway.com/#databases

PDO の操作は非常に簡単です。MySql データベースに接続するには、次のようにします。

<?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();
}

すでに接続されているので、おそらくいくつかの INSERT を実行します。

<?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();
}

さらに良いことに、実行前に SQL を準備できます。

<?
$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']}";
}

さよなら!

=)

于 2013-11-14T19:09:42.450 に答える