I have a array containing some elements:
array(1) { ["dump"]=> string(21) "["0.0",1.1,2.2,99.99]" }
I need to insert them into in my table, but I don't know which datatype to use.
define('SECURE', true);
require "connect_to_mysql.php";
$sqlCommand = "CREATE TABLE example (
id int(11) NOT NULL auto_increment,
inserts float(5),
PRIMARY KEY (id),
UNIQUE KEY user_name (inserts))";
if ($mysqli->query($sqlCommand)) {
echo "Your example table has been created successfully!";
} else {
echo "CRITICAL ERROR;".$mysqli->error;
}
Moreover, each array element should be in a separate data field. How do I do that?
UPDATE
var_dump($_POST); // gives: array(1) { ["dump"]=> string(17) "[3.3,4.3,3.4,4.4]" }
$var["dump"] = $_POST;
$arr = json_decode($var["dump"], true);
$col1 = $arr[0];
$col2 = $arr[1];
$col3 = $arr[2];
$col4 = $arr[3];
what i need to do: 1. Count elements, in this case: 4. 2. save every element in a seperate variable. 3. insert those element´s in my table
jsen_encode just returns error in this case
you should use following command to create table
$sqlCommand = "CREATE TABLE example (
id int(11) NOT NULL auto_increment,
inserts1 float(5,2),inserts2 float(5,2),
inserts3 float(5,2),inserts4 float(5,2),
PRIMARY KEY (id),
)";
your variable is just single string. you need to split it to 4 variables. the easiest way to do is use json_decode()
in your variable format. also as @alok.kumar said. you need 4 columns (I think NUMERIC
is better than FLOAT
)
this is sample code:
$arr = json_decode($_POST["dump"], true); <= UPDATED
$col1 = $arr[0];
$col2 = $arr[1];
$col3 = $arr[2];
$col4 = $arr[3];
To normalize as other says, you need two tables. following is just example.
Table Schema:
CREATE TABLE example(
id INT NOT NULL auto_increment,
primary key(id)
);
CREATE TABLE dumps(
id INT NOT NULL auto_increment,
example_id INT NOT NULL,
dump_value NUMERIC(5,2),
PRIMARY KEY(id),
INDEX(example_id),
);
Insert through $_POST
ed data:
$arr = json_decode($_POST["dump"], true);
for ($cnt = 0; $cnt < count($arr); ++cnt)
{
$insert_query = "INSERT INTO dumps (dump_values) VALUES(".$arr[$cnt].")";
}
There are many possible approaches you can go ahead with.Let me list out some of the common approaches
Normalizing the data: This means you will have two tables one for saving the primary key and the second table to hold id to each element in array as a row. Pro : Data is better structured, better search of element Con : Logical Validation of data is harder to achieve ( as your combination of insert array is unique)
Serializing of insert : You can make you of serialize
and unserialize
function in php. So you can still get the data in db which will maintain datatype Pro : Well fits in php envirnment and maintains data type Con : Search will be slow as indexing wont work here. Not suited in places where data is is read from other languages like Java
JSON encoding: This is current trend, using json encoded text in DB if you dont not have a search option. json is well encoded by most of the languages and is compact. Pro : You can encode and decode in most langauge Con : Search is slow as indexing wont help.
Its based on your requirement and platform you need evaluate and decide on a format. var_dump
is not something I would suggest anyways
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.