PHP SQL插入数据库


PHP SQL Insert To DB

我已经尝试了许多不同的方法将数据插入到我的数据库中。我更进一步了,它过去只是说错误,但现在当你提交表单时,它会加载一个空白页,但表单中的数据不会添加到表中;/

                <form name="datainsert" method="post" action="dataInsert.php">
                    <label>Server Name: </label>
                    <input type="text" name="name" placeholder="Enter Server Name" style="margin-left:90px; width:160px; padding:5px; margin-top:10px;"><br />
                    <label>Server Location:</label>
                    <input type="text" name="location" placeholder="Enter Server Location" style="margin-left:71px; width:160px; padding:5px; margin-top:10px;"><br />
                    <label>Server Operating System:</label>
                    <input type="text" name="os" placeholder="Enter Server OS" style="margin-left:16px; width:160px; padding:5px; margin-top:10px;"><br/>
                    <input style="margin-top:10px;" name="submit" value="submit" type="submit">
                </form>

<?php
include 'dbconnect.php';
$name = $_POST['name'];
$location = $_POST['location'];
$os = $_POST['os'];
)
mysql_query("INSERT INTO fostvm (name, location, os) VALUES ('$name', '$location', '$os')");
$result=mysql_query($sql);
if($result){
    echo "Data Added Successfully";
} else {
    echo "Error";
}
?>

有人能看到语法错误吗?或者我可能在哪里出错

谢谢!

试试这个。您的代码是Ok的,只需注释这一行($result=mysql_query($sql);)。使用此代码。为什么在代码中尝试了两次mysql_qury()。

<?php
    include 'dbconnect.php';
    $name = $_POST['name'];
    $location = $_POST['location'];
    $os = $_POST['os'];
    )
    $result = mysql_query("INSERT INTO fostvm (name, location, os) VALUES ('$name', '$location', '$os')");
    //$result=mysql_query($sql);
    if($result){
        echo "Data Added Successfully";
    } else {
        echo "Error";
    }
    ?>

你没有$sql variable,你正在使用它mysql_query试试这个,

<?php
    include 'dbconnect.php';
    if(isset($_POST['submit'])){ // check for form submit
        $name = $_POST['name'];
        $location = $_POST['location'];
        $os = $_POST['os'];
        $result=mysql_query("INSERT INTO fostvm (name, location, os) VALUES ('$name', '$location', '$os')");
        if($result){
            echo "Data Added Successfully";
        } else {
            echo "Error";
        }
    }
?>

您还应该使用mysqli,因为mysql是deprecated