I have a variable $username....

$username = $_SESSION['username'];

and two arrays....

$scorearray[1] = $pqasso_result;
$scorearray[2] = $iip_result;
$scorearray[3] = $iso_result;
$scorearray[4] = $excel_result;
$scorearray[5] = $social_result;
$scorearray[6] = $qmark_result;
$scorearray[7] = $chmark_result;
$scorearray[8] = $qfirst_result;
$scorearray[9] = $balanced_result;
$scorearray[10] = $bigpic_result;

and
$qualitysystem[1] = "PQASSO";
$qualitysystem[2] = "Investors in People";
$qualitysystem[3] = "ISO Standards";
$qualitysystem[4] = "Excel";
$qualitysystem[5] = "Social Audit";
$qualitysystem[6] = "Quality Mark";
$qualitysystem[7] = "Charter Mark";
$qualitysystem[8] = "Quality First";
$qualitysystem[9] = "Balanced Scorecard";
$qualitysystem[10] = "Big Picture";

I am trying to insert into a table these arrays like so

foreach .... {
$sql = "INSERT INTO qualityuserscores (username, qualitysystem, score)

$username
$scorearray[1]
$qualitysystem[1] =

so te table would like so:

Username qualitysystem score
tez pqasso 1000
tez iip 2000

my question is how do I build the array and insert syntax to achieve this?

    Untested, so there may be a syntax error or two. 🙂

    $values = array();
    foreach($scorearray as $key => $score)
    {
       // each element will be a parenthesized list of values:
       $values[] = sprintf("('%s','%s',%d)", $username, $qualitysystem[$key], $score);
    }
    // merge values into sequence of comma-separated value lists:
    $valueList = implode(',', $values);
    // create the SQL:
    $sql = "INSERT INTO `tablename` (`username`, `qualitysystem`, `score`) VALUES $values";
    

      thankyou for the reply nogdog. I have tried this and an error message comes up

      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 'Array' at line 1

      
      $username = $_SESSION['username']; 
      $scorearray[1] = $pqasso_result;
      $scorearray[2] = $iip_result;
      $scorearray[3] = $iso_result;
      $scorearray[4] = $excel_result;
      $scorearray[5] = $social_result;
      $scorearray[6] = $qmark_result;
      $scorearray[7] = $chmark_result;
      $scorearray[8] = $qfirst_result;
      $scorearray[9] = $balanced_result;
      $scorearray[10] = $bigpic_result;
      $qualitysystem[1] = "PQASSO";
      $qualitysystem[2] = "Investors in People";
      $qualitysystem[3] = "ISO Standards";
      $qualitysystem[4] = "Excel";
      $qualitysystem[5] = "Social Audit";
      $qualitysystem[6] = "Quality Mark";
      $qualitysystem[7] = "Charter Mark";
      $qualitysystem[8] = "Quality First";
      $qualitysystem[9] = "Balanced Scorecard";
      $qualitysystem[10] = "Big Picture";
      
      $values = array(); 
      foreach($scorearray as $key => $score) 
      { 
         // each element will be a parenthesized list of values: 
         $values[] = sprintf("('%s','%s',%d)", $username, $qualitysystem[$key], $score); 
      } 
      // merge values into sequence of comma-separated value lists: 
      $valueList = implode(',', $values); 
      // create the SQL: 
      $sql = "INSERT INTO `qualityuserscores` (`username`, `qualitysystem`, `score`) VALUES $values"; 
      
      $result = mysql_query($sql) or exit(mysql_error()); 
      
      }
      

        echo out your $sql statement and post it here

        depending on what values holds, you need () around the $values variable, unless they are held in that variable

          You're imploding the array into a string ($valueList) but then you're appending the array and not the string to the SQL query.

          Try:

          $sql = "INSERT INTO `qualityuserscores` (`username`, `qualitysystem`, `score`) VALUES $valueList"; 
          
            liquorvicar wrote:

            You're imploding the array into a string ($valueList) but then you're appending the array and not the string to the SQL query.

            Try:

            $sql = "INSERT INTO `qualityuserscores` (`username`, `qualitysystem`, `score`) VALUES $valueList"; 
            

            Told you it was untested. 🙂
            That looks like it should fix it. (Or at least do what I was trying to do.)

              Write a Reply...