mysql - insert string from textarea having comma same like csv using php -
inserting data csv containing comma in name working fine . inserts same want e.g "test,name"
i inserting data database text area. using code
$addmult = nl2br($_post['textarea']); $data1 = explode('<br />',$addmult); foreach($data1 $dataline) { $line = explode(',',$dataline); $name = $line[0]; $partno = $line[1]; $detail = $line[2]; $qty = $line[3]; $price = $line[4]; $sql = "insert tablename set name=$name, partno=$partno, detail=$detail, qty=$qty, price=$price"; } it's working, if want insert "name" having comma in it. example wnant insert data (name="test,name")
test,name,123,testdetails,2,100 test,name2,321,testdetails2,22,200 the code above split name wnat name "test,name" , "test,name2"
thanks
will str_getcsv() work you? parse csv string array...
see: http://php.net/manual/en/function.str-getcsv.php
the following:
<?php $stringary = "\"test,name\",123,testdetails,2,100"; print_r(str_getcsv($stringary)); ?> note, "test,name" surrounded quotes in csv line, otherwise 2 columns , not one.
outputs...
array ( [0] => test,name [1] => 123 [2] => testdetails [3] => 2 [4] => 100 ) so
<?php $stringary = "\"test,name\",123,testdetails,2,100"; $sql = "insert tablename set name={stringary[0]}, partno={stringary[1]}, detail={stringary[2]}, qty=$qty, price={stringary[3]}"; ?> if user forgets quote name or details figure out have more columns should , combine adjacent non-numeric columns seeing have 2 non-numeric columns , separated numeric column.
also, know above example have considered this, in case: consider using mysqli_real_escape_string() user input, or equivalent db you're using...
Comments
Post a Comment