【问题标题】:Writing in database via JSON php issue通过 JSON php 问题写入数据库
【发布时间】:2012-05-11 17:30:13
【问题描述】:

这里写的代码是RadarsMySql.php

 <?php
 $con = mysql_connect("localhost","root","dacian");


 $db = mysql_select_db("radars");

 $sql = mysql_query("SELECT latitude,longitude,description FROM radars WHERE id > '0'");

 while($row=mysql_fetch_assoc($sql))
 $output[]=$row;
 print(json_encode($output));
 foreach($out["radars"] as $radars) { 
 $latitude = addslashes($radars[latitude]); 
 $longitude= addslashes($radars[longitude]); 
 $description = addslashes($radars[description]);

 mysql_query("INSERT INTO radars (latitude, longitude, description) VALUES('$name', '$ingredients', '$ingredients2')") or die (mysql_error()); 
 }

  mysql_close(); ?>

它给了我这个错误:

Notice: Undefined variable: out in C:\xampp\htdocs\RadarsMySql.php on line 13

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\RadarsMySql.php on line 13

我需要这个,因为我想使用 JSON 从 android 应用程序写入数据。谁能告诉我出了什么问题或给我一些提示?

【问题讨论】:

  • 也许您想使用$output 而不是$out
  • PHP 给您的错误信息很有帮助、清晰且信息丰富。你试过阅读它们吗?
  • 据我所知,$out 没有在任何地方定义为数组。 $out 应该是什么?
  • 小心 SQL 注入

标签: php android mysql database json


【解决方案1】:
foreach ($out["radars"] as $radars) {}

如果$out["radars"] 是一个数组,这很好。如果不是,你会得到一个错误:Invalid argument supplied for foreach

在您的情况下,$out["radars"] 根本不存在。事实上$out 根本不存在。所以你得到另一个错误:Undefined variable out

您正在初始化变量$output,但随后尝试将其用作$out。那是行不通的。

要从数据库中提取数据,将其编码为 JSON,然后输出:

$sql = 'SELECT latitude,longitude,description FROM radars WHERE id>0'
$result = mysql_query($sql);

$rows = array();
while ($row = mysql_fetch_assoc($sql)) $rows[] = $row;
echo json_encode($rows);

要接收发布到服务器的 JSON,对其进行处理,并将其添加到数据库中:

// It's being posted as application/json, not as application/x-www-form-urlencoded, so it won't populate the $_POST array.
if ($json = file_get_contents('php://input')) {
    // Never mind. We'll do it ourselves.
    $a = json_decode($json, true); // Now we have a nice PHP array.
    $insert = '';
    foreach ($a as $v) {
        if ($insert) $insert .= ',';
        $insert .= ' ("' . $d->escape($v['lattitude']) . '", "' . $d->escape($v['longitude']) . '", "' . $d->escape($v['description']) . '")';
    }
    $sql = 'INSERT INTO `radars` (`latitude`, `longitude`, `description`) VALUES' . $insert;
    $d->exec($sql);
}

// I'm assuming you have a database class here, $d.
// $d->exec() could be simply mysql_query() or mysqli_query().
// $d->escape() should be mysql_real_escape_string() or mysqli_real_escape_string(), but both of those functions will fail if a database connection is not currently open.
// So $d->escape() should (a) check whether a connection is currently open, (b) open one if not, (c) perform the escape and return the result.

【讨论】:

  • 注意:未定义索引:第 13 行 C:\xampp\htdocs\RadarsMySql.php 中的雷达警告:为 C:\xampp\htdocs\RadarsMySql.php 中的 foreach() 提供的参数无效13
  • 这就是我用输出修改后得到的。没事吧?
  • @stanga。我不知道$output 是什么。尝试print_r($output); exit; 并将其添加到问题中。
  • 实际上,放弃它。我将完全重写我的答案。
  • 好的。我不太明白你在说什么,但我想知道你是否知道一个教程,或者你可以告诉我我应该怎么做/写在那里建立android应用程序和mysql数据库之间的连接?
【解决方案2】:

你在哪里定义 $out? ;)

foreach($out["radars"] as $radars) { 

【讨论】:

  • ... 无处可去。我是 mysql/php 编程的新手。你能帮我解决这个问题吗?我有 3 列 latitude , longitude , description 。我如何编写 php 以允许我从 android 应用程序发布?
【解决方案3】:

使用

     foreach($output["radars"] as $radars) { 

你在上面创建的打印语句

【讨论】:

  • 只收到通知和警告就可以了吗?没有错误?
猜你喜欢
  • 2017-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-08
  • 1970-01-01
相关资源
最近更新 更多