Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Team

I need some help, my record is not updating any new entries and this is a problem for me because the user details not submitted and saved. I need some help with my code below

What I have tried:

require_once 'db_conn.php';

// Start a transaction
$conn->beginTransaction();

// Set the parameters
$amount = $_POST['amount'];
$purpose = $_POST['purpose'];
$voucher = $_POST['voucher'];
$gender = $_POST['gender'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$title = $_POST['title'];
$typeID = $_POST['typeID'];
$email = $_POST['email'];
$number = $_POST['number'];
$maritalstatus = $_POST['maritalstatus'];
$street1 = $_POST['street1'];
$street2 = $_POST['street2'];
$town = $_POST['town'];
$province = $_POST['province'];
$code = $_POST['code'];
$propertyownership = $_POST['propertyownership'];
$placeofwork = $_POST['placeofwork'];
$jobtitle = $_POST['jobtitle'];
$jobstreet = $_POST['jobstreet'];
$jobstreet2 = $_POST['jobstreet2'];
$jobtown = $_POST['jobtown'];
$worknumber = $_POST['worknumber'];
$jobprovince = $_POST['jobprovince'];
$jobcode = $_POST['jobcode'];
$income = $_POST['income'];
$bankname = $_POST['bankname'];
$branchname = $_POST['branchname'];
$accountno = $_POST['accountno'];
$accounttype = $_POST['accounttype'];
$signature = $_POST['signature'];
$bankstatement = $_POST['bankstatement'];
$payslip = $_POST['payslip'];
$id = null;

// Check if the application ID is set
if (isset($_POST['application'])) {
  $application_unique_id = $_POST['application'];
  
  // Lock the row for update
  $stmt = $conn->prepare("SELECT * FROM application WHERE id = :application_unique_id FOR UPDATE");
  $stmt->bindValue(':application_unique_id', $application_unique_id, PDO::PARAM_INT);
  $stmt->execute();

  $row = $stmt->fetch(PDO::FETCH_ASSOC);

  // Check if the row exists
  if ($row) {
    // Update the existing row
    $stmt = $conn->prepare("UPDATE application SET amount=:amount, purpose=:purpose, voucher=:voucher, gender=:gender, fname=:fname, lname=:lname, title=:title, typeID=:typeID, email=:email, number=:number, maritalstatus=:maritalstatus, street1=:street1, street2=:street2, town=:town, province=:province, code=:code, propertyownership=:propertyownership, placeofwork=:placeofwork, jobtitle=:jobtitle, jobstreet=:jobstreet, jobstreet2=:jobstreet2, jobtown=:jobtown, worknumber=:worknumber, jobprovince=:jobprovince, jobcode=:jobcode, income=:income, bankname=:bankname, branchname=:branchname, accountno=:accountno, accounttype=:accounttype, signature=:signature, bankstatement=:bankstatement, payslip=:payslip WHERE id=:application_unique_id");
    // Bind the parameters
    $stmt->bindValue(':amount', (string)$amount, PDO::PARAM_STR);
    $stmt->bindValue(':purpose', $purpose, PDO::PARAM_STR);
    $stmt->bindValue(':voucher', $voucher, PDO::PARAM_STR);
    $stmt->bindValue(':gender', $gender, PDO::PARAM_STR);
    $stmt->bindValue(':fname', $fname, PDO::PARAM_STR);
    $stmt->bindValue(':lname', $lname, PDO::PARAM_STR);
    $stmt->bindValue(':title', $title, PDO::PARAM_STR);
    $stmt->bindValue(':typeID', $typeID, PDO::PARAM_STR);
    $stmt->bindValue(':email', $email, PDO::PARAM_STR);
    $stmt->bindValue(':number', $number, PDO::PARAM_STR);
    $stmt->bindValue(':maritalstatus', $maritalstatus, PDO::PARAM_STR);
    $stmt->bindValue(':street1', $street1, PDO::PARAM_STR);
    $stmt->bindValue(':street2', $street2, PDO::PARAM_STR);
    $stmt->bindValue(':town', $town, PDO::PARAM_STR);
    $stmt->bindValue(':province', $province, PDO::PARAM_STR);
    $stmt->bindValue(':code', $code, PDO::PARAM_STR);
    $stmt->bindValue(':propertyownership', $propertyownership, PDO::PARAM_STR);
    $stmt->bindValue(':placeofwork', $placeofwork, PDO::PARAM_STR);
    $stmt->bindValue(':jobtitle', $jobtitle, PDO::PARAM_STR);
    $stmt->bindValue(':jobstreet', $jobstreet, PDO::PARAM_STR);
    $stmt->bindValue(':jobstreet2', $jobstreet2, PDO::PARAM_STR);
    $stmt->bindValue(':jobtown', $jobtown, PDO::PARAM_STR);
    $stmt->bindValue(':worknumber', $worknumber, PDO::PARAM_STR);
    $stmt->bindValue(':jobprovince', $province, PDO::PARAM_STR);
    $stmt->bindValue(':jobcode', $jobcode, PDO::PARAM_STR);
    $stmt->bindValue(':income', $income, PDO::PARAM_STR);
    $stmt->bindValue(':bankname', $bankname, PDO::PARAM_STR);
    $stmt->bindValue(':branchname', $branchname, PDO::PARAM_STR);
    $stmt->bindValue(':accountno', $accountno, PDO::PARAM_STR);
    $stmt->bindValue(':accounttype', $accounttype, PDO::PARAM_STR);
    $stmt->bindValue(':signature', $signature, PDO::PARAM_STR);
    $stmt->bindValue(':bankstatement', $bankstatement, PDO::PARAM_STR);
    $stmt->bindValue(':payslip', $payslip, PDO::PARAM_STR);

    
try {
    // Execute the statement
    $stmt->execute();

    // Get the number of rows affected
    $rowCount = $stmt->rowCount();
    echo "Updated $rowCount rows in the application table.";

    // Commit the transaction
    $conn->commit();

} catch (PDOException $e) {
    // Rollback the transaction on error
    $conn->rollBack();
    echo "Error: " . $e->getMessage();
}
Posted
Comments
Member 15627495 13-Mar-23 4:44am    
in your db, are all the 'fields' typed as 'string' ?

if you have a 'type conflict', the query won't pass when you execute it.
Gcobani Mkontwana 13-Mar-23 5:38am    
@Member 156274925 number within the table is int, rest set them as varchar data type.What am i missing here?
Member 15627495 13-Mar-23 7:16am    
to debug, you need to check all your Vars,

var_dump($var);
print_r($var);

// those two will display ( or not in case of fail -> (0)


the if($row) is too random as test for the $row value.
do :
if(count($row) == 1){ ... }



as you use FETCH_ASSOC to fetch your result, your var could be that place :
$row['0']['name of field data']

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900