0

I have code php. and I wont insert to database. this code wrong insert can help correction my code php

this code

$data = "3 003.3.003.003.006 31500.0,3 003.3.003.003.003 76500.0,4 003.3.003.003.002 72000.0,5 003.3.003.003.004 153750.0,6 003.3.003.003.005 187500.0,3 003.3.003.003.001 46500.0,";


$exone = explode(",",$data);
for ( $i = 0; $i < count( $exone ); $i++ ) { $extwo = explode(" ",$exone[$i]);

for ( $j = 0; $j < count( $extwo); $j++ ) { echo $extwo[$j]. "<br />";


$sql = "INSERT INTO tbl_test(qty, kode, price) VALUES('$extwo[$j]','$extwo[$j]',$extwo[$j]')";
echo $sql;}}
3
  • 1
    Just echoing your $sql won't do anything. Also, you're inserting the same value for qty, kode and price? What is the expected output and what do you get? Commented May 21, 2019 at 8:55
  • this $data $data = "3 003.3.003.003.006 31500.0, 3 003.3.003.003.003 76500.0, 4 003.3.003.003.002 72000.0, 5 003.3.003.003.004 153750.0, 6 003.3.003.003.005 187500.0, 3 003.3.003.003.001 46500.0, "; Commented May 21, 2019 at 9:04
  • i wont insert values for " $extwo[$j] " Commented May 21, 2019 at 9:07

2 Answers 2

1

String end with comma "," so probably at last point program tries to add null. Try to erase last commas.

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

4 Comments

after comma"," enter
Try this this $data $data = "3 003.3.003.003.006 31500.0, 3 003.3.003.003.003 76500.0, 4 003.3.003.003.002 72000.0, 5 003.3.003.003.004 153750.0, 6 003.3.003.003.005 187500.0, 3 003.3.003.003.001 46500.0,-";
yes..true data.. and then data don't insert to table $sql.. help or correction my code
"INSERT INTO tbl_test(qty, kode, price) VALUES('$extwo[$j]','$extwo[$j]','$extwo[$j]')" try this one and be carefull with quotation marks
0

There's no need for your second for loop, you can access the values after explode.

explode() your $data string by ,, loop over the results and explode() each result (if it is not empty) by space:

<?php

$data = "3 003.3.003.003.006 31500.0,3 003.3.003.003.003 76500.0,4 003.3.003.003.002 72000.0,5 003.3.003.003.004 153750.0,6 003.3.003.003.005 187500.0,3 003.3.003.003.001 46500.0,";
$exone = explode(",", $data);

foreach ( $exone as $x ) {
    if ( !empty($x) ) {
        $extwo = explode(" ", $x);
        // var_dump($extwo);
        $sql = "INSERT INTO tbl_test(qty, kode, price) VALUES('{$extwo[0]}','{$extwo[1]}','{$extwo[2]}');";
        echo $sql . PHP_EOL;
    }
}

Output:

INSERT INTO tbl_test(qty, kode, price) VALUES('3','003.3.003.003.006','31500.0');
INSERT INTO tbl_test(qty, kode, price) VALUES('3','003.3.003.003.003','76500.0');
INSERT INTO tbl_test(qty, kode, price) VALUES('4','003.3.003.003.002','72000.0');
INSERT INTO tbl_test(qty, kode, price) VALUES('5','003.3.003.003.004','153750.0');
INSERT INTO tbl_test(qty, kode, price) VALUES('6','003.3.003.003.005','187500.0');
INSERT INTO tbl_test(qty, kode, price) VALUES('3','003.3.003.003.001','46500.0');

If you uncomment the line // var_dump($extwo); you'll see that $extwo is an array with your desired values. You can access them using $extwo[0], $extwo[1] ...

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.