ajax调用脚本上的PHP错误

时间:2016-08-29 09:31:20

标签: javascript php mysql ajax database

一切顺利,我想为你们提供帮助

脚本现在它创建一个表但发送空信息 所以我试着这样做: http://mediaads.eu/villageop/back/savepoints.php?user_id=abcdefghijklm

现在我称之为剧本的脚本给我这个错误:

所以我编辑了脚本代码来清理它  所以现在我的代码是:

<?php
header('Access-Control-Allow-Origin: *');
error_reporting(E_ALL);
ini_set('display_errors',1);

$servername = "localhost";
$username = "publiadd_publix";
$password = "1a3g7893fsh";

try {
    $conn = new PDO("mysql:host=$servername;dbname=publiadd_registervillageop", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully"; 
}
catch(PDOException $e){
    echo "Connection failed: " . $e->getMessage();
}


if(isset($_GET['user_id'])){
     //$user_id = intval($_GET['user_id']);
     //Lightly sanitize the GET's to prevent SQL injections and possible XSS attacks

    try {
      $dbh = new PDO("mysql:host=$servername;dbname=publiadd_registervillageop", $username, $password);

      $user_id = @$_GET['user_id'];  
      $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
      $sql = "INSERT INTO users (user_id) VALUES ('".$_POST["user_id"]."')";
      if ($dbh->query($sql)) {
         echo "<script type= 'text/javascript'>alert('New Record Inserted Successfully');</script>";
      }
      else{
         echo "<script type= 'text/javascript'>alert('Data not successfully Inserted.');</script>";
      }    
      $dbh = null;
    }
    catch(PDOException $e){
       echo $e->getMessage();
    }

}
?>

$sql->execute(array($user_Id));


     if($sql){         
          //The query returned true - now do whatever you like here.
          echo 'Your ID was saved. Congrats!';              
     }else{         
          //The query returned false - you might want to put some sort of error reporting here. Even logging the error to a text file is fine.
          echo 'There was a problem saving your points. Please try again later.';              
     }         
}else{
     echo 'Your id wasnt passed in the request.';
}

// close MySQL connection 
$conn = null;
?>
<html>
<head>
</head>
<body>
<body bgcolor="#ffffff">
</body>
</html>

1 个答案:

答案 0 :(得分:0)

您在表单中使用的方法是GET,因此在$_GET条件下使用$_POST而不是if。所以用$_POST替换$_GET,第二个错误是你的表名,users表不是publiadd_registervillageop所以你的sql查询看起来像,

$sql = "INSERT INTO users (user_id) VALUES ('".$_GET["user_id"]."')";