0

I have the following code to process my PHP form:

$result1 = $db->query("SELECT `name` FROM staff_fields WHERE `".$_SESSION['logged_business']."` = 'Yes'")->fetchAll(PDO::FETCH_ASSOC);
$query = "UPDATE staff SET ";
foreach($result1 as $q){
    $query .= $q['name']."=:".$q['name'].", ";
}
$query = rtrim($query, ' ,')." WHERE id=:id";
$stmt = $db->prepare($query);
foreach($result1 as $col){
    $stmt->bindParam(':'.$col['name'], $_POST[$col['name']]);
}

$stmt->bindParam(':id', $_POST['id']);
if($stmt->execute())
    header('Location: staff.php?updated');
else
    echo "<center><h1>Error Updating Staff</h1></center>";

And the following code for the form it self.

$stmt = $db->prepare("SELECT * FROM staff_fields WHERE ".$_SESSION['logged_business']."='Yes'");
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt1 = $db->prepare("SELECT * FROM staff WHERE id=:id");
$stmt1->bindParam(':id', $_GET['id']);
$stmt1->execute();
while($row1 = $stmt1->fetchAll(PDO::FETCH_ASSOC)){
$table = "<form action='./edit_staff.php' method='post'>
            <table align='center'>";
    foreach($result as $res){
        $table .= 
                "
                    <tr>
                        <td>".$res['label']."</td>
                        <td>";

                        if($res['type'] == "text"){
                            $table .= "<input type='text' name='".$res['name']."' value='".$row1[0][$res['name']]."'/>";
                        }
                        if($res['type'] == "date"){
                            $table .= "<input type='date' name='".$res['name']."' value='";         
                            $date = new DateTime($row1[0][$res['name']]);
                            $table .= $date->format('Y-m-d')."'/>";
                        }
                        if($res['type'] == "yesno"){
                            $table .= "<select name='".$res['name']."' />";     
                            if($row1[0][$res['name']] == "Yes"){
                                    $table .= "<option value='Yes' selected>Yes</option>
                                                <option value='No'>No</option>";
                            }else{
                                $table .= "<option value='No' selected>No</option>
                                            <option value='Yes'>Yes</option>";
                            }   
                            $table .="</select>";
                        }

            $table .=  "</td>
                    </tr>
                ";
    }
    $table .="<tr><td><input type='hidden' number='id' value='".$row1[0]['id']."' /></td><td><input type='submit' name='btn' value='Add Staff' /></td></tr></table></form>";
    echo $table;
}

However despite debugging the code and investigating it isn't working. It is the first part of the code that isn't working (form submission). I tried using debug params on the update statement but this is what I get:

SQL: [642] UPDATE staff SET first_name1459057776924=:first_name1459057776924, last_name1459057788088=:last_name1459057788088, job_title1459057796608=:job_title1459057796608, proof_of_age_on_file1459057805910=:proof_of_age_on_file1459057805910, date_of_birth1459057814082=:date_of_birth1459057814082, start_date1459057824504=:start_date1459057824504, signed_contract_in_place1459057835607=:signed_contract_in_place1459057835607, references_received1459057869650=:references_received1459057869650, payroll_informa Params: 11 Key: Name: [24] :first_name1459057776924 paramno=-1 name=[24] ":first_name1459057776924" is_param=1 param_type=2 Key: Name: [23] :last_name1459057788088 paramno=-1 name=[23] ":last_name1459057788088" is_param=1 param_type=2 Key: Name: [23] :job_title1459057796608 paramno=-1 name=[23] ":job_title1459057796608" is_param=1 param_type=2 Key: Name: [34] :proof_of_age_on_file1459057805910 paramno=-1 name=[34] ":proof_of_age_on_file1459057805910" is_param=1 param_type=2 Key: Name: [27] :date_of_birth1459057814082 paramno=-1 name=[27] ":date_of_birth1459057814082" is_param=1 param_type=2 Key: Name: [24] :start_date1459057824504 paramno=-1 name=[24] ":start_date1459057824504" is_param=1 param_type=2 Key: Name: [38] :signed_contract_in_place1459057835607 paramno=-1 name=[38] ":signed_contract_in_place1459057835607" is_param=1 param_type=2 Key: Name: [33] :references_received1459057869650 paramno=-1 name=[33] ":references_received1459057869650" is_param=1 param_type=2 Key: Name: [45] :payroll_information_given_to_ho1459057881692 paramno=-1 name=[45] ":payroll_information_given_to_ho1459057881692" is_param=1 param_type=2 Key: Name: [26] :leaving_date1459057889857 paramno=-1 name=[26] ":leaving_date1459057889857" is_param=1 param_type=2 Key: Name: [3] :id paramno=-1 name=[3] ":id" is_param=1 param_type=2

How come this code isn't working? The staff_fields contains the fields and that is used in the form as well so I don't get why this isn't working.

I tried enabling the log in IIS through the my.ini but that didn't log anything as far as I could see.

THIS IS NOT A DUPLICATE!!!! $stmt->execute() returns TRUE because I am redirected to staff.php?updated!

I also have $db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); set so what you have linked does not help. Read the question before acting in haste.

  • Why you created columns with such name `first_name1459057776924`? – itzmukeshy7 Mar 29 '16 at 13:24
  • @itzmukeshy7 columns are created through an admin panel and to reduce the likelihood of name conflicts when the client makes them. It appends the timestamp –  Mar 29 '16 at 13:29
  • THIS IS NOT A DUPLICATE!!!! `$stmt->execute()` returns TRUE because I am redirected to `staff.php?updated`! I also have `$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );` set so what you have linked does not help. Read the question before acting in haste. –  Mar 29 '16 at 13:38
  • @YourCommonSense go tell that to everyone who comes here asking for help. If I have tried to debug it and couldn't figure it out so I came here for some help what is wrong with that? Firstly you mark it as a duplicate because you can't be bothered to read a question properly and then you refuse to admit it and unlock the question so someone HELPFUL can help. I guarantee you that when you unlock it someone will be able to help. Also, if it isn't related to PDO just remove the tag genius. –  Mar 29 '16 at 13:52
  • 1
    Unfortunately, this site is not for debugging help either. You are supposed to provide a specific problem you face, not just dump the code and ask what's wrong with it. I am sorry for that but the site rules are quite clear in this regard. In order to make this question on topic you may start from defining the "doesn't work" statement. – Your Common Sense Mar 29 '16 at 13:57
  • "It doesn't update in the database" –  Mar 29 '16 at 14:01
  • 1
    Then you may start with creating a Minimal, Complete, and Verifiable example: a code that creates a temporary table with sample data, a query that selects and displays current data, an update query, and another select query that displays the data after update. – Your Common Sense Mar 29 '16 at 14:17

1 Answers1

-1

Can you replace:

foreach($result1 as $q){
$query .= $q['name']."=:".$q['name'].", ";
}

With

foreach($result1 as $q){
$query .= $q['name']."='".$_POST[$q['name']]."', ";
}

You used form to submit the values but you have't captured the values posted from the form.

Hopefully this works.

Cheers

Ash
  • 27
  • 6
  • Hm.. There is something called PDO, which I use. –  Mar 29 '16 at 13:29
  • Yes, i know what PDO is. I saw you did Bindparam below which i missed earlier. Have you consider using serialize()? for all the parameters of payroll_informa Params. – Ash Mar 29 '16 at 13:47