【问题标题】:Php error on ajax call scriptajax调用脚本上的php错误
【发布时间】:2016-08-29 09:31:20
【问题描述】:

首先,我想为大家提供帮助

脚本现在它创建一个表但发送空信息 所以我尝试这样做: 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>

【问题讨论】:

  • WTF 是否在您的问题中添加了ruby-on-rails 标签?
  • 请编辑您的标签和问题以提高可读性
  • 搜索数组键 user_id 并打印以检查 var 是否真的包含任何值。然后尝试插入表格
  • 为什么要设置两次数据库连接?在第一次 try/catch 块之后,您可以在 php 文件中的任何位置全局使用它。此外,您正在使用 PDO,因此您无需关闭连接。

标签: javascript php mysql ajax database


【解决方案1】:

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

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

【讨论】:

  • Tanks for The Help 脚本现在创建一个表格但发送空数据
  • 哪个错误?您是否将$_POST 更改为$_GET,并确保您的呼叫网址中有?user_id=1
  • 是的,我已经更改了帖子以获取并将插入更改为
  • 坦克帮我解决这个问题
  • 我已经清理了一些我不明白我需要删除哪个代码?
猜你喜欢
  • 2011-08-11
  • 2018-01-24
  • 1970-01-01
  • 2013-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-22
相关资源
最近更新 更多