PHP doesn't insert in SQL Table even if there aren't error

时间:2016-02-12 22:05:58

标签: php jquery mysql phpmyadmin sql-insert

I am working with the ip information of who visits an html page. I don't want to use GeoIp etc: I'm going to use ipinfo.io service. At the end of html page I did a get function, and inside it I wrote the ajax post.

func bar<T>(_: T.Type) -> [T] {
    return [T]()
}

bar(Int.self)
// Result: [], with type Array<Int>

I was inspired by this: http://jsfiddle.net/zk5fn/2/

In the write.php I've written some fwrite method that wrote all the data taken. And it works. Now, I wanted to post these data in a database. I've create one with phpmyadmin with 000webhost.

I display no error in the post, but where I open phpmyadmin the table is empty... why? This is the word.php:

$.get("http://ipinfo.io", function (response) {
var ip = response.ip;
var hostname = response.hostname;
var city = response.city;
var region = response.region;
var country = response.country;
var loc = response.loc;
var org = response.org;
var postal = response.postal;
var details = JSON.stringify(response, null, 4); 

  $.ajax({
    type: "POST",
    url: 'write.php', 
    data: '&ip=' + ip + '&hostname=' + hostname +'&city=' + city +  '&region=' + region +  '&country=' + country +  '&loc=' + loc +  '&org=' + org +  '&postal=' + postal +  '&details=' + details,
    success: function (data) {   
      alert("Sent");
    },
    error: function(jqXHR, text, error){ 
       alert("Error: not sent.");       
      }
  }); 

}, "jsonp");

UPDATE This is the log of the post: enter image description here

UPDATE 2 This is the SQL seen when I click for export the database:

<?php 

 $link =  mysql_connect("localhost", "name......", "psw....", "database-name");   

if (!$link) {
    alert('Could not connect: ' . mysql_error());
}

echo 'Connected successfully';

// Parse input
$ip      = $_POST['ip'];
$hostname    = $_POST['hostname'];
$city    = $_POST['city'];
$region  = $_POST['region'];
$country  = $_POST['country'];
$loc  = $_POST['loc'];
$org  = $_POST['org'];
$postal  = $_POST['postal'];
$details  = $_POST['details'];

$sql="insert into `sessions` (ip, hostname, city, region,country, loc, org, postal) values('$ip','$hostname', '$city', '$region', '$country', '$loc', '$org' ,'$postal')";  

    $res = mysql_query($sql); 



if($res){

    echo "Records added successfully.";

}   

mysql_close($link);
?> 

5 个答案:

答案 0 :(得分:1)

I would change Byte: 11011100 Mask: 00001000 ---------------- AND: 00001000 to $res = mysql_query($sql); to learn more about possible errors on your insert query. Also you should apply mysql_real_escape_string to any data you're inserting into your database.

答案 1 :(得分:0)

如果您调试PHP代码,$ _POST中有什么?试试这个:

$received = print_r($_POST, true);
$file = fopen('log.txt', 'w+');
fwrite($file, $received);
fclose($file);

在此之后,验证PHP接收的内容,如果需要,粘贴到我们这可以帮助您。

我不尝试,但我认为错误发生在data: '&ip=' + ip ...。由于这是第一个密钥,&是不必要的。尝试调试,因为我解释了,看看它是通过ajax发送给PHP的。

答案 2 :(得分:0)

我建议使用MySQLi Prepared Statement的以下PHP:

<?php 
$link =  mysqli_connect("localhost", "name......", "psw....", "database-name");   
$error = false;

if (mysqli_connect_errno()) {
    $error = mysqli_connection_error();
    echo "Connection Error: $error"; 
    exit();
} else {
    if($stmt = mysqli_prepare($link, "INSERT INTO sessions (ip, hostname, city, region, country, loc, org, postal) VALUES (?, ?, ?, ?, ?, ?, ?, ?)")){
        mysqli_stmt_bind_param(
            $stmt,
            "ssssssss",
            $_POST['ip'],
            $_POST['hostname'],
            $_POST['city'],
            $_POST['region'],
            $_POST['country'],
            $_POST['loc'],
            $_POST['org'],
            $_POST['postal'],
            $_POST['details']
        );
        mysqli_stmt_execute($stmt);
        echo "Records added successfully.";
        mysqli_stmt_close($stmt);
    }
}
mysqli_close($link);
?>

在PHP 5.5.0中不推荐使用MySQL扩展,它已在PHP 7.0.0中删除。

然后在success中,您可以执行以下操作:

success: function (data) {   
  if(data.indexOf("Error")){
    console.error(data);
  } else {
    console.log(data);
  }
}

答案 3 :(得分:0)

试试这个JS代码:

$.get("http://ipinfo.io", function (response) {
  var ip = response.ip;
  var hostname = response.hostname;
  var city = response.city;
  var region = response.region;
  var country = response.country;
  var loc = response.loc;
  var org = response.org;
  var postal = response.postal;
  var details = JSON.stringify(response, null, 4); 

  $.ajax({
    type: "POST",
    url: 'write.php', 
    data: 'ip=' + ip + '&hostname=' + hostname +'&city=' + city +  '&region=' + region +  '&country=' + country +  '&loc=' + loc +  '&org=' + org +  '&postal=' + postal +  '&details=' + details,
    success: function (data) {   
      console.log(data)
    },
    error: function(jqXHR, text, error){ 
        console.log(text);
    }
  }); 

}, "jsonp");

这个PHP代码:

$link =  mysql_connect("localhost", "name......", "psw....", "database-name");   

if (!$link) {
    die('Could not connect: ' . mysql_error());
}

print("This is what was received: ");
print("\r\n");
print_r($_POST);
print("\r\n");

// Parse input
$ip = $_POST['ip'];
$hostname = $_POST['hostname'];
$city = $_POST['city'];
$region = $_POST['region'];
$country = $_POST['country'];
$loc = $_POST['loc'];
$org = $_POST['org'];
$postal = $_POST['postal'];
$details = $_POST['details'];

$sql = "insert into `sessions` (ip, hostname, city, region,country, loc, org, postal) values('$ip','$hostname', '$city', '$region', '$country', '$loc', '$org' ,'$postal')";

print("This is what is sending to database: \"$sql\"");
print("\r\n");

$res = mysql_query($sql) or die(mysql_error()); 

if($res){
    print("Records added successfully.");
}   

mysql_close($link);
die();

在此之后打开 console 部分的Web浏览器开发人员工具,看看会发生什么。

答案 4 :(得分:0)

我用altervista改变了主机。现在它的工作......我花了一天时间没事......该死的!所以,对于谁会遇到这个问题,请不要选择000webhost。因为有了它,你可以创建数据库,但你不能使用外部文件删除/添加/修改任何内容。