Trouble with PDO update - php

I am updating a form using PDO's update function. For some reason it is not going through.
Here is code:
$data = "UPDATE insuranceverificationdisclaimer SET InsCoName =:insur, PhoneNumber = :phone, Policy = :policy, InsuredName = :insurname
, MailingAdrs = :mailingad, EffDate = :effdate, ExpDate = :expdate, Email1 = :email, YrVehicle = :yr, Make = :make
, Model = :model, VIN = :vin, TraineeUserName = :user, EmpName = :empname, EmpCoName = :empcomp, AgencyNumber = :agnum
, SignDate = :signdate, AgentName = :agname, AgentPhone = :agphone, AgentEmail = :agemail, Combinedlimit = :csl, bodyinjur = :body
, bodyinjureachacc = :acc
, propertydmg = :prop WHERE TraineeUsername = :user";
echo"1";
$insertdata = $DBH->prepare($data);
$insertdata->execute(array(':insur' => $compname, ':phone' => $phone , ':policy' => $policynum, ':insurname' => $nameofPolicyholder
, ':mailingad' => $newMailingAdrs, ':effdate' => $Policyeffdate, ':expdate' => $Policyexpdate, ':email' => $newEmployeeEmail
, ':yr' => $YearOfVehicle, ':make' => $MakeOfVehicle, ':model' => $ModelOfVehicle, ':vin' => $Vehicleid, ':user' => $username, ':empname' => $EmployeeName, ':empcomp' => $EmployeeCompanyName, ':agnum' => $Agencynum
, ':signdate' => $TodaysDate, ':agname' => $agentname, ':agphone' => $agentphone, ':agemail' => $agentemail, ':csl' => $singlelimit
, ':body' => $bodyinjur, ':acc' => $eachacc, ':prop' => $propertydmg ));
Where , ':csl' => $singlelimit, ':body' => $bodyinjur, ':acc' => $eachacc, ':prop' => $propertydmg begins this is the function that is not working, these are ints in the database and the values are ints. If I remove from the select and the array it will work but besides that it will not.
Let me know if you need anything else!
UDATED---------------------------
Wont go through once you hit execute page just stays white.

You should turn on your error reporting as suggested by Michael Berkowski. For now I can only assume that the error is cause by params datatypes not being defined. You can try the following:
$insertdata->bindParam(':insur', $compname, PDO::PARAM_STR);
$insertdata->bindParam(':csl', $singlelimit, PDO::PARAM_INT);
//...bind the other params
$insertdata->execute();

Related

Why this Query returns always HY093 Error

My Query return always the Mysql HY093 error but why?
I have all parameters and still its not working...
Can someone help ?
// create PDO params array
$params = [
':employee ' => $formData['employee'],
':usedFrom' => $formData['usedFrom'],
':usedTill' => $formData['usedTill'],
':company' => $formData['company'],
':costCenter' => $formData['costCenter'],
':vehicle' => $formData['vehicle'],
':vehicleTag' => $formData['vehicleTag'],
':updatedAt' => $formData['updatedAt'],
':ID' => $formData['ID']
];
// update entry
$return = $db->pquery(
'UPDATE entry SET employee_id = :employee, used_from = :usedFrom, used_till = :usedTill, company_id = :company, cost_center_id = :costCenter, vehicle_id = :vehicle, vehicle_tag = :vehicleTag, updated_at = :updatedAt WHERE ID = :ID',
$params
);

Datastax PHP Cassandra, getting "No Value or Column for name" error

Cassandra\Exception\InvalidArgumentException: No value or column for name
I'm trying to update some old code from YACassandraPDO to Datastax's new PHP extension, and I'm running into some issues. Notably, this error. I can't figure out why or what's wrong.
A parameter numbers match. Is there something drastically different Datastax is doing for prepared statements?
$query = "UPDATE {$this->keyspace}.visit USING TTL {$this->ttl}
SET ip=:ip, browser=:browser, referrer=:ref, terms=:terms
, url=:url, stime=:stime, etime=:etime
, company=:company, isp=:isp, domain=:domain, client_type = :client_type
, city=:city, state=:state_cd, postal_cd=:postal, country=:country
, country_cd=:country_cd, latitude=:lat, longitude=:long, area_cd=:area_cd
, user_id=:user_id
WHERE visit_dt=:dt AND cid=:cid AND sid=:sid AND pvid=:pvid";
The array is:
$params = [
':dt' => new Timestamp(strtotime(date('Y-m-d')), 0),
':cid' => $data['customerId'],
':sid' => $data['sid'],
':pvid' => $data['pvid'],
// Columns
':ip' => $ip,
':browser' => utf8_encode($data['browser']),
':client_type' => utf8_encode($device),
':ref' => utf8_encode($data['referrer']),
':terms' => utf8_encode($terms),
':url' => utf8_encode($data['url']),
':stime' => $data['now'],
':etime' => $data['etime'],
':user_id' => $data['userid'],
// Geodecode information
':company' => utf8_encode($geo->isp),
':domain' => utf8_encode($geo->domainName),
':isp' => utf8_encode($geo->isp),
':city' => utf8_encode($geo->cityName),
':state_cd' => utf8_encode($geo->regionName),
':country' => utf8_encode($geo->countryName),
':country_cd' => utf8_encode($geo->countryCode),
':postal' => $geo->zipCode,
':lat' => floatval($geo->latitude),
':long' => floatval($geo->longitude),
':area_cd' => $areac
];
Execution:
$prep = $this->adapter()->prepare($stmnt);
$return = $this->adapter()->execute($prep, ['arguments' => $params]);
Okay, I've discovered the issue: the colens. ':key' needs to be just 'key'.
Man, that took just too long to see. All my old bind names were $stmnt->bindValue(':company', [...]);; so, it completely escaped my view.

Running Multiple MySQL Queries in PHP

In PHP I'm attempting to build a form that needs to check for an ID in one table and if it exists it should create a record in another table. So far, that works but the issue I'm having is when I attempt to handle the case if the ID I checked for didn't exist. If it doesn't exist I'd like to create another one. But every time I try it, I get 500 errors from the server when I fetch the results.
Essentially I made the following function
function trySQL($con, $query, $params) {
$stmt = $con->prepare($query);
$result = $stmt->execute($params);
$id = $stmt->insert_id;
return array($stmt,$result,$id);
}
I call this function multiple times through out my php code but when I call it more then once and attempt to fetch the results it breaks.
$custINSquery = "
INSERT INTO custs (
FirstName,
LastName,
EmailAddress,
PhoneNumber
) VALUES (
:FirstName,
:LastName,
:EmailAddress,
:PhoneNumber
)
";
$createJob = "
INSERT INTO jobs (
custs_id,
StAddress,
State,
ZipCode,
MoistureLocation,
status_id
) VALUES (
:custs_id,
:StAddress,
:State,
:ZipCode,
:IssueDesc,
:status_id
)
";
$custSELquery = "SELECT id, FirstName, LastName, EmailAddress FROM custs WHERE FirstName = :FirstName AND LastName = :LastName AND EmailAddress = :EmailAddress";
$custSELquery_params = array(
':FirstName' => $_POST['FirstName'],
':LastName' => $_POST['LastName'],
':EmailAddress' => $_POST['EmailAddress']
);
$checkcust = trySQL($db, $custSELquery, $custSELquery_params);
$row = $checkcust[0]->fetch();
if(!$row){
$custINSquery_params = array(
':FirstName' => $_POST['FirstName'],
':LastName' => $_POST['LastName'],
':EmailAddress' => $_POST['EmailAddress'],
':PhoneNumber' => $_POST['PhoneNumber']
);
$custins = trySQL($db, $custINSquery, $custINSquery_params);
$custsel = trySQL($db, $custSELquery, $custSELquery_params);
$custs_id = $custsel[0]->fetch();
if ($custs_id != null) {
$createJobParam = array(
':custs_id' => $custs_id,
':StAddress' => $_POST['StAddress'],
':State' => $_POST['State'],
':ZipCode' => $_POST['ZipCode'],
':IssueDesc' => $_POST['MoistureLocation'],
':status_id' => $_POST['status_id']
);
$jobins = trySQL($db, $createJob, $createJobParam);
$jobres = $jobins[0]->fetch();
die("um...");
if ($jobres) {
# code...
die("looks like I made it");
}
}
} else {
$createJobParam = array(
':custs_id' => $row['id'],
':StAddress' => $_POST['StAddress'],
':State' => $_POST['State'],
':ZipCode' => $_POST['ZipCode'],
':IssueDesc' => $_POST['MoistureLocation'],
':status_id' => $_POST['status_id']
);
$data['success'] = true;
$data['message'] = 'Success!';
}
Additional Notes: When I look through the php doc's they are saying that I could use the inserted_id thing in order to get the ID that I inserted previously but when I try that it just gives me nulls with this set up.
Any help would be appreciated.
Thanks!

adding multiple rows in mysql table based on an array

I have a form which allows people to message each other, a user can select multiple people to message, when i submit a form it gives me the name and id of people selected to message in an array. uptil here i am able to get it to work for a single recipient
I want to be able to use this array and INSERT message for each user in different rows of mysql table
this is the array that i get when i submit a form
Array (
[to_user_id] => Array
(
[0] => 54
[1] => 55
)
[subject] => aaa
[message] => bbb
[send_message] =>
this is the part of code that works for a single recipient but not multiple
$to_user_id_array = ($_POST['to_user_id']);
$params = array(
':to_user_id' => $to_user_id_array,
':subject' => $_POST['subject'],
':message' => $_POST['message'],
':sender_id' => $this->user_id,
':status' => "0",
':type' => "message",
':sender_name' => $sender_name,
':to_user_name' => $to_user_name,
':delete_received' => 'no',
':delete_sent' => 'no',
);
$sql = "INSERT INTO `messages` (`sender_id`,`subject`,`comment`,`to_user_id`,`status`,`type`,`sender_name`,`to_user_name`,`delete_received`,`delete_sent`)
VALUES (:sender_id, :subject, :message, :to_user_id, :status, :type, :sender_name,:to_user_name,:delete_received,:delete_sent);";
parent::query($sql, $params);
$this->error = "<div class='alert alert-success'>" . _('Your message has been sent.') . "</div>";
Will really appreciate any help..
This is what worked for me, i hope this helps someone else in similar position
while ($value = $stmt->fetch(PDO::FETCH_ASSOC)) {
$params = array(
':to_user_id' => $value['user_id'],
':subject' => $_POST['subject'],
':message' => $_POST['message'],
':sender_id' => $this->user_id,
':status' => "0",
':type' => "message",
':sender_name' => $sender_name,
':to_user_name' => $value['name'],
':delete_received' => 'no',
':delete_sent' => 'no',
);
$sql = "INSERT INTO `messages` (`sender_id`,`subject`,`comment`,`to_user_id`,`status`,`type`,`sender_name`,`to_user_name`,`delete_received`,`delete_sent`)
VALUES (:sender_id, :subject, :message, :to_user_id, :status, :type, :sender_name,:to_user_name,:delete_received,:delete_sent);";
parent::query($sql, $params);
}

Zend db insertion exception

Please some body help me verified many times but i couldn't find where i did a mistake
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '' for key 'Index_6'
This is my code:
$first_name = $_POST ['firstName'];
$middle_name = $_POST ['middleName'];
$last_name = $_POST ['lastName'];
$office_ph_no = $_POST ['officePhoneNumber'];
$cell_ph_no = $_POST ['cellPhoneNumber'];
$email = $_POST ['emailId'];
$supervisor_name = $_POST ['supervisorName'];
$supervisor_email = $_POST ['supervisorEmail'];
$supervisor_ph = $_POST ['supervisorPhone'];
$pass = $_POST ['password'];
$sql = $db->query ( "INSERT INTO user( user_id, title, first_name, last_name, user_identity_id, email_id, password, office_phone_number, public_id, session_id, address_id, created_by, last_modified_by, created_on, last_modified_on, is_activated, is_deprecated, middle_name, cell_phone_number, superviser_name, superviser_email, superviser_phone_number)
VALUES ( :p_user_id, :p_title, :p_first_name, :p_last_name, :p_user_identity_id, :p_email_id, :p_password, :p_office_phone_number, :p_public_id, :p_session_id, :p_address_id, :p_created_by, :p_last_modified_by, :p_created_on, :p_last_modified_on, :p_is_activated, :p_is_deprecated, :p_middle_name, :p_cell_phone_number, :p_superviser_name, :p_superviser_email, :p_superviser_phone )",
array (
'p_user_id' => '',
'p_title' => NULL,
'p_first_name' => $first_name,
'p_last_name' => $last_name,
'p_user_identity_id' => 'id',
'p_email_id' => $email,
'p_password' => $pass,
'p_office_phone_number' => $office_ph_no,
'p_public_id' => NULL,
'p_session_id' => NULL,
'p_address_id' => NULL,
'p_created_by' => NULL,
'p_last_modified_by' => NULL,
'p_created_on' => '',
'p_last_modified_on' => '',
'p_is_activated' => '0',
'p_is_deprecated' => '0',
'p_middle_name' => $middle_name,
'p_cell_phone_number' => $cell_ph_no,
'p_superviser_name' => $supervisor_name,
'p_superviser_email' => $supervisor_email,
'p_superviser_phone' => $supervisor_ph
)
In array the name 'p_ is_deprecated' has a blank space.

Categories