0

Im new in php and Im practicing in database and arrays. I have two tables in my database. I was able to get the datas in one of my table and save it in an array. Now what I'm trying to do is to save the datas in the array to my other table.

My array is multidime:

Array ( [0] => Array ( [recNAME] => NAME1 [company] => [address1] => ADDRESS1 [address2] => [city] => CITY1 [province] => PROVINCE1 [postalCODE] => 0 [contactNO] => 926XXXXX [email] => [email protected] [commodity] => COMMODITY1 [type] => [weight] => 0 [length] => 0 [width] => 0 [height] => 0 [decVAL] => 0 [specINSTRUC] => [shipREF] => REF1 [payMETHOD] => CashOnDelivery [packNO] => 200XXXX-XXX [trackNO] => 200XXXX-XXX ) [1] => Array ( [recNAME] => NAME2 [company] => [address1] => ADDRESS2 [address2] => [city] => CITY2 [province] => PROVINCE2 [postalCODE] => 0 [contactNO] => 905XXXXXX [email] => [email protected] [commodity] => COMMODITY2 [type] => [weight] => 0 [length] => 0 [width] => 0 [height] => 0 [decVAL] => 361 [specINSTRUC] => I'm in the above given address from M-F, 11AM-6PM, call or text me for confirmation [shipREF] => 200XXXXX [payMETHOD] => CashOnDelivery [packNO] => 200XXXX-XXX [trackNO] => 200XXXX-XXX ) )

what are the ways to save it to my other table? I searched a few and most of them are to serialize. I don't know how that would save it to the table. Please help. Regards.

3 Answers 3

1

You have to make dynamic string array and implode each value separate by common into the string.

The dynamic string would be generated and then you can insert that string into the table.

Sign up to request clarification or add additional context in comments.

Comments

1

Yo can use php serialize method for generating a storable representation of a value. And when you get data from database, use unserialize

5 Comments

I saw samples of serialize method here but I didn't see that the serialize method indicates what table to pass the array to. will it make another table?
You must serialize your array and insert it into table as you like, but if you want insert each element from loop, you must use method foreach and insert every loop element into your table
after I use $serialized = serialize($arr); how can I indicate as to which table will I insert it?
INSERT INTO table (id, my_array) VALUES(0, ".$serialized.") - table, is your same table for storing the array
so I'll write it like this: $myquery = "INSERT INTO temp (id, my_array) VALUES(0, ".$serialized."); ??
1

You have to write a query to achieve that, ofcourse you have to loop your Array.

There are ofcourse better ways to achieve this but for simple practicing I suggest you do it this way, you got alot of string cutting and so on with it.

Use echo $query to test the result of your code before executing it.

A working example could look like this:

$query = "INSERT INTO myTable (`name`, `company`, `adress_1`) VALUES (";

foreach($myArray as $data) {
    $query .= "'" . mysql_real_escape_string($data["recNAME"]) . "', ";
    $query .= "'" . mysql_real_escape_string($data["company"]) . "', ";
    $query .= "'" . mysql_real_escape_string($data["address1"]) . "'), (";
}

$query = substr($query, 0, strlen($query) - 3);

echo $query;

mysql_query($query);

The best solution is to write / find a useful MySQL handler class that delivers simple functions like an insert function that accepts an array.

But I am not certain how much you know about OOP. It is best if you understand the princip how to do it manually and then write the class yourself, then you have fully understood it for sure.

However, the more complex way: This is not a complete example!

class MySQL
{
    private $handle = null;
    private function connect() {
        $this->handle = mysql_connect("127.0.0.1", "user", "pass", "db");
    }

    public function execute($query) {
        if($this->handle == NULL) {
            $this->connect();
        }
        if($this->handle != NULL) {
            mysql_query($query, $this->handle);
        }
    }

    public function insert($table, $fields, $values) {
        $query = "";
        if(is_array($fields) == true && is_array($values) == true) {
            $query .= "INSERT INTO `$table` (";
            foreach($fields as $field) {
                $query .= "`$field`" . ", ";
            }
            $query = substr($query, 0, strlen($query) - 2);
            $query .= ") VALUES (";
            $count = 0;
            foreach($values as $value) {
                $query .= "'" . mysql_real_escape_string($value) . "'";
                if($count == count($fields)) {
                    $query .= ", ";
                } else {
                    $query .= "),(";
                }
                $count++;
            }
            $query = substr($query, 0, strlen($query) - 2);
        }

        if(strlen($query) > 0) {
            $this->execute($query);
            return true;
        }

        return false;
    }
}

Ofcourse this example is not very complete, its just a short example you can store results, numrows and much more in the class, however here is the usuage:

$sql = new MySQL();
$sql->insert("myTable", array("name, "company", "adress_1"), array("guy, "company a", "street 1", "girl", "company b", "street 2"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.