I am making use of the function implode but I get bad argument error messages that I have tried to debug but im not sure how to deal with this much further.


	  	 $sqlarray = 'UPDATE qualityuser SET ' . implode(',', $sql_array) . ' WHERE username = ' . $_SESSION['username']; 
foreach ($_SESSION['formdata2'] as $col =>$val ){
	if($col <> 'submit'){
	$sqlarray .=", $col = '$val' ";
	}

$SESSION['formdata2'] is an array of fields from a form. . I am trying to UPDATE the fields in the table 'qualityuser' by refering to the array $SESSION['formdata2']
and making reference to the session variable $username but this is the result

Notice: Undefined variable: sql_array in /disk1/home3/mindseye/public_html/notts_quality/info_resource/selfassess_part2b_frmhandler_v3.php on line 98

Warning: implode() [function.implode]: Bad arguments. in /disk1/home3/mindseye/public_html/notts_quality/info_resource/selfassess_part2b_frmhandler_v3.php on line 98

Notice: Undefined variable: sql in /disk1/home3/mindseye/public_html/notts_quality/info_resource/selfassess_part2b_frmhandler_v3.php on line 105
Query was empty

I have tried to use 'UPDATE on duplicate key' but I cannot as my host provides MySQL Version 4.0.21 and UPDATE on duplicate key is only permisalble with version 5+.
Any pointers on how I can fix the syntax as it stands?

Thanks in advance

    I know this.
    I have assigned $sql_array to equate as "update qualityuser... etc
    If this is wrong what do I need to change?

      I have assigned $sql_array to equate as "update qualityuser... etc

      You have a variable named $sqlarray, and another named $sql_array. It is $sqlarray that you are trying to assign a string to. $sql_array must be an array that exists even before that line.

        If I understand correctly this is what you mean (here is code in more of a context)

         <?php
        		include "include/session.php";
        $_SESSION['formdata2']=$_POST;
        include("connect.php"); 
        //array to hold form errors       
        $form_error = array(); //validate your form data if(!isset($_POST['time'])){ $form_error[] = "No first name entered"; } if(!isset($_POST['demand'])){ $form_error[] = "No second name entered";
        }
        if(count($form_error) > 0){ //hold the error in session so you can display the error in your form page. $_SESSION['form_error'] = $form_error; //redirect to the form header("Location: http://www.mindseyemidlands.co.uk/notts_quality/info_resource/selfassess_part1a_v1.php");
        } else { $username = $_SESSION['username']; $sqlarray = array(); // build main query from form part 2 $sql= 'UPDATE qualityuser SET ' . implode(',', $sqlarray) . ' WHERE username = ' . $_SESSION['username']; foreach ($_SESSION['formdata2'] as $col =>$val ){ if($col <> 'submit'){ $sqlarray .=", $col = '$val' "; } } mysql_query($sql, $connection) or die(mysql_error());

        however I now have a different result

        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 'WHERE username = test170107v1' at line 1

          Print out your SQL statement and have a look. You might want to indent your code properly, e.g.

          <?php
          include "include/session.php";
          $_SESSION['formdata2'] = $_POST;
          include "connect.php";
          
          //array to hold form errors
          $form_error = array();
          
          //validate your form data
          if (!isset($_POST['time'])) {
              $form_error[] = "No first name entered";
          }
          if (!isset($_POST['demand'])) {
              $form_error[] = "No second name entered";
          }
          
          if (count($form_error) > 0) {
              //hold the error in session so you can display the error in your form page.
              $_SESSION['form_error'] = $form_error;
              //redirect to the form
              header("Location: http://www.mindseyemidlands.co.uk/notts_quality/info_resource/selfassess_part1a_v1.php");
          }  else {
              $username = $_SESSION['username'];
          
          $sqlarray = array();
          // build main query from form part 2
          $sql = 'UPDATE qualityuser SET ' . implode(',', $sqlarray) . ' WHERE username = ' . $_SESSION['username'];
          foreach ($_SESSION['formdata2'] as $col => $val) {
              if ($col <> 'submit'){
                  $sqlarray .=", $col = '$val' ";
              }
          }
          mysql_query($sql, $connection) or die(mysql_error());

            OK- so I debug like so:

             else { 
                $username = $_SESSION['username']; 
            
            $sqlarray = array(); 
            // build main query from form part 2 
            $sql = 'UPDATE qualityuser SET ' . implode(',', $sqlarray) . ' WHERE username = ' . $_SESSION['username']; 
            foreach ($_SESSION['formdata2'] as $col => $val) { 
                if ($col <> 'submit'){ 
                    $sqlarray .=", $col = '$val' "; 
                } 
            } 
            echo $sqlarray;
            echo $sql;
            mysql_query($sql, $connection) or die(mysql_error()); 
            

            after results I cannot see anything wrong with username:

            Array, username = 'test170107v1' , time = 'Low' , demand = 'Low' , external_support = 'Self assessment' , training = 'Low' , complexity = 'Low' , depth = 'Low' , partuse = 'Yes' , cost = 'Low' , orgwhole = 'Yes' , external_validated = 'Yes' , Submit = 'Submit' UPDATE qualityuser SET WHERE username = test170107v1You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE username = test170107v1' at line 1

              You can not use the array before it is being created. First create it, then make the sql.

              else {
                  $username = $_SESSION['username'];
              
              $sqlarray = array();
              foreach ($_SESSION['formdata2'] as $col => $val) {
                  if ($col <> 'submit'){
                      $sqlarray .=", $col = '$val' ";
                  }
              }
              // build main query from form part 2
              $sql = 'UPDATE qualityuser SET ' . implode(',', $sqlarray) . ' WHERE username = ' . $_SESSION['username'];
              
              echo $sqlarray;
              echo $sql;
              mysql_query($sql, $connection) or die(mysql_error());

              Edit: But why do you make a new array anyway? It should be possible to do the following.

              else {
                  $username = $_SESSION['username'];
              
              // build main query from form part 2
              $sql = 'UPDATE qualityuser SET ' . implode(',', $_SESSION['formdata2']) . ' WHERE username = ' . $_SESSION['username'];
              
              echo $sqlarray;
              echo $sql;
              mysql_query($sql, $connection) or die(mysql_error());

              And don't forget to use [man]mysql_real_escape_string[/man] on everything you use in a query.

                I see- is there some limitation when using implode as I am still getting a bad argument error message with both the solutions above Piranha

                Warning: implode() [function.implode]: Bad arguments. in /disk1/home3/mindseye/public_html/notts_quality/info_resource/selfassess_part2b_frmhandler_v3.php on line 105
                Array, username = 'test170107v1' , time = 'Low' , demand = 'Low' , external_support = 'Self assessment' , training = 'Low' , complexity = 'Low' , depth = 'Low' , partuse = 'Yes' , cost = 'Low' , orgwhole = 'Yes' , external_validated = 'Yes' , Submit = 'Submit' UPDATE qualityuser SET WHERE username = test170107v1You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE username = test170107v1' at line 1

                  I ran another test using:

                     else { 
                      $username = $_SESSION['username']; 
                  
                  // build main query from form part 2 
                  $sql = 'UPDATE qualityuser SET ' . implode(',', $_SESSION['formdata2']) . ' WHERE username = ' . $_SESSION['username']; 
                  
                  echo $sqlarray; 
                  echo $sql; 
                  mysql_query($sql, $connection) or die(mysql_error()); 
                  

                  and this results in

                  Warning: Invalid argument supplied for foreach() in /disk1/home3/mindseye/public_html/notts_quality/info_resource/selfassess_part2b_frmhandler_v3.php on line 99
                  ArrayUPDATE qualityuser SET WHERE username = test170107v1You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE username = test170107v1' at line 1

                    There still are nothing to change, I would guess that there is no values in $_SESSION['formdata2'].

                      this is a real head sratcher- i did a google search of 'update where bad argument implode' and found similar procedures to the one suggested to me but the same error message occurs. Tried this and same

                      "UPDATE qualityuser SET ".implode(",",$sqlarray)." WHERE username =".$username;
                      

                        This is so odd- I have renamed $SESSION['formdata2'] to $SESSION['formdata1'] as there is form completed earlier on and this time the results are:

                        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 'Low,Low,Self assessment,Low,Low,Low,Yes,Low,Yes,Yes,submit WHER

                        This makes me think that $_SESSIOn['formdata'] is being passed as you can see the values listed: Low,Self assessment,Low,Low,Low,Yes,Low,Yes,Yes,submit

                        To help the case here is an overview of the other form I referred to which works just fine- notice im using exactly the same syntax bar the UPDATE procedure.

                        <?php
                        include("include/session.php");
                        $_SESSION['formdata1']='';
                        $username = $_SESSION['username'];
                        include("connect.php");
                        // query 
                        $sql = "SELECT * FROM qualityuser WHERE username = '".$username."'"; 
                        $result = mysql_query($sql); 
                        // if we have rows, fetch them & prepopulate the form
                        if(mysql_num_rows($result) > 0) { 
                           $row = mysql_fetch_array($result); 
                        } else { 
                           $row = array('firstname'=>'','secondname'=>'','organisation'=>'','telephone'=>'','address1'=>'','address2'=>'','town'=>''
                           ,'postcode'=>'' ,'registeredcharity'=>'','incorporated'=>'','regionalassociation'=>'','regionalcharity'=>'','project'=>''
                           ,'nature_selfhelp'=>'','nature_frontline'=>'','nature_intermediary'=>'','nature_infrastructure'=>'','nature_partnership'=>''
                           ,'involved_commitee'=>'','involved_volunteers'=>'','involved_paid'=>'','funded_unrestrictedfunds'=>'','funded_localcouncil'=>''
                           ,'funded_undercontract'=>'','funded_grants_fees'=>'','funded_socialenterprise'=>'','activity_care'=>'','activity_advice_individual'=>''
                           ,'activity_advice_organisations'=>'','activity_specialist'=>'','activity_community'=>'','activity_awareness'=>'','activity_sport'=>''
                           ,'activity_mutual_support'=>'','have_policies_procedures'=>'','have_management_structure'=>'','have_communication'=>'','have_supervision'=>''
                           ,'have_strategy'=>'','have_aims'=>'','have_review'=>'');
                        } 
                        ?> 
                        
                        /////and then the form 
                        input name="firstname" type="text" id="firstname" value="<?=$row['firstname']?>" size="40"></td>
                        ////etc////
                        

                        and the formhandler for it

                        <?php 
                        include("include/session.php");
                        $_SESSION['formdata1']=$_POST;
                        include("connect.php"); 
                        //array to hold form errors       
                        $form_error = array(); //validate your form data if(!isset($_POST['firstname'])){ $form_error[] = "No first name entered"; } if(!isset($_POST['secondname'])){ $form_error[] = "No second name entered";
                        }
                        if(count($form_error) > 0){ //hold the error in session so you can display the error in your form page. $_SESSION['form_error'] = $form_error; //redirect to the form header("Location: http://www.mindseyemidlands.co.uk/notts_quality/info_resource/selfassess_part1a_v1.php");
                        } else { $username = $_SESSION['username']; // build main query from form part 1 $sql = "INSERT INTO qualityuser SET qualityuser_id = NULL "; foreach ($_SESSION['formdata1'] as $col =>$val ){ if($col <> 'submit'){ $sql .=", $col = '$val' "; }

                        }
                        mysql_query($sql, $connection) or die(mysql_error());

                        //remove the form_error session
                        unset($_SESSION['form_error']);
                        [/code]

                          Have I exhausted all of you with this problem- my apologies. I have reached a dead end with this one 🙂

                            Write a Reply...