0

I have a web-based system that reads a sheet of an excel file (each sheet uploaded is 6 rows-5 columns) like this:

enter image description here

Below is the code I'm using (for demonstration purposes I've set static values for the $entry but its real code would be for example $entry1=$objTpl->getActiveSheet()->getCell('A2')->getValue();

$con=mysqli_connect("localhost","root","","view");

if(isset($_POST['submit']))
{
$entry1 = 1;
$entry2 = 2;
$entry3 = 3;
$entry4 = 4;
$entry5 = 5;
$entry6 = 1;
$entry7 = 2;
$entry8 = 0;
$entry9 = 4;
$entry10 = 0;
$entry11 = 0;
$entry12 = 2;
$entry13 = 3;
$entry14 = 4;
$entry15 = 0;
$entry16 = 1;
$entry17 = 2;
$entry18 = 0;
$entry19 = 0;
$entry20 = 5;
$entry21 = 1;
$entry22 = 0;
$entry23 = 3;
$entry24 = 0;
$entry25 = 5;

if($entry1 != 0 || !empty($entry1))
{   
mysqli_query($con,"INSERT INTO viewer(base_id,name,value) VALUES('1','entry1','$entry1')");
}
if($entry2 != 0 || !empty($entry2))
{   
mysqli_query($con,"INSERT INTO viewer(base_id,name,value) VALUES('1','entry2','$entry2')");
}
if($entry3 != 0 || !empty($entry3))
{   
mysqli_query($con,"INSERT INTO viewer(base_id,name,value) VALUES('1','entry3','$entry3')");
}
if($entry4 != 0 || !empty($entry4))
{   
mysqli_query($con,"INSERT INTO viewer(base_id,name,value) VALUES('1','entry4','$entry4')");
}
if($entry5 != 0 || !empty($entry5))
{   
mysqli_query($con,"INSERT INTO viewer(base_id,name,value) VALUES('1','entry5','$entry5')");
}
if($entry6 != 0 || !empty($entry6))
{   
mysqli_query($con,"INSERT INTO viewer(base_id,name,value) VALUES('1','entry6','$entry6')");
}
if($entry7 != 0 || !empty($entry7))
{   
mysqli_query($con,"INSERT INTO viewer(base_id,name,value) VALUES('1','entry7','$entry7')");
}
if($entry8 != 0 || !empty($entry8))
{   
mysqli_query($con,"INSERT INTO viewer(base_id,name,value) VALUES('1','entry8','$entry8')");
}
if($entry9 != 0 || !empty($entry9))
{   
mysqli_query($con,"INSERT INTO viewer(base_id,name,value) VALUES('1','entry9','$entry9')");
}
if($entry10 != 0 || !empty($entry10))
{   
mysqli_query($con,"INSERT INTO viewer(base_id,name,value) VALUES('1','entry10','$entry10')");
}
if($entry11 != 0 || !empty($entry11))
{   
mysqli_query($con,"INSERT INTO viewer(base_id,name,value) VALUES('1','entry11','$entry11')");
}
if($entry12 != 0 || !empty($entry12))
{   
mysqli_query($con,"INSERT INTO viewer(base_id,name,value) VALUES('1','entry12','$entry12')");
}
if($entry13 != 0 || !empty($entry13))
{   
mysqli_query($con,"INSERT INTO viewer(base_id,name,value) VALUES('1','entry13','$entry13')");
}
if($entry14 != 0 || !empty($entry14))
{   
mysqli_query($con,"INSERT INTO viewer(base_id,name,value) VALUES('1','entry14','$entry14')");
}
if($entry15 != 0 || !empty($entry15))
{   
mysqli_query($con,"INSERT INTO viewer(base_id,name,value) VALUES('1','entry15','$entry15')");
}
if($entry16 != 0 || !empty($entry16))
{   
mysqli_query($con,"INSERT INTO viewer(base_id,name,value) VALUES('1','entry16','$entry16')");
}
if($entry17 != 0 || !empty($entry17))
{   
mysqli_query($con,"INSERT INTO viewer(base_id,name,value) VALUES('1','entry17','$entry17')");
}
if($entry18 != 0 || !empty($entry18))
{   
mysqli_query($con,"INSERT INTO viewer(base_id,name,value) VALUES('1','entry18','$entry18')");
}
if($entry19 != 0 || !empty($entry19))
{   
mysqli_query($con,"INSERT INTO viewer(base_id,name,value) VALUES('1','entry19','$entry19')");
}
if($entry20 != 0 || !empty($entry20))
{   
mysqli_query($con,"INSERT INTO viewer(base_id,name,value) VALUES('1','entry20','$entry20')");
}
if($entry21 != 0 || !empty($entry21))
{   
mysqli_query($con,"INSERT INTO viewer(base_id,name,value) VALUES('1','entry21','$entry21')");
}
if($entry22 != 0 || !empty($entry22))
{   
mysqli_query($con,"INSERT INTO viewer(base_id,name,value) VALUES('1','entry22','$entry22')");
}
if($entry23 != 0 || !empty($entry23))
{   
mysqli_query($con,"INSERT INTO viewer(base_id,name,value) VALUES('1','entry23','$entry23')");
}
if($entry24 != 0 || !empty($entry24))
{   
mysqli_query($con,"INSERT INTO viewer(base_id,name,value) VALUES('1','entry24','$entry24')");
}
if($entry25 != 0 || !empty($entry25))
{   
mysqli_query($con,"INSERT INTO viewer(base_id,name,value) VALUES('1','entry25','$entry25')");
}
}
?>

I do have a couple of questions:

  1. Would it be possible to have all of these statements into one?
  2. Are there any significant performance changes when doing one big query rather than multiple ones?
O. Jones
  • 103,626
  • 17
  • 118
  • 172
Carl Eren
  • 31
  • 7
  • Yes and yes. You can insert multiple sets of data in one statement, check http://dev.mysql.com/doc/refman/5.6/en/insert.html – jeroen Mar 21 '15 at 14:15
  • It's a little tricky though, since they aren't static, I can't make a static sql query. I can't join variables holding these `('1','entry1','$entry1')` together into one statement as I'll have problems placing the commas. One solution I found however, was to ALLOW 0s so that I can create a static query THEN delete all of the rows with 0 values. – Carl Eren Mar 21 '15 at 14:36

1 Answers1

1

Yes, you can do this with a single SQL statement. It would work like this. You can string together as many sets of (val, val, val) as you want. (There's a limit on the string length of the overall query, but it's quite large in MySQL).

INSERT INTO viewer(base_id,name,value) VALUES
      ('1','entry1','$entry1'),
      ('1','entry2','$entry2'),
      ('1','entry3','$entry3')

If you're inserting stuff into an InnoDB table, you get quite a lot of performance boost by doing this when your connection is in autocommit mode. That's because you commit all the rows in the insert together, and commit has an overhead.

Edit In php, if you have an array $values, each element of which contains a text string like (1,'val','val') you can use this code to create your query string.

$sqlstart = 'INSERT INTO viewer(base_id,name,value) VALUES ';
$sql = sqlstart . implode(',' , $values);

This puts commas between but not after the elements of the $values array. implode() is made for this kind of purpose.

So, your code might look like this:

$values = array();  /* make an empty array */
if ($entry1 != 0 || !empty($entry1)) {
   $values[] = "('1','entry1','$entry1')";  /* append to array */
}
if ($entry2 != 0 || !empty($entry2)) {
   $values[] = "('1','entry2','$entry2')";
}
/* etc etc for the rest of your $entry values */
$sqlstart = 'INSERT INTO viewer(base_id,name,value) VALUES ';
$sql = sqlstart . implode(',' , $values);

(Please beware of SQL insertion exploits).

O. Jones
  • 103,626
  • 17
  • 118
  • 172
  • I did try this path, however, I had troubles with the commas. If I tried making $statement1 = ('1','entry1','$entry1'); and have those $statements echo'ed out into the sql query. I can't place the commas properly since the values aren't static. – Carl Eren Mar 21 '15 at 14:27
  • If I tried to create a query that looks like this `INSERT INTO viewer(base_id,name,vale) VALUES $statement1,$statement2` and so on I would get in trouble with the commas as I can have two commas when a $statement doesn't exist since it's 0 or doesn't have a value. – Carl Eren Mar 21 '15 at 14:32
  • @CarlEren I don't understand the problem, you can build your single sql statement using the same logic you have now for multiple statements. – jeroen Mar 21 '15 at 14:43
  • I don't know how to put the commas to separate them since the values aren't static. If for example I have `$statement1=('1','entry1','$entry1'); $statement2=('1','entry2','$entry2');` then echo it out into this query `INSERT INTO viewer(base_id,name,value) VALUES $statement1 $statement2` but that's the problem, I don't know how to put the commas as I can't possible know which one has values. Keep in mind that this is a system that is used to upload those spreadsheets to the database and if I tried to upload a different spreadsheet with fewer values, my old queries won't make it. – Carl Eren Mar 21 '15 at 14:50
  • If I tried to create this query `"INSERT INTO viewer(base_id,name,value) VALUES $statement1,$statement2";` what would happen if the second spreadsheet I upload doesn't have an $entry2? that would mean `"INSERT INTO viewer(base_id,name,value) VALUES $statement1,";` since $statement2 would be declared IF $entry2 has a value. – Carl Eren Mar 21 '15 at 14:56
  • Good answer, although the comma I was pertaining to was the comma after the ('1','entry1','$entry1')`,` <--- that one – Carl Eren Mar 21 '15 at 15:52
  • That very comma is the one that won't be placed at the end of the string when you use `implode()` . – O. Jones Mar 21 '15 at 16:03
  • Oh that settles it then :D, Great answer. – Carl Eren Mar 21 '15 at 16:13