使用Php

时间:2017-05-09 04:32:44

标签: php mysql

我正在创建一个允许用户为项目获得资金的Web应用程序,我已经完成了大部分工作,但我被困在一个地方,其中php脚本返回正状态代码,但没有数据插入到Mysql数据库中。以下是我正在使用的PHP脚本:

代码:

<?php
session_start();
if(!isset($_SESSION['username']))
{
    echo "Unauthorised Page Usage Please Relogin to Access All the Page features;";
    header('location:login.html');


}


$sponsor=mysql_real_escape_string($_POST['sponsorid']);
$projectid=mysql_real_escape_string($_POST['projectid']);
$pledge=mysql_real_escape_string($_POST['pledgevalue']);


$servername = "localhost";
$usernam = "root";
$password = "";
$dbname = "project";
$httpStatusCode = 400;
$httpStatusMsg  = 'Incorrect Username or Password';
$protocol=isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0';

$connection=new mysqli($servername,$usernam,$password,$dbname);
if (!$connection) {
    die("Connection failed: " . $connection->connect_error);
} 

$sql1="INSERT INTO `sponsor`(`spon_id`,`project_id`,`spon_amt`,`spon_date_time`) VALUES ('$sponsor','$projectid','$pledge',NOW())";
$result=$connection->query($sql1);
if ($result) {
        $Success=200;
        $httpStatusMsg=mysqli_error($connection);
        header($protocol.' '.$Success.' '.$result);


 } 

    else {
$Success=400;
    $httpStatusMsg=mysqli_error($connection);
    header($protocol.' '.$Success.' '.$httpStatusMsg);
    }
?>

以下是用于将数据发布到页面的ajax:

   var xhttp = new XMLHttpRequest();
         xhttp.onreadystatechange = function() {
        if (this.readyState === 4) {
            if(this.status===404){
                alert(this.responseText);
                                             }
            if(this.status===200)
            {

            alert("Project backed successfully");
            window.location.reload(true);



                    }

            }
  };    
        xhttp.open("POST", "sponsor.php", true);

        xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        var param= "sponsorid"+"="+ <?php echo json_encode($_SESSION['username']); ?>+"&"+"pledgevalue"+"="+document.getElementById("pledge").value+"&"+"projectid"+"="+<?php echo json_encode($projectid);?>;
        console.log(param);
        xhttp.send(param);



        } 

我已经交叉引用了我的赞助商表,以确保每个字段都相同。代码在我朋友的计算机上正常工作。请帮帮我

更新:$ sql1查询给我错误代码:1644我在数据库中运行它时出现问题(使用XAMMP)。

请帮忙。

3 个答案:

答案 0 :(得分:1)

更改

$result=$connection->query($sql);

$result=$connection->query($sql1);

答案 1 :(得分:0)

您已使用$sql1作为查询,当您向数据库发出查询时,您使用的是$sql,但实际上并不可用。

如果启用错误报告,您将看到有关未定义变量$sql的错误 如果仍然没有解决,则使用echo $sql1;打印生成的sql查询并尝试在database.you中执行它将得到确切的错误。

答案 2 :(得分:-1)

试试这个:

<?php
    session_start();
    if(!isset($_SESSION['username']))
    {
        echo "Unauthorised Page Usage Please Relogin to Access All the Page features;";
        header('location:login.html');


    }


    $sponsor=mysql_real_escape_string($_POST['sponsorid']);
    $projectid=mysql_real_escape_string($_POST['projectid']);
    $pledge=mysql_real_escape_string($_POST['pledgevalue']);


    $servername = "localhost";
    $usernam = "root";
    $password = "";
    $dbname = "project";
    $httpStatusCode = 400;
    $httpStatusMsg  = 'Incorrect Username or Password';
    $protocol=isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0';

    $connection=new mysqli($servername,$usernam,$password,$dbname);
    if (!$connection) {
        die("Connection failed: " . $connection->connect_error);
    } 

    $sql1="INSERT INTO `sponsor`(`spon_id`,`project_id`,`spon_amt`,`spon_date_time`) VALUES ('$sponsor','$projectid','$pledge',NOW())";
    $result=$connection->query($sql1);
    if ($result) {
            $Success=200;
            $httpStatusMsg=mysqli_error($connection);
            header($protocol.' '.$Success.' '.$result);


     } 

        else {
    $Success=400;
        $httpStatusMsg=mysqli_error($connection);
        header($protocol.' '.$Success.' '.$httpStatusMsg);
        }
    ?>