Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an array.Now I want to insert data in my table using this array value. $claw is the array

Array
(
    [0] => Array
        (
            [0] => Alabama
            [1] => Yes
            [2] => R
            [3] => 

are free to enact restrictions greater than .

            [4] => 
            [5] => 
            [6] => 
            [7] => 

It is unlawful to sell
        )
)

Now I want to insert data in my table using this array value.

I am using this code but not work

    public function Insert($claw)
    {


                $fields = array();
                for($i=0;$i<$claw; $i++) 
                {
                    for($j=0;$j<$claw[$i]; $j++) 
                    {               
                        $fields[] = $claw[$i][$j];
                    }
                }

                $sql = "Insert into information values(" . implode(', ', $fields) . ')';

return $this->MyCommandR($sql);


    }

I cant understand whats my wrong

share|improve this question
    
You need to quote your values, and you should really escape them as well. –  Chelsea Jun 22 at 6:21
    
Problem 1: $i<$claw ... that doesn't seem right. Problem 2: Your insert values need quotes. –  Mark M Jun 22 at 6:21
    
$i<count($claw) –  Feroz Akbar Jun 22 at 6:32
add comment

1 Answer 1

You need to define the field to save these data $sql = "Insert into information (name,yes,ABC,CBA) values(" . implode(', ', $fields) . ')';

or

$field = $conn->query("DESCRIBE  `".$table."`"); 
$constrantsQueryfield = array();
$constrantsQueryvalue = array();
while($row = mysqli_fetch_assoc($field))
{

if ($row['Field']!='ID' && $row['Key']!='PRI')
{
if (array_key_exists($row['Field'],$data))
{
if ($row['Key']=='DATE')
{
    $constrantsQueryfield[] = $row['Field'];
$constrantsQueryvalue[] = date("Y-m-d");
}
else
{
$constrantsQueryfield[] = $row['Field'];
$constrantsQueryvalue[] = $data[$row['Field']];
}
}
}
}
$constrantQuery = "INSERT INTO `".$table."` ";
for ($i = 0;$i<count($constrantsQueryfield);$i++)
{
if ($i == 0){
$constrantQuery .= "(";
}
$constrantQuery .= "`".$constrantsQueryfield[$i]."`";
if ($i+1 == count($constrantsQueryfield))
{
$constrantQuery .= ")";

}
else
{
    $constrantQuery .= ", ";

    }
}
$constrantQuery .= "  VALUES  ";
$contstantQuery .= "(" . implode(', ', $data) . ')'; // ANy data array

$conn->query($constrantQuery);
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.