简体   繁体   English

PHP SQL插入数据库

[英]PHP SQL Insert To DB

I have tried a number of different ways of inserting data into my DB I have got a little further it used to just say error but now when you submit the form it loads a blank page, the data from the form isn't added to the table however ;/ 我尝试了多种将数据插入数据库的方法,但我只是想说些错误而已,但现在,当您提交表单时,它会加载一个空白页,该表单中的数据不会添加到表; /

                <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";
}

?>

Can anyone see a syntax error or where I might be going wrong 谁能看到语法错误或我可能要去哪里哪里

thanks! 谢谢!

You have no $sql variable and you are using it mysql_query try this, 您没有$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";
        }
    }
?>

Also you should use mysqli as mysql is deprecated 你也应该使用mysqli的作为MySQL的deprecated

Try this. 尝试这个。 your code is Ok just comment this line ($result=mysql_query($sql);). 您的代码还可以,只需注释以下行($ result = mysql_query($ sql);)。 use this code. 使用此代码。 why you try mysql_qury() two times in your code. 为什么在代码中两次尝试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";
    }

    ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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