2

I created a for loop that loops through an array or words exploded from a string. The for loop works and has been tested by echo statements. When running this code it only inserts 1 record into the database rather than the number of strings in the array.

$item=explode(" ", $items);
for ($i = 0; $i < count($item); ++$i) {
    // Create connection
$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "INSERT INTO invlog (itemid, qty)
VALUES ('".$item[$i]."', '-1')";

if (mysqli_query($conn, $sql)) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

mysqli_close($conn);    
    }
7
  • 3
    sidenote: why not take out that connection outside before the loop. no need to connect/close every iteration Commented Nov 22, 2014 at 2:11
  • Are you getting an error message or does it seem to be failing silently? Commented Nov 22, 2014 at 2:11
  • 1
    Connecting and closing for each iteration like this is absurd. Commented Nov 22, 2014 at 2:16
  • no errors. also I have tried moving both the close and conn outside the for many times, still has the same issues Commented Nov 22, 2014 at 2:19
  • @chuckbeyor what does $items contain? you should post it in the question too. if one of those words are quoted, then your query will be screwed. you should bind them instead. Commented Nov 22, 2014 at 2:23

2 Answers 2

2

For efficiancy, and to avoid multiple database calls, you should really only execute the statemnt once.

So loop through, create the query, then execute it.

Like so..

$item=explode(" ", $items);
$query="";
 for ($i = 0; $i < count($item); ++$i) {
  // Build Query 
  $query.="(".$item[$i].",-1),";
 }
 $query = rtrim($query); // Remove last trailing comma from the right hand side of query string

 //Connect to database, and do DB stuff outside of loop in one call
 $conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
 // Check connection
 if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
 }
 $sql = "INSERT INTO invlog (itemid, qty)
 VALUES ".$query;

  if (mysqli_query($conn, $sql)) {
    echo "New records created successfully";
  } else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
  }

  mysqli_close($conn);    
Sign up to request clarification or add additional context in comments.

6 Comments

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '4321 9876 6543,-1),'
trialling comma is still there
@chuckbeyor well if you look at that, it wasn't really exploded. '4321 9876 6543,-1),' the numbers are still joined with a space. you should put var_dump($item) in your question as well. and just use rtrim($query, ',') for that
This is the query it makes (itemid, qty) VALUES (1234 4321 9876 6543,-1)
I got a question. If these were submited in a text area and each one was on a new line might that be a issue?
|
0
$replacer  = array("\r\n", "\n", "\r", "\t", "  ");
$items = str_replace($replacer, " ", $items);
$item=explode(" ", $items);
// Create connection
$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}


for ($i = 0; $i < count($item); ++$i) {

$sql = "INSERT INTO invlog (itemid, qty)
VALUES ('".$item[$i]."', '-1')";

if (mysqli_query($conn, $sql)) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}


    }
    mysqli_close($conn);  

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.