简体   繁体   English

使用php在数据库中存储数组值?

[英]Storing array values in he database using php?

I am trying to create an php script which can read and array list from android and read it's element and store it in data base, now I have been able to store the data in an text file but unable to proceed further, could some body help me out in this matter?我正在尝试创建一个 php 脚本,它可以从 android 读取和数组列表并读取它的元素并将其存储在数据库中,现在我已经能够将数据存储在一个文本文件中,但无法继续进行,有人可以帮忙吗我出在这件事上? this is the php script which I have used:-这是我使用的 php 脚本:-

<?php
$i=0;
$password="";
$user="root";
$database="shadowpets";
$host="localhost";
$response=array();
$con=mysqli_connect($host,$user,$password,$database)or die("Unable to connect");
if($_SERVER["REQUEST_METHOD"]=="POST"){

    if(isset($_POST['OrderSummary'])){

        $data=$_POST['OrderSummary'];
        $file='text.txt';
         $result=file_put_contents($file,$data);  

            $response["success"]=1;
            $response["message"]="done";
    }else{
        $response["success"]=0;
        $response["message"]="parameters not correctly formatted";
    }

    echo json_encode($response);
}
    ?>

what changes I can do here, so I can read the data store it in the database?我可以在这里做哪些更改,以便我可以读取存储在数据库中的数据?

First, you need to set up a database and a table in a database server (MySQL).首先,您需要在数据库服务器(MySQL)中设置数据库和表。

Then you can create an insert statement like below, bind its parameters and execute it.然后,您可以创建如下所示的插入语句,绑定其参数并执行它。

if (!($statement = $con->prepare('INSERT INTO `your_table` (`OrderSummary`) VALUES (?)'))) {
    die('Could not prepare statement: (' . $con->errno . ') ' . $con->error);
}
if (!$statement->bind_param('s', $_POST['OrderSummary'])) {
    die('Could not bind parameter: (' . $statement->errno . ') ' . $statement->error);
}
if (!$statement->execute()) {
    die('Could not execute statement: (' . $statement->errno . ') ' . $statement->error);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM