【发布时间】: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