0

我正在尝试在我的网站上设置一个表单,但它给了我一个错误,我确定我的代码是正确的,但它仍然无法正常工作。

我正在努力寻找问题,任何帮助将不胜感激。这是 php 脚本:

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

这是 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

1

好的,这就是交易,我可以在您的脚本中看到以下问题:

首先,尝试如下进行连接:

mysql_connect($host, $username, $password)

(从变量中删除“)。

其次,我看到您正在向数据库插入一个新寄存器,而我看不到任何“id”列,您的数据库表是否使用索引?你有自动增量值吗?如果是,您应该按如下方式格式化您的 SQL:

"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')";

您可以尝试解决问题的其他方法如下:复制您的 SQL 语句并将变量替换为实际值,只需编写随机词,例如:

"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')";

有了这个,使用 phpMyAdmin,将 SQL 语句粘贴到你的数据库 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')";

让我们尝试这些选项中的任何一个,并让我们及时了解您遇到的错误,以便尝试使用更多信息来解决这个问题。

于 2013-11-14T18:17:06.597 回答
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 回答