John Davidson

Insert a php string as multiple rows on MySQL table


I have a table with more than 750 rows of data that I paste on a textarea and get as a single string variable, then I pass the data as just one string thru an ajax and I want to insert it on my database in just one INSER INTO line

$data = mysqli_real_escape_string($conexion,$_POST['data']); // get the string of data

// on the next two lines I try to give the format like if it were multiple rows of data to insert
$data = str_replace("\t", "','", $data);
$data = str_replace("\n", "')('", $data);
// at this point $data = data1','data2','data3','data4','data5','data6')
// ('data1','data2','data3','data4','data5','data6')
// .
// .
// .
// .
// ('data1','data2','data3','data4','data5','data6
$sql = "INSERT INTO table(col1, col2, col3, col4, col5, col5, col6) VALUES ('$data')";
$result = mysqli_query($conexion,$sql);

Is there a way to make this work, I was able to insert the data on the database one value at a time, but is taking to long
Any ideas?

My Answer:
", "','", $data);
$data = str_replace("\n", "'),('", $data);

$query = "INSERT INTO table_name (column_name) VALUES ('$data')";
mysqli_query($conexion, $query);

Rate this post

3 of 5 based on 7301 votes


© 2024 - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us