【问题标题】:How to insert data from a select in MYSQL using JSON?如何使用 JSON 从 MYSQL 中的选择中插入数据?
【发布时间】:2016-02-09 21:28:11
【问题描述】:

目的是我在名为“ReciboCliente”的表中成功插入,它是必需的和“用户 ID”,但我的 android apk 只发送“用户名”。我想在 INSERT 之前进行 SELECT 以从表“Agente”中更改“userID”的“用户名”。这个可以在web服务中解决,但是我做不到。

这是Web服务

中的功能
public function insertPolizaCliente2(Request $request)
{

    try {


        //fetch table rows from mysql db
        $connection = mysqli_connect("host", "user_name", "pass", "table");

        $sql = "select id from Agente where userID = ".$request->input('agentes')." limit 1";
        $result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));


        //create an array
        //$emparray = array();
        if($row =mysqli_fetch_assoc($result))
        {
            $agent[] = $row;
        }

        $agentes = json_decode($agent[]);


        $id = DB::table('ReciboCliente')
            ->insertGetId([
                'noRecibo' => $request->input('noRecibo'),
                'agenteID' => $agentes,  
                'totalPrima' => $request->input('totalPrima'),
                'montoRecibido' => $request->input('montoRecibido'),
                'nombreCliente' => $request->input('nombreCliente'),
                'ced_RNC_Cliente' => $request->input('ced_RNC_Cliente'),
                'telefonoCelularCliente' => $request->input('telefonoCelularCliente'),
                'telefonoOficinaCliente' => $request->input('telefonoOficinaCliente'),
                'direccionCasaCliente' => $request->input('direccionCasaCliente'),
                'direccionOficinaCliente' => $request->input('direccionOficinaCliente'),
                'conceptoRecibido' => $request->input('conceptoRecibido'),
                'formaPago' => $request->input('formaPago'),
                'descripcionVehiculo' => $request->input('descripcionVehiculo'),
                'chasis' => $request->input('chasis'),
                'Placa' => $request->input('Placa'),
                'cilindro' => $request->input('cilindro'), 
                'tonelada' => $request->input('tonelada'),
                'pasajero' => $request->input('pasajero'),
                'grua' => $request->input('grua'),
                'carcelEspecial' => $request->input('carcelEspecial'),
                'Placa' => $request->input('Placa'),
                'cilindro' => $request->input('cilindro'),
                'created_at' => \Carbon\Carbon::now()->toDateTimeString(),
                'fechaHasta' => \Carbon\Carbon::now()->toDateTimeString(),
                'updated_at' => \Carbon\Carbon::now()->toDateTimeString(),
            ]);






        if(!empty($id)){
            $posts[] = array('data' => 'inserted');
        } else {
            $posts[] = array('data' => 'not inserted');
        }


        return $this->json_response('200', 'successful', $posts);

    }
    catch (Exception $e){
        return $this->json_response('400', 'error', 'bad request');
    }

}

【问题讨论】:

  • 是你的生产数据库凭据???
  • 这是@devpro。我已经登录并窃取了所有数据。有趣的是他们的头像是“匿名的”¯\_(ツ)_/¯
  • 感谢您的数据库凭据。我们现在只需要您的信用卡、社会保险号和护照号码,以及您母亲的娘家姓……
  • 你不会接受任何重要的事情
  • 帮我解决这个问题:')

标签: php android mysql json


【解决方案1】:

您没有创建正确的 JSON。改变这个:

if($row =mysqli_fetch_assoc($result)){
        $agent[] = $row;
}

$agentes = json_decode($agent[]);

到这里:

if($row = mysqli_fetch_assoc($result)) {
    $agent[] = $row['id'];
}

$agentes = json_decode($agent);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-29
    • 2017-05-12
    • 1970-01-01
    相关资源
    最近更新 更多