1
Answer

why pdo fails to allow new entries on the db column

Guest User

Guest User

2y
587
1

Hi Team

I need some help, my query to submit new entries on the table is failing and dont usually see any errors and dont know if i could create a new table for this to work. 

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();
}
PHP
Answers (1)
0
Manikandan Murugesan

Manikandan Murugesan

128 14.8k 450.5k 2y

Based on the code you provided, it seems that the code is only handling the case where the application ID is already set and updating an existing row in the "application" table. There doesn't seem to be any code for inserting new entries into the table.

To insert a new record into a table, you can use the INSERT INTO SQL statement.

$stmt = $conn->prepare("INSERT INTO application (amount, purpose, voucher, gender, fname, lname, title, typeID, email, number, maritalstatus, street1, street2, town, province, code, propertyownership, placeofwork, jobtitle, jobstreet, jobstreet2, jobtown, worknumber, jobprovince, jobcode, income, bankname, branchname, accountno, accounttype, signature, bankstatement, payslip) 
VALUES (:amount, :purpose, :voucher, :gender, :fname, :lname, :title, :typeID, :email, :number, :maritalstatus, :street1, :street2, :town, :province, :code, :propertyownership, :placeofwork, :jobtitle, :jobstreet, :jobstreet2, :jobtown, :worknumber, :jobprovince, :jobcode, :income, :bankname, :branchname, :accountno, :accounttype, :signature, :bankstatement, :payslip)");