MySQL INSERT INTO无法正常工作

时间:2013-02-24 04:01:39

标签: php html mysql insert

正如主题所示,由于某种原因,我无法插入MySQL数据库。我几乎无处不在,我无法找到问题的答案。

我在第二个代码示例的底部进行了此检查,但返回失败。

的index.php

<form name="form1" method="post" action="insert.php">
    <label name="firstname">Fornavn:</label></br>
    <input name="firstname" type="text" id="name">

    <label name="lastname">Efternavn:</label></br>
    <input name="lastname" type="text" id="lastname">

    <label name="address">Adresse:</label></br>
    <input name="address" type="text" id="lastname">

    <label name="zipcode">Postnr.:</label></br>
    <input name="zipcode" type="text" id="lastname">

    <label name="city">By:</label></br>
    <input name="city" type="text" id="lastname">

    <label name="phone">Telefon:</label></br>
    <input name="phone" type="text" id="lastname">

    <label name="mobile">Mobil:</label></br>
    <input name="mobile" type="text" id="lastname">

    <label name="email">Email:</label></br>
    <input name="email" type="text" id="lastname">

    <input type="submit" name="Submit" value="Submit">
</form>

insert.php

<?php
    $host="localhost";
    $username="root";
    $password="";
    $db_name="test8";
    $tbl_name="test_mysql8";

    mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB");

    $firstname = $_POST['firstname'];
    $lastname = $_POST['lastname'];
    $address = $_POST['address'];
    $zipcode = $_POST['zipcode'];
    $city = $_POST['city'];
    $phone = $_POST['phone'];
    $mobile = $_POST['mobile'];
    $email = $_POST['email'];

    $sql = "INSERT INTO $tbl_name
        (
            id,
            firstname,
            lastname,
            address,
            zipcode,
            city,
            phone,
            mobile,
            email
        )
        VALUES
        (
            '$firstname',
            '$lastname',
            '$address',
            '$zipcode',
            '$city',
            '$phone',
            '$mobile',
            '$email'
        )";

    $result = mysql_query($sql);

    if($result){
        echo "Successful";
        echo "<BR>";
        echo "<a href='insert.php'>Back to main page</a>";
    }
    else
    {
        echo "Unsuccessful";
        echo "<BR>";
        echo "<a href='insert.php'>Back to main page</a>";
    }

?>

0 个答案:

没有答案