I am building chats pop up where one user will initiate conversation with another user.
In my initiatechat function i have the following error Call to undefined method stdClass::save().
Look at codes and find THIS SAVE FUNCTION ==>> the save() function that throw error.
If two users have already initiated conversation this error does not happen.
public function actionInitiatechat() {
if (isset($_POST)){
//$message = Myclass::checkPostvalue($_POST['message']) ? $_POST['message'] : "";
$senderId = Myclass::checkPostvalue($_POST['sender']) ? $_POST['sender'] : "";
$receiverId = Myclass::checkPostvalue($_POST['receiver']) ? $_POST['receiver'] : "";
$messageType = Myclass::checkPostvalue($_POST['messageType']) ? $_POST['messageType'] : "";
$sourceId = Myclass::checkPostvalue($_POST['sourceId']) ? $_POST['sourceId'] : "";
$timeUpdate = time();
$message = $_POST['message'];
$Products = Products::model()->findByPk($sourceId);
if(isset($Products) && $Products->approvedStatus == 0)
{
echo "error";
}
else
{
$criteria = new CDbCriteria;
$criteria->condition = "(user1 = '$senderId' AND user2 = '$receiverId') OR (user1 = '$receiverId' AND user2 = '$senderId')";
$chatModel = Chats::model()->find($criteria);
$encodeMsg = urlencode($message);
if (empty($chatModel)){
$newChat = new Chats();
$newChat->user1 = $senderId;
$newChat->user2 = $receiverId;
$newChat->lastMessage = $encodeMsg;
$newChat->lastToRead = $receiverId;
$newChat->lastContacted = $timeUpdate;
$newChat->save();
$criteria = new CDbCriteria;
$criteria->condition = "(user1 = '$senderId' AND user2 = '$receiverId') OR (user1 = '$receiverId' AND user2 = '$senderId')";
$chatModel = Chats::model()->find($criteria);
}
$chatModel->lastContacted = $timeUpdate;
if ($chatModel->user1 == $senderId){
$chatModel->lastToRead = $chatModel->user2;
}else{
$chatModel->lastToRead = $chatModel->user1;
}
$chatModel->lastMessage = $encodeMsg;
THIS SAVE FUNCTION ==>> $chatModel->save();
$messageModel = new Messages();
$messageModel->message = $encodeMsg;
$messageModel->messageType = $messageType;
$messageModel->senderId = $senderId;
$messageModel->sourceId = $sourceId;
$messageModel->chatId = $chatModel->chatId;
$messageModel->createdDate = $timeUpdate;
$messageModel->save();
}
echo "success";
}
}
else
{
echo "failed";
}
}
The statement below is presumably returning a "bare" object of type stdClass, which doesn't define a save() method, hence your error.
$chatModel = Chats::model()->find($criteria);
Run var_dump($chatModel); immediately after this statement and see what type of object you're getting.
I need to find duplicated details in the existing table while uploading a Excel file that contains some details,i need to find that by phone number and customer name. I am using mattexcel to upload the data into database.
I don't want to insert that details if it is in there but other details must insert into that table
Controller
public function importExcel(Request $request)
{
if ($request->hasFile('import_file')) {
Excel::load($request->file('import_file')->getRealPath(), function ($reader) {
foreach ($reader->toArray() as $key => $row) {
$data['customername'] = $row['customername'];
$data['chassis'] = $row['chassis'];
$data['model'] = $row['model'];
$data['branchcode'] = $row['branchcode'];
$data['delivery'] = $row['delivery'];
$data['customerid'] = $row['customerid'];
$data['phone'] = $row['phone'];
$data['invoicedate'] = $row['invoicedate'];
$data['dse'] = $row['dse'];
$data['branch'] = $row['branch'];
$data['finance'] = $row['finance'];
$data['dono'] = $row['dono'];
$data['invoice'] = $row['invoice'];
$data['zsm'] = $row['zsm'];
$data['sm'] = $row['sm'];
$data['agm'] = $row['agm'];
$data['dsecode'] = $row['dsecode'];
$data['address'] = $row['address'];
$data['email'] = $row['email'];
$data['color'] = $row['color'];
$data['extendedwarrenty'] = $row['extendedwarrenty'];
$data['autocaddownload'] = $row['autocaddownload'];
$data['numberplate'] = $row['numberplate'];
$data['mcpstatus'] = $row['mcpstatus'];
$data['plandt'] = $row['plandt'];
$data['planok'] = $row['planok'];
$data['fasttag'] = $row['fasttag'];
// $data['settilment_pdf_path'] = $row['settilment_pdf_path'];
$data['rcstatus'] = $row['rcstatus'];
$branch = Branch::where([['branch_code', $row['branchcode']], ['status', 0]])->first();
$registration_id = Registration::orderBy('registration_id', 'desc')->take(1)->get();
if (count($registration_id) > 0) {
$regid = $registration_id[0]->registration_id;
$regid = $regid + 1;
} else {
$regid = 1;
}
$register = new Registration();
$register->registration_id = $regid;
$register->customername = $row['customername'];
$register->chassis = $row['chassis'];
$register->model = $row['model'];
$register->branchcode = $row['branchcode'];
$register->delivery = $row['delivery'];
$register->customerid = $row['customerid'];
$register->phone = $row['phone'];
$register->invoicedate = $row['invoicedate'];
$register->dse = $row['dse'];
$register->branch = $row['branch'];
$register->finance = $row['finance'];
$register->dono = $row['dono'];
$register->invoice = $row['invoice'];
$register->zsm = $row['zsm'];
$register->sm = $row['sm'];
$register->agm = $row['agm'];
$register->dsecode = $row['dsecode'];
$register->address = $row['address'];
$register->email = $row['email'];
$register->color = $row['color'];
$register->extendedwarrenty = $row['extendedwarrenty'];
$register->autocaddownload = $row['autocaddownload'];
$register->numberplate = $row['numberplate'];
$register->mcpstatus = $row['mcpstatus'];
$register->plandt = $row['plandt'];
$register->planok = $row['planok'];
$register->fasttag = $row['fasttag'];
$register->rcstatus = $row['rcstatus'];
$register->dealership = $branch->dealership_id;
$register->zone = $branch->zone_id;
$register->dh = $branch->dh_id;
$register->status = '0';
$register->created_user_id = Session::get('created_id');
$register->save();
$regidn = Registration::orderBy('registration_id', 'desc')->get();
$regidd = $regidn[0]->registration_id;
$ssitrack = new Ssi_track();
$ssitrack->registration_id = $regid;
$ssitrack->ssi_track_id = $regid;
$ssitrack->save();
$ssitrackk = Ssi_track::orderBy('ssi_track_id', 'desc')->get();
$ssitrackk = $ssitrackk[0]->registration_id;
}
});
}
return back()->with('success', 'Your File Is Successfully Uploaded To Database!');
}
Option 1. You can add unique values combination in migration.
Schema::table('your_table_name', function (Blueprint $table) {
$table->unique(['phone ','customername ']);
});
This won't let you insert same combination values for these column combination, however it also throws error stopping you import function.
Option 2 (Better).
Check if value already exits and ignore import for that column.
$old_customer = Regiter::where('phone', $row['phone'])->where('customername', $customername )->first();
//Inser only if customer not found
if(is_null($old_customer))
{
//INSERT QUERY
}
To decrease number of query you can pluck name and phone with single query or use any other optimization tricks.
I have seen the other examples here on StackOverflow but neither are working for me, my code creates an address line in NetSuite but the addr1, city, state and zip are empty, the default billing and shipping do show false or if I set it to true it shows true so that part is updating.. The response doesn't show any errors. Any ideas?
Here is my code:
$customer = new Customer();
$customer->internalId = 16;
$customer->firstName = 'Joe';
$customer->middleName = 'A';
$customer->lastName = 'Smith';
$customer->email = 'joe#email.com';
$address = new CustomerAddressBook();
$address->defaultShipping = false;
$address->defaultBilling = false;
$address->isResidential = true;
$address->addr1 = '123 Street';
$address->city = 'New York';
$address->zip = '12345';
$address->state = 'NY';
$addressBook = new CustomerAddressbookList();
$addressBook->addressbook = array($address);
$addressBook->replaceAll = false;
// add address to cutomer
$customer->addressbookList = $addressBook;
$request = new UpdateRequest();
$request->record = $customer;
$netsuiteService = new NetSuiteService();
$response = $netsuiteService->update($request);
$address = new Address();
$address->addr1 = '123 Street';
$address->city = 'New York';
$address->zip = '12345';
$address->state = 'NY';
$address_book = new CustomerAddressBook();
$address_book->defaultShipping = false;
$address_book->defaultBilling = false;
$address_book->isResidential = true;
$address_book->addressbookAddress = $address;
$address_book_list = new CustomerAddressbookList();
$address_book_list->addressbook = $address_book;
$address_book_list->replaceAll = false;
$customer = new Customer();
$customer->internalId = 16;
$customer->firstName = 'Joe';
$customer->middleName = 'A';
$customer->lastName = 'Smith';
$customer->email = 'joe#email.com';
$customer->addressbookList = $address_book_list;
$request = new UpdateRequest();
$request->record = $customer;
$netsuiteService = new NetSuiteService();
$response = $netsuiteService->update($request);
if (!$response->writeResponse->status->isSuccess) {
echo "UPDATE ERROR";
} else {
echo "UPDATE SUCCESS, id " . $response->writeResponse->baseRef->internalId;
}
Data not being updated when i am trying to update, its showing could not update data only always, i have the session carrying reg from my registration table and i want to update with reg in my slam table in which reg is present. here reg is present in both tables.
<?php
session_start();
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$con=mysqli_connect("xyz","aaa","aaa","aaa");
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$reg = $_REQUEST["reg"];
$t1 = $_REQUEST["t"];
$t2 = $_REQUEST['t2'];
$t3 = $_REQUEST['t3'];
$t4 = $_REQUEST['t4'];
$t5 = $_REQUEST['t5'];
$t6 = $_REQUEST['t6'];
$t7 = $_REQUEST['t7'];
$t8 = $_REQUEST['t8'];
$t9 = $_REQUEST['t9'];
$t10 = $_REQUEST['t10'];
$t11 = $_REQUEST['t11'];
$t13 = $_REQUEST['t13'];
$t14 = $_REQUEST['t14'];
$t15 = $_REQUEST['t15'];
$t16 = $_REQUEST['t16'];
$t17 = $_REQUEST['t17'];
$t18 = $_REQUEST['t18'];
$t19 = $_REQUEST['t19'];
$t20 = $_REQUEST['t20'];
$t21 = $_REQUEST['t21'];
$t22 = $_REQUEST['t22'];
$t23 = $_REQUEST['t23'];
$t24 = $_REQUEST['t24'];
$t25 = $_REQUEST['t25'];
$t26 = $_REQUEST['t26'];
$t27 = $_REQUEST['t27'];
$t28 = $_REQUEST['t28'];
$t29 = $_REQUEST['t29'];
$t30 = $_REQUEST['t30'];
$t31 = $_REQUEST['t31'];
$t32 = $_REQUEST['t32'];
$t33 = $_REQUEST['t33'];
$t34 = $_REQUEST['t34'];
$t35 = $_REQUEST['t35'];
$t36 = $_REQUEST['t36'];
$t37 = $_REQUEST['t37'];
$t38 = $_REQUEST['t38'];
$sql="UPDATE slambook SET iam = '$t1' ,pals = '$t2',birthday = '$t3' ,mobile = '$t4' ,zodiac ='$t5' ,address = '$t6',email ='$t7',site = '$t8',id = '$t9' ,hang = '$t10',food = '$t11',colour = '$t13',outfit = '$t14',novel = '$t15',singer = '$t16',song = '$t17',movie = '$t18',actor = '$t19',aim = '$t20',memor = '$t21',you = '$t22',date = '$t23',city = '$t24',frienship = '$t25',nuts = '$t26',besties = '$t27',destiny = '$t28',y = '$t29',moment = '$t30',achieve = '$t31',dream = '$t32',experience = '$t33',imagination = '$t34',regrets = '$t35',stupid = '$t36',belief = '$37',about = '$38' where reg=$reg";
$retval = mysqli_query($con,$sql);
if(!$retval )
{
die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
mysqli_close($con);
}
else
{
?>
I have a PHP script that I am trying to use to pull form data from a html document, process it and then write the data to a mysql database table.
I can get the script to write to the mysql database ok but it is only passing empty strings, I have declared variables as empty strings at the start of the script, these are then passed $_POST data from the form elements, but it appears not to be working, I have tried every which way of quotation and concatenation without success.
When I echo the variables they are there but just empty, heres the codes, its quite a long form but most of the variables are processed the same way, any help would be greatly appreciated, I'm pretty new to PHP.
<?PHP
//Create null variables
$datereportId = "";
$timereportId = "";
$nameinvolvedId = "";
$maleinvolvedId = "";
$femaleinvolvedId = "";
$addressinvolvedId = "";
$telinvolvedId = "";
$deptinvolvedId = "";
$jobinvolvedId = "";
$supinvolvedId = "";
$cjemployId = "";
$othemployId = "";
$contractId = "";
$visitId = "";
$paxId = "";
$publicId = "";
$apronId = "";
$terminalId = "";
$hangarId = "";
$terminalId = "";
$othlocationId = "";
$captainId = "";
$csId = "";
$aircraftId = "";
$fltnoId = "";
$seatnoId = "";
$sectorId = "";
$accbyId = "";
$relationId = "";
$dateaccId = "";
$timeaccId = "";
$weathercondId = "";
$weathercommentsId = "";
$normdutyId = "";
$normdutnId = "";
$areacleanyId = "";
$areacleannId = "";
$heatyId = "";
$heatnId = "";
$ppeyId = "";
$ppenId = "";
$equipyId = "";
$equipnId = "";
$commentsId = "";
$accna1Id = "";
$accfi2Id = "";
$accex3Id = "";
$accel4Id = "";
$accco5Id = "";
$accob6Id = "";
$accfa7Id = "";
$accsl8Id = "";
$accma9Id = "";
$accpl10Id = "";
$accst11Id = "";
$accve12Id = "";
$accph13Id = "";
$accsh14Id = "";
$accth15Id = "";
$accot16Id = "";
$acccomments17Id = "";
$injna1Id = "";
$injab2Id = "";
$injla3Id = "";
$injbu4Id = "";
$injbr5Id = "";
$injfa6Id = "";
$injir7Id = "";
$injbl8Id = "";
$injfr9Id = "";
$injin10Id = "";
$injre11Id = "";
$injcr12Id = "";
$injel13Id = "";
$injco14Id = "";
$injsh15Id = "";
$injot16Id = "";
$injcomments17Id = "";
$bodna1Id = "";
$bodab2Id = "";
$bodba3Id = "";
$bodch4Id = "";
$bodhe5Id = "";
$bodey6Id = "";
$bodea7Id = "";
$bodno8Id = "";
$bodte9Id = "";
$bodha10Id = "";
$bodar11Id = "";
$bodle12Id = "";
$bodkn13Id = "";
$bodan14Id = "";
$bodfo15Id = "";
$bodot16Id = "";
$bodcomments17Id = "";
$faidyId = "";
$faidnId = "";
$faidy_nameId = "";
$fkityId = "";
$fkitnId = "";
$fkity_nameId = "";
$doctyId = "";
$doctnId = "";
$docty_commentId = "";
$hospyId = "";
$hospnId = "";
$hospy_commentId = "";
$medic_commentId = "";
$wit1nameId = "";
$wit1addrId = "";
$wit1telId = "";
$wit2nameId = "";
$wit2addrId = "";
$wit2telId = "";
$wit3nameId = "";
$wit3addrId = "";
$wit3telId = "";
$submitbyId = "";
$submitbyemailId = "";
$submitbytitleId = "";
$submitbydeptId = "";
$sexinvolvedId = "";
$orginvolved = "";
$location = "";
$normalduty = "";
$areaclean = "";
$heating = "";
$ppeworn = "";
$equipcheck = "";
$accnature = "";
$injnature = "";
$bodnature = "";
$firstaid = "";
$fakused = "";
$doctor = "";
$hospreq = "";
//If form is submitted populate data
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
//Populate variables with sanitised data variables
//$datereportId = test_input($_POST["id_datereport"]);
//$timereportId = test_input($_POST["id_timereport"]);
$nameinvolvedId = test_input($_POST["id_nameinvolved"]);
$maleinvolvedId = test_input($_POST["id_maleinvolved"]);
$femaleinvolvedId = test_input($_POST["id_femaleinvolved"]);
$addressinvolvedId = test_input($_POST["id_addressinvolved"]);
$telinvolvedId = test_input($_POST["id_telinvolved"]);
$deptinvolvedId = test_input($_POST["id_deptinvolved"]);
$jobinvolvedId = test_input($_POST["id_jobinvolved"]);
$supinvolvedId = test_input($_POST["id_supinvolved"]);
$cjemployId = test_input($_POST["id_cjemploy"]);
$othemployId = test_input($_POST["id_othemploy"]);
$contractId = test_input($_POST["id_contract"]);
$visitId = test_input($_POST["id_visit"]);
$paxId = test_input($_POST["id_pax"]);
$publicId = test_input($_POST["id_public"]);
$apronId = test_input($_POST["id_apron"]);
$terminalId = test_input($_POST["id_terminal"]);
$hangarId = test_input($_POST["id_hangar"]);
$terminalId = test_input($_POST["id_terminal"]);
$othlocationId = test_input($_POST["id_othlocation"]);
$captainId = test_input($_POST["id_captain"]);
$csId = test_input($_POST["id_cs"]);
$aircraftId = test_input($_POST["id_aircraft"]);
$fltnoId = test_input($_POST["id_fltno"]);
$seatnoId = test_input($_POST["id_seatno"]);
$sectorId = test_input($_POST["id_sector"]);
$accbyId = test_input($_POST["id_accby"]);
$relationId = test_input($_POST["id_relation"]);
//$dateaccId = test_input($_POST["id_dateacc"]);
//$timeaccId = test_input($_POST["id_timeacc"]);
$weathercondId = test_input($_POST["id_weathercond"]);
$weathercommentsId = test_input($_POST["id_weathercomments"]);
$normdutyId = test_input($_POST["id_normduty"]);
$normdutnId = test_input($_POST["id_normdutn"]);
$areacleanyId = test_input($_POST["id_areacleany"]);
$areacleannId = test_input($_POST["id_areacleann"]);
$heatyId = test_input($_POST["id_heaty"]);
$heatnId = test_input($_POST["id_heatn"]);
$ppeyId = test_input($_POST["id_ppey"]);
$ppenId = test_input($_POST["id_ppen"]);
$equipyId = test_input($_POST["id_equipy"]);
$equipnId = test_input($_POST["id_equipn"]);
$equipcommentsId = test_input($_POST["id_equipcomments"]);
$accna1Id = test_input($_POST["id_accna1"]);
$accfi2Id = test_input($_POST["id_accfi2"]);
$accex3Id = test_input($_POST["id_accex3"]);
$accel4Id = test_input($_POST["id_accel4"]);
$accco5Id = test_input($_POST["id_accco5"]);
$accob6Id = test_input($_POST["id_accob6"]);
$accfa7Id = test_input($_POST["id_accfa7"]);
$accsl8Id = test_input($_POST["id_accsl8"]);
$accma9Id = test_input($_POST["id_accma9"]);
$accpl10Id = test_input($_POST["id_accpl10"]);
$accst11Id = test_input($_POST["id_accst11"]);
$accve12Id = test_input($_POST["id_accve12"]);
$accph13Id = test_input($_POST["id_accph13"]);
$accsh14Id = test_input($_POST["id_accsh14"]);
$accth15Id = test_input($_POST["id_accth15"]);
$accot16Id = test_input($_POST["id_accot16"]);
$acccomments17Id = test_input($_POST["id_acccomments17"]);
$injna1Id = test_input($_POST["id_injna1"]);
$injab2Id = test_input($_POST["id_injab2"]);
$injla3Id = test_input($_POST["id_injla3"]);
$injbu4Id = test_input($_POST["id_injbu4"]);
$injbr5Id = test_input($_POST["id_injbr5"]);
$injfa6Id = test_input($_POST["id_injfa6"]);
$injir7Id = test_input($_POST["id_injir7"]);
$injbl8Id = test_input($_POST["id_injbl8"]);
$injfr9Id = test_input($_POST["id_injfr9"]);
$injin10Id = test_input($_POST["id_injin10"]);
$injre11Id = test_input($_POST["id_injre11"]);
$injcr12Id = test_input($_POST["id_injcr12"]);
$injel13Id = test_input($_POST["id_injel13"]);
$injco14Id = test_input($_POST["id_injco14"]);
$injsh15Id = test_input($_POST["id_injsh15"]);
$injot16Id = test_input($_POST["id_injot16"]);
$injcomments17Id = test_input($_POST["id_injcomments17"]);
$bodna1Id = test_input($_POST["id_bodna1"]);
$bodab2Id = test_input($_POST["id_bodab2"]);
$bodba3Id = test_input($_POST["id_bodba3"]);
$bodch4Id = test_input($_POST["id_bodch4"]);
$bodhe5Id = test_input($_POST["id_bodhe5"]);
$bodey6Id = test_input($_POST["id_bodey6"]);
$bodea7Id = test_input($_POST["id_bodea7"]);
$bodno8Id = test_input($_POST["id_bodno8"]);
$bodte9Id = test_input($_POST["id_bodte9"]);
$bodha10Id = test_input($_POST["id_bodha10"]);
$bodar11Id = test_input($_POST["id_bodar11"]);
$bodle12Id = test_input($_POST["id_bodle12"]);
$bodkn13Id = test_input($_POST["id_bodkn13"]);
$bodan14Id = test_input($_POST["id_bodan14"]);
$bodfo15Id = test_input($_POST["id_bodfo15"]);
$bodot16Id = test_input($_POST["id_bodot16"]);
$bodcomments17Id = test_input($_POST["id_bodcomments17"]);
$faidyId = test_input($_POST["id_faidy"]);
$faidnId = test_input($_POST["id_faidn"]);
$faidy_nameId = test_input($_POST["id_faidy_name"]);
$fkityId = test_input($_POST["id_fkity"]);
$fkitnId = test_input($_POST["id_fkitn"]);
$fkity_nameId = test_input($_POST["id_fkity_name"]);
$doctyId = test_input($_POST["id_docty"]);
$doctnId = test_input($_POST["id_doctn"]);
$docty_commentId = test_input($_POST["id_docty_comment"]);
$hospyId = test_input($_POST["id_hospy"]);
$hospnId = test_input($_POST["id_hospn"]);
$hospy_commentId = test_input($_POST["id_hospy_comment"]);
$medic_commentId = test_input($_POST["id_medic_comment"]);
$wit1nameId = test_input($_POST["id_wit1name"]);
$wit1addrId = test_input($_POST["id_wit1addr"]);
$wit1telId = test_input($_POST["id_wit1tel"]);
$wit2nameId = test_input($_POST["id_wit2name"]);
$wit2addrId = test_input($_POST["id_wit2addr"]);
$wit2telId = test_input($_POST["id_wit2tel"]);
$wit3nameId = test_input($_POST["id_wit3name"]);
$wit3addrId = test_input($_POST["id_wit3addr"]);
$wit3telId = test_input($_POST["id_wit3tel"]);
//$submitbyId = test_input($_POST["id_submitby"]);
//$submitbyemailId = test_input($_POST["id_submitbyemail"]);
//$submitbytitleId = test_input($_POST["id_submitbytitle"]);
//$submitbydeptId = test_input($_POST["id_submitbydept"]);
$sexinvolvedId = $maleinvolvedId . $femaleinvolvedId;
$orginvolved = $cjemployId . $othemployId . $contractId . $visitId . $paxId . $publicId;
$location = $apronId . $terminalId . $hangarId . $terminalId . $othlocationId;
$normalduty = $normdutyId . $normdutnId;
$areaclean = $areacleanyId . $areacleannId;
$heating = $heatyId . $heatnId;
$ppeworn = $ppeyId . $ppeyId;
$equipcheck = $equipyId . $equipnId;
$accnature = $accna1Id . $accfi2Id . $accex3Id . $accel4Id . $accco5Id . $accob6Id . $accfa7Id . $accsl8Id . $accma9Id . $accpl10Id . $accst11Id . $accve12Id . $accph13Id . $accsh14Id . $accth15Id . $accot16Id;
$injnature = $injna1Id . $injab2Id . $injla3Id . $injbu4Id . $injbr5Id . $injfa6Id . $injir7Id . $injbl8Id . $injfr9Id . $injin10Id . $injre11Id . $injcr12Id . $injel13Id . $injco14Id . $injsh15Id . $injot16Id;
$bodnature = $bodna1Id . $bodab2Id . $bodba3Id . $bodch4Id . $bodey6Id . $bodea7Id . $bodno8Id . $bodte9Id . $bodha10Id . $bodar11Id . $bodle12Id . $bodkn13Id . $bodan14Id . $bodfo15Id . $bodot16Id;
$firstaid = $faidyId . $faidnId;
$fakused = $fkityId . $fkitnId;
$doctor = $doctyId . $doctnId;
$hospreq = $hospyId . $hospnId;
}
//Sanitise all variables
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
//Mandatory variable handling, define variables and set to empty values
$nameErr = $emailErr = $posErr = $depErr = $dateErr = $timeErr = $dateErr1 = $timeErr1 = "";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if (empty($_POST["id_submitby"]))
{$nameErr = "Name is required";}
else
{$submitbyId = test_input($_POST["id_submitby"]);}
if (empty($_POST["id_submitbyemail"]))
{$emailErr = "Email is required";}
else
{$submitbyemailId = test_input($_POST["id_submitbyemail"]);}
if (empty($_POST["id_submitbytitle"]))
{$posErr = "Job title is required";}
else
{$submitbytitleId = test_input($_POST["id_submitbytitle"]);}
if (empty($_POST["id_submitbydept"]))
{$depErr = "Department is required";}
else
{$submitbydeptId = test_input($_POST["id_submitbydept"]);}
if (empty($_POST["id_datereport"]))
{$dateErr = "Date is required";}
else
{$datereportId = test_input($_POST["id_datereport"]);}
if (empty($_POST["id_timereport"]))
{$timeErr = "Time is required";}
else
{$datereportId = test_input($_POST["id_datereport"]);}
if (empty($_POST["id_dateacc"]))
{$dateErr1 = "Date is required";}
else
{$datereportId = test_input($_POST["id_dateacc"]);}
if (empty($_POST["id_timeacc"]))
{$timeErr1 = "Time is required";}
else
{$datereportId = test_input($_POST["id_timeacc"]);}
}
$email = test_input($_POST["id_submitbyemail"]);
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/",$email))
{
$emailErr1 = "Invalid email format";
}
spamcheck($_POST["id_submitbyemail"]);
function spamcheck($field)
{
//filter_var() sanitizes the e-mail
//address using FILTER_SANITIZE_EMAIL
$field=filter_var($field, FILTER_SANITIZE_EMAIL);
//filter_var() validates the e-mail
//address using FILTER_VALIDATE_EMAIL
if(filter_var($field, FILTER_VALIDATE_EMAIL))
{
$submitbyemailId_OK = $field;
return $submitbyemailId_OK;
}
else
{
$submitbyemailId_OK = "Spam email error";
return $submitbyemailId_OK;
}
}
//Check for null or empty variables and assign mysql value
function checkNull($var)
{
if($var == '')
{
$var = '--';
}
else
{
$var = $var;
}
}
//Apply checkNull to variables
checkNull($datereportId);
checkNull($timereportId);
checkNull($nameinvolvedId);
checkNull($addressinvolvedId);
checkNull($telinvolvedId);
checkNull($deptinvolvedId);
checkNull($jobinvolvedId);
checkNull($supinvolvedId);
checkNull($othlocationId);
checkNull($captainId);
checkNull($csId);
checkNull($aircraftId);
checkNull($fltnoId);
checkNull($seatnoId);
checkNull($sectorId);
checkNull($accbyId);
checkNull($relationId);
checkNull($dateaccId);
checkNull($timeaccId);
checkNull($weathercondId);
checkNull($weathercommentsId);
checkNull($equipcommentsId);
checkNull($acccomments17Id);
checkNull($injcomments17Id);
checkNull($bodcomments17Id);
checkNull($faidy_nameId);
checkNull($fkity_nameId);
checkNull($docty_commentId);
checkNull($hospy_commentId);
checkNull($medic_commentId);
checkNull($wit1nameId);
checkNull($wit1addrId);
checkNull($wit1telId);
checkNull($wit2nameId);
checkNull($wit2addrId);
checkNull($wit2telId);
checkNull($wit3nameId);
checkNull($wit3addrId);
checkNull($wit3telId);
checkNull($submitbyId);
checkNull($submitbyemailId);
checkNull($submitbytitleId);
checkNull($submitbydeptId);
checkNull($sexinvolvedId);
checkNull($orginvolvedId);
checkNull($locationId);
checkNull($normaldutyId);
checkNull($areacleanId);
checkNull($heatingId);
checkNull($ppewornId);
checkNull($equipcheckId);
checkNull($accnatureId);
checkNull($injnatureId);
checkNull($bodnatureId);
checkNull($firstaId);
checkNull($fakusedId);
checkNull($doctorId);
checkNull($hospreqId);
//Create mysql connection
$host = '';
$id = 'u1282431_skelly';
$pwd = '';
$db = 'db1282431_healthsafety';
$connection = #mysqli_connect($host, $id, $pwd, $db);
//Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect" . mysqli_connect_error();
}
//Insert entries from form into table
$sql="INSERT INTO healthsafety (datereport, timereport, nameinvolved, sexinvolved, addressinvolved, telinvolved, deptinvolved, jobinvolved, supinvolved, orginvolved, location, othlocation, captain, cs, aircraft, fltno, seatno, sector, accby, relation, dateacc, timeacc, weathercond, weathercomments, normalduty, areaclean, heating, ppeworn, equipcheck, equipcheckcomments, accnature, accother, injnature, injother, bodnature, bodother, firstaid, firstaidby, fakused, fakusedby, doctor, doctorinfo, hospreq, hospinfo, medcomment, wit1name, wit1addr, wit1tel, wit2name, wit2addr, wit2tel, wit3name, wit3addr, wit3tel, submitbyname, submitbyemail, submitbytitle, submitbydept)
VALUES
('$_POST[id_datereport]', '$timereportId', '$nameinvolvedId', '$sexinvolvedId', '$addressinvolvedId', '$telinvolvedId', '$deptinvolvedId', '$jobinvolvedId', '$supinvolvedId', '$orginvolvedId', '$locationId', '$othlocationId', '$captainId', '$csId', '$aircraftId', '$fltnoId', '$seatnoId', '$sectorId', '$accbyId', '$relationId', '$dateaccId', '$timeaccId', '$weathercondId', '$weathercommentsId', '$normaldutyId', '$areacleanId', '$heatingId', '$ppewornId', '$equipcheckId', '$commentsId', '$accnatureId', '$acccomments17Id', '$injnatureId', '$injcomments17Id', '$bodnatureId', '$bodcomments17Id', '$firstaid', '$faidy_nameId', '$fakusedId', '$fkity_nameId', '$doctorId', '$docty_commentId', '$hospreqId', '$hospy_commentId', '$medic_commentId', '$wit1nameId', '$wit1addrId', '$wit1telId', '$wit2nameId', '$wit2addrId', '$wit2telId', '$wit3nameId', '$wit3addrId', '$wit3telId', '$submitbyId', '$submitbyemailId', '$submitbytitleId', '$submitbydeptId');";
//Entry success or failure message
if (!mysqli_query($connection,$sql))
{
//Error message if entry fails
die('Error: ' . mysqli_error($connection));
}
else
{
//output success html page if entries were successfully added:
echo "Entries added";
}
?>
I will try to give you the main concept and you can then apply it to your case. Say you have a form like this:
<form action="path/to/handler/file.php" method="post">
Name: <input type="text" name="name">
Email: <input type="text" name="email">
Phone: <input type="text" name="phone">
<input type="submit" name="submit" value="Submit">
</form>
Now in the handler file:
if (!empty($_POST)) {
foreach ($_POST as $key => $value) {
${$key} = htmlspecialchars(trim($value), ENT_QUOTES, 'UTF-8');
}
}
This snippet of code will give you the next:
// supposing that the next values are those the user entered
$name = "john doe";
$email = "john.doe#gmail.com";
$phone = "55555555";
Now you can deal with these variables ($name, $email and $phone) to do whatever you want, like to check if a variable is empty or not, check if it is a valid email or not, check if it is an number or not, etc... and then save them in the database.
try using something like this, to get the propper contents:
function give_get($varname)
{
if (isset($_GET[$varname])) return trim($_GET[$varname]);
else return "";
}
function give_post($varname)
{
if (isset($_POST[$varname])) return trim($_POST[$varname]);
else return "";
}