简体   繁体   English

无法通过 ajax php 在 sql 中插入值

[英]can't insert values in sql via ajax php

i'm trying to understand this error but i dont how to solve it this is my JS code:我试图理解这个错误,但我不知道如何解决它,这是我的 JS 代码:

$.ajax({
                url: 'C:\\inetpub\\wwwroot\\VisionwareHelp\\Php/CriaUserEempresa.php',
                type: "POST",
                data: ({Pname: Pname, Uname: Uname, email: email, Ename: Ename, Sigla: Sigla}),
                complete:function(data) 
                {
                    resposta = data;
                    console.log(resposta);
                }
            });

this is my php code:这是我的 php 代码:

$serverName = $server;
$uid = $uid;
$pwd = $pass;
$connectionInfo = array( "UID" => $uid, "PWD" => $pwd,"Database"=>"Portal");
//$connectionInfo = array("Database"=>"Portal");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
$Pname = $_POST["Pname"];
$Uname = $_POST["Uname"];
$email = $_POST["email"];
$Ename = $_POST["Ename"];
$Sigla = $_POST["Sigla"];

if( $conn )
{
    $sqlCliente = "INSERT INTO Portal.dbo.Empresa VALUES ($Ename, $Sigla)";
    if (mysqli_query($conn, $sqlCliente)) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sqlCliente . "<br>" . mysqli_error($conn);
    }

}

when i do console.log of data, give me this:当我做数据的 console.log 时,给我这个: 日志

what is wrong with my code?我的代码有什么问题?

尝试改成$sqlCliente = "INSERT INTO Portal.dbo.Empresa VALUES ('$Ename', '$Sigla')";

I think problem with your sql insert statement.我认为您的 sql 插入语句有问题。

$sqlCliente = "INSERT INTO Portal.dbo.Empresa(Ename,Sigla) VALUES ($Ename, $Sigla)";

OR或者

$sqlCliente = "INSERT INTO Portal.dbo.Empresa VALUES ($Ename, $Sigla,..... Add all parameters to here)";

url: 'C:\\\\inetpub\\\\wwwroot\\\\VisionwareHelp\\\\Php/CriaUserEempresa.php',

This line is incorrect.这一行是不正确的。

PHP files need a server to get executed. PHP 文件需要一个服务器才能执行。 By the looks of your path, it looks like you have hosted in IIS.从您的路径来看,您似乎已托管在 IIS 中。 IIS doesn't suppport PHP by default - so assuming you have installed PHP and configured properly, read below.默认情况下,IIS 不支持 PHP - 因此假设您已安装 PHP 并正确配置,请阅读以下内容。

You don't post to the physical path of the PHP file, rather URL of the PHP.您不会发布到 PHP 文件的物理路径,而是 PHP 的 URL。

Therefore, it should look something similar to:因此,它应该类似于:

url: localhost/VisionwareHelp/php/CriaUserEempresa.php',

Until you fix the URL to the correct one, none of your posting code will work correctly.在您将 URL 修正为正确的 URL 之前,您的任何发布代码都无法正常工作。

If you have not installed PHP on IIS, then you need to download and install a PHP-enabled server like ' WampServer ' and host your PHP in that.如果您尚未在 IIS 上安装 PHP,那么您需要下载并安装一个支持 PHP 的服务器,例如“ WampServer ”,并在其中托管您的 PHP。

Also, read below articles - it will help to improve your knowledge.另外,阅读以下文章 - 这将有助于提高您的知识。

In Ajax, The correct syntax of sending multiple data is: http://api.jquery.com/jQuery.ajax/在 Ajax 中,发送多个数据的正确语法是: http : //api.jquery.com/jQuery.ajax/

So, Replace所以,更换

data: ({Pname: Pname, Uname: Uname, email: email, Ename: Ename, Sigla: Sigla}),

with

data: {'Pname':Pname, 'Uname':'Uname', 'email':email, 'Ename':Ename, 'Sigla':Sigla },

And this line url: 'C:\\\\inetpub\\\\wwwroot\\\\VisionwareHelp\\\\Php/CriaUserEempresa.php', is also incorrect.而这一行url: 'C:\\\\inetpub\\\\wwwroot\\\\VisionwareHelp\\\\Php/CriaUserEempresa.php',也是不正确的。

replace代替

url: 'C:\\inetpub\\wwwroot\\VisionwareHelp\\Php/CriaUserEempresa.php',

with

url: 'Php/CriaUserEempresa.php',

Hope it will help you..希望能帮到你。。

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

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