I write some big script, which have tajemniczy_users array. I need update data in this array. I have an object users and method updateUser()
public function updateUser() {
$this->sth = $this->db->prepare("UPDATE tajemniczy_users SET username = :username, password = :password, uid = :uid, imie = :imie, nazwisko = :nazwisko, email = :email, pesel = :pesel, ulica = :ulica, kodpocztowy = :kodpocztowy, miejscowosc = :miejscowosc, wojewodztwo = :wojewodztwo, telefon1 = :telefon1, telefon2 = :telefon2, gg = :gg, skype = :skype, wyksztalcenie = :wyksztalcenie, zatrudnienie = :zatrudnienie, plec = :plec, stancywilny = :stancywilny, dzieci = :dzieci, internet = :internet, dostep = :dostep, fax = :fax, skaner = :skaner, aparat = :aparat, dyktafon = :dyktafon, miasta = :miasta, banki = :banki, d12_16 = :d12_16, d16_20 = :d16_20, d8_12 = :d8_12, brakd = :brakd, sklepy = :sklepy, hipermarkety = :hipermarkety, prowadzeniesamochodu = :prowadzeniesamochodu, wlasnysamochod = :wlasnysamochod, tajemniczy = :tajemniczy, uwagi = :uwagi WHERE id = :id;");
$this->sth->bindValue(':username' , $this->username , PDO::PARAM_STR);
$this->sth->bindValue(':password' , $this->password , PDO::PARAM_STR);
$this->sth->bindValue(':uid' , md5(rand()) , PDO::PARAM_STR);
$this->sth->bindValue(':imie' , $this->imie , PDO::PARAM_STR);
$this->sth->bindValue(':nazwisko' , $this->nazwisko , PDO::PARAM_STR);
$this->sth->bindValue(':email' , $this->email , PDO::PARAM_STR);
$this->sth->bindValue(':pesel' , $this->pesel , PDO::PARAM_STR);
$this->sth->bindValue(':ulica' , $this->ulica , PDO::PARAM_STR);
$this->sth->bindValue(':kodpocztowy' , $this->kodpocztowy , PDO::PARAM_STR);
$this->sth->bindValue(':miejscowosc' , $this->miejscowosc , PDO::PARAM_STR);
$this->sth->bindValue(':wojewodztwo' , $this->wojewodztwo , PDO::PARAM_STR);
$this->sth->bindValue(':telefon1' , $this->telefon1 , PDO::PARAM_STR);
$this->sth->bindValue(':telefon2' , $this->telefon2 , PDO::PARAM_STR);
$this->sth->bindValue(':gg' , $this->gg , PDO::PARAM_STR);
$this->sth->bindValue(':skype' , $this->skype , PDO::PARAM_STR);
$this->sth->bindValue(':wyksztalcenie' , $this->wyksztalcenie , PDO::PARAM_STR);
$this->sth->bindValue(':zatrudnienie' , $this->zatrudnienie , PDO::PARAM_STR);
$this->sth->bindValue(':plec' , $this->plec , PDO::PARAM_STR);
$this->sth->bindValue(':stancywilny' , $this->stancywilny , PDO::PARAM_STR);
$this->sth->bindValue(':dzieci' , $this->dzieci , PDO::PARAM_INT);
$this->sth->bindValue(':internet' , $this->internet , PDO::PARAM_INT);
$this->sth->bindValue(':dostep' , $this->dostep , PDO::PARAM_STR);
$this->sth->bindValue(':fax' , $this->fax , PDO::PARAM_INT);
$this->sth->bindValue(':skaner' , $this->skaner , PDO::PARAM_INT);
$this->sth->bindValue(':aparat' , $this->aparat , PDO::PARAM_INT);
$this->sth->bindValue(':dyktafon' , $this->dyktafon , PDO::PARAM_INT);
$this->sth->bindValue(':miasta' , $this->miasta , PDO::PARAM_STR);
$this->sth->bindValue(':banki' , $this->banki , PDO::PARAM_STR);
$this->sth->bindValue(':d12_16' , $this->d12_16 , PDO::PARAM_INT);
$this->sth->bindValue(':d16_20' , $this->d16_20 , PDO::PARAM_INT);
$this->sth->bindValue(':d8_12' , $this->d8_12 , PDO::PARAM_INT);
$this->sth->bindValue(':brakd' , $this->brakd , PDO::PARAM_STR);
$this->sth->bindValue(':sklepy' , $this->sklepy , PDO::PARAM_STR);
$this->sth->bindValue(':hipermarkety' , $this->hipermarkety , PDO::PARAM_STR);
$this->sth->bindValue(':prowadzeniesamochodu' , $this->prowadzeniesamochodu , PDO::PARAM_STR);
$this->sth->bindValue(':wlasnysamochod' , $this->wlasnysamochod , PDO::PARAM_INT);
$this->sth->bindValue(':tajemniczy' , $this->tajemniczy , PDO::PARAM_STR);
$this->sth->bindValue(':uwagi' , $this->uwagi , PDO::PARAM_STR);
$this->sth->bindValue(':id' , $this->id , PDO::PARAM_INT);
$this->sth->execute();
}
Unfortunately this query doesn't update data.
print_r($this->sth) after all bindValues return
PDOStatement Object ( [queryString] => UPDATE tajemniczy_users SET username = :username, password = :password, uid = :uid, imie = :imie, nazwisko = :nazwisko, email = :email, pesel = :pesel, ulica = :ulica, kodpocztowy = :kodpocztowy, miejscowosc = :miejscowosc, wojewodztwo = :wojewodztwo, telefon1 = :telefon1, telefon2 = :telefon2, gg = :gg, skype = :skype, wyksztalcenie = :wyksztalcenie, zatrudnienie = :zatrudnienie, plec = :plec, stancywilny = :stancywilny, dzieci = :dzieci, internet = :internet, dostep = :dostep, fax = :fax, skaner = :skaner, aparat = :aparat, dyktafon = :dyktafon, miasta = :miasta, banki = :banki, d12_16 = :d12_16, d16_20 = :d16_20, d8_12 = :d8_12, brakd = :brakd, sklepy = :sklepy, hipermarkety = :hipermarkety, prowadzeniesamochodu = :prowadzeniesamochodu, wlasnysamochod = :wlasnysamochod, tajemniczy = :tajemniczy, uwagi = :uwagi WHERE id = :id; )
I checked names, variables, types of data etc. I have no idea. Can you help me?
edit:
bindValue return true or false, so I tried echo that values, but all of them are TRUE.
Table: http://pastebin.com/DQybkZE0
Related
Im trying to create a new row in the table 'Colaboradores' but it doesn't populate, but when i 'echo' the '$sql' with works fine along with the connection. I already check the name of the columns in the sql table. Im using MAMP as a sever
<?php
include("../../config.php");
session_start();
if($_SERVER["REQUEST_METHOD"] === "POST") {
$nomeF = $_POST['nomeF'];
$nomeL = $_POST['nomeL'];
$Prof = $_POST['Profissao'];
$morada = $_POST['morada'];
$cod = $_POST['cod'];
$num = $_POST['num'];
$mail = $_POST['mail'];
$ordeb = $_POST['ordb'];
$orde = $_POST['orde'];
$dataI = $_POST['dataI'];
$dataF = $_POST['dataF'];
$notas1 = $_POST['notas1'];
$notas2 = $_POST['notas2'];
try
{
$db = new PDO('mysql:host=localhost;dbname=SCMMM;charset=utf8', 'root', 'root');
}
catch(Exception $e)
{
die('Error : '.$e->getMessage());
}
$sql = "INSERT INTO Colaboradores (NomeF, NomeL, Profissao, Morada, CodPostal, Telemovel, mail, precoh, precohmais, dataI, dataF, notas1, notas2)
VALUES (:nomeF, :nomeL, :Prof, :morada, :cod, :num, :mail, :ordeb, :orde, :dataI, :dataF, :notas1, :notas2)";
$stmt = $db->prepare($sql);
$stmt->bindValue('nomeF', $nomeF, PDO::PARAM_STR);
$stmt->bindValue('nomeL', $nomeL, PDO::PARAM_STR);
$stmt->bindValue('Prof', $Prof, PDO::PARAM_STR);
$stmt->bindValue('morada', $morada, PDO::PARAM_STR);
$stmt->bindValue('cod', $cod, PDO::PARAM_STR);
$stmt->bindValue('num', $num, PDO::PARAM_INT);
$stmt->bindValue('mail', $mail, PDO::PARAM_STR);
$stmt->bindValue('ordb', $ordeb, PDO::PARAM_INT);
$stmt->bindValue('orde', $orde, PDO::PARAM_INT);
$stmt->bindValue('dataI', $dataI, PDO::PARAM_STR);
$stmt->bindValue('dataF', $dataF, PDO::PARAM_STR);
$stmt->bindValue('notas1', $notas1, PDO::PARAM_STR);
$stmt->bindValue('notas2', $notas2, PDO::PARAM_STR);
$stmt->execute();
}
?>
You can easily improve your code :
Avoid symbol as ã or + in database
Avoid space in database (replace by _)
Inform yourself about OOP and PDO
Inform yourself about SQL injection, Prepare query, ...
Use a convention for your variables names, lower camelcase ? upper camelcase ? whatever but stay regular
Now try with this code
$nomeF = $_POST['nomeF'];
$nomeL = $_POST['nomeL'];
$descP = $_POST['descP'];
$morada = $_POST['morada'];
$num = $_POST['num'];
$mail = $_POST['mail'];
$dataI = $_POST['dataI'];
$dataF = $_POST['dataF'];
$ordeb = $_POST['ordeb'];
$orde = $_POST['orde'];
$notas1 = $_POST['notas1'];
$notas2 = $_POST['notas2'];
try
{
$db = new PDO('mysql:host=localhost;dbname=DBNAME;charset=utf8', 'USERNAME', 'PASSWORD');
}
catch(Exception $e)
{
die('Erreur : '.$e->getMessage());
}
$sql = "INSERT INTO Colaboradores (nomeF, nomeL, descP, morada, mail, ordeb, orde, dataI, dataF, notas1, notas2)
VALUES (:nomeF, :nomeL, :descP, :morada, :num, :mail, :ordeb, :orde, :dataI, :dataF, :notas1, :notas2)";
$stmt = $db->prepare($sql);
$stmt->bindValue('nomeF', $nomeF, PDO::PARAM_STR);
$stmt->bindValue('nomeL', $nomeL, PDO::PARAM_STR);
$stmt->bindValue('descP', $descP, PDO::PARAM_STR);
$stmt->bindValue('morada', $morada, PDO::PARAM_STR);
$stmt->bindValue('num', $num, PDO::PARAM_INT);
$stmt->bindValue('mail', $mail, PDO::PARAM_STR);
$stmt->bindValue('ordeb', $ordeb, PDO::PARAM_STR);
$stmt->bindValue('dataI', $dataI, PDO::PARAM_STR);
$stmt->bindValue('dataF', $dataF, PDO::PARAM_STR);
$stmt->bindValue('notas1', $notas1, PDO::PARAM_STR);
$stmt->bindValue('notas2', $notas2, PDO::PARAM_STR);
$stmt->execute();
EDIT
I build your project on my computer, try to add
$error = $stmt->errorInfo();
print_r($error);
To see what's happen during your request.
On my side, I found a mismatch with the word ordeb and ordb
For example : $stmt->bindValue('ordb', $ordeb, PDO::PARAM_INT);
And can you check also the format of your date, it should be "Y-m-d H:i:s")
Note : All your columns in your table are of text type, text should be used only for long text (like in textarea), you should use varchar which allow you to save up to 255 characters (enough).
try
{
$stmt = $conn->prepare(" Update site_users SET users_first_name = :users_first_name, users_last_name = users_last_name
,users_email_verified = :users_email_verified , users_password = :users_password, users_dob_day = :users_dob_day, users_dob_month = :users_dob_month
,users_dob_year = :users_dob_year, users_password_1 = :users_password_1, users_registration_time= :users_registration_time
WHERE users_email = :users_email");
$stmt->bindParam(':users_first_name', $_POST["firstname"] , PDO::PARAM_STR);
$stmt->bindParam(':users_last_name', $_POST["lastname"] , PDO::PARAM_STR);
$stmt->bindParam(':users_email', $_POST["email"] , PDO::PARAM_STR);
$stmt->bindParam(':users_password', $hashed_password , PDO::PARAM_STR);
$stmt->bindParam(':users_password_1', $_POST["password"] , PDO::PARAM_STR);
$stmt->bindParam(':users_email_verified', $users_email_verified , PDO::PARAM_STR);
$stmt->bindParam(':users_dob_day', $_POST["day"], PDO::PARAM_STR);
$stmt->bindParam(':users_dob_month', $_POST["month"], PDO::PARAM_STR);
$stmt->bindParam(':users_dob_year', $_POST["year"], PDO::PARAM_STR);
$stmt->bindParam(':users_registration_time',$date, PDO::PARAM_STR);
$stmt->execute();
}
catch(PDOException $e)
{
echo "Error: " . $e->getMessage();
}
This is the error, the code echo
Error: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens
Where is the problem in this code?
i think you are missing the : in your users_last_name = users_last_name query
probably it has to be users_last_name = :users_last_name
I get this error message on line 242:
Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens on line 242
PDOException: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined on line 242
global $db, $id;
$malattiaPastNome = $_POST['malattia_past_nome'];
$malattiaPastDataInizio = $_POST['malattia_past_data_inizio'];
$malattiaPastDataFine = $_POST['malattia_past_data_fine'];
$malattiaPresentiNome = $_POST['malattia_presenti_nome'];
$interventiTraumi = $_REQUEST['interventi_traumi'];
$interventiTraumiAltro = $_POST['interventi_traumi_altro'];
$interventiTraumiData = $_POST['interventi_traumi_data'];
$sintomiAttualiNome = $_REQUEST['sintomi_attuali_nome'];
$sintomiAttualiNomeAltro = $_POST['sintomi_attuali_nome_altro'];
$sintomiAttualiFrequenza = $_REQUEST['sintomi_attuali_frequenza'];
$sintomiAttualiDurata = $_REQUEST['sintomi_attuali_durata'];
$sintomiAttualiIntensita = $_REQUEST['sintomi_attuali_intensita'];
$alimQuotidColazione = $_REQUEST['alim_quotid_colazione'];
$alimQuotidColazioneBevande = $_REQUEST['alim_quotid_colazione_bevande'];
$alimQuotidColazioneAlimenti = $_REQUEST['alim_quotid_colazione_alimenti'];
$alimQuotidMerenda = $_REQUEST['alim_quotid_merenda'];
$alimQuotidMerendaBevande = $_REQUEST['alim_quotid_merenda_bevande'];
$alimQuotidMerendaAlimenti = $_REQUEST['alim_quotid_merenda_alimenti'];
$alimQuotidPranzo = $_REQUEST['alim_quotid_pranzo'];
$alimQuotidPranzoBevande = $_REQUEST['alim_quotid_pranzo_bevande'];
$alimQuotidPranzoAlimenti = $_REQUEST['alim_quotid_pranzo_alimenti'];
$alimQuotidPomeridiano = $_REQUEST['alim_quotid_pomeridiano'];
$alimQuotidPomeridianoBevande = $_REQUEST['alim_quotid_pomeridiano_bevande'];
$alimQuotidPomeridianoAlimenti = $_REQUEST['alim_quotid_pomeridiano_alimenti'];
$alimQuotidCena = $_REQUEST['alim_quotid_cena'];
$alimQuotidCenaBevande = $_REQUEST['alim_quotid_cena_bevande'];
$alimQuotidCenaAlimenti = $_POST['alim_quotid_cena_alimenti'];
$sql = 'UPDATE utenti2 SET malattia_past_nome=:past_nome, malattia_past_data_inizio=:past_dataInizio, malattia_past_data_fine=:past_data_fine, malattia_presenti_nome=:presenti_nome, interventi_traumi=:interventi_traumi, interventi_traumi_altro=:traumi_altro, interventi_traumi_data=:traumi_data, sintomi_attuali_nome=:attuali_nome, sintomi_attuali_nome_altro=:attuali_nome_altro, sintomi_attuali_frequenza=:attuali_frequenza, sintomi_attuali_durata=:attuali_durata, sintomi_attuali_intensita=:attuali_intensita, alim_quotid_colazione=:colazione, alim_quotid_colazione_bevande=:colazioe_bevande, alim_quotid_colazione_alimenti=:colazione_alimenti, alim_quotid_merenda=:merenda, alim_quotid_merenda_bevande=:merenda_bevande, alim_quotid_merenda_alimenti=:merenda_alimenti, alim_quotid_pranzo=:_pranzo, alim_quotid_pranzo_bevande=:pranzo_bevande, alim_quotid_pranzo_alimenti=:pranzo_alimenti, alim_quotid_pomeridiano=:pomeridiano, alim_quotid_pomeridiano_bevande=:pomeridiano_bevande, alim_quotid_pomeridiano_alimenti=:pomeridiano_alimenti, alim_quotid_cena=:cena, alim_quotid_cena_bevande=:bevande, alim_quotid_cena_alimenti=:cena_alimenti WHERE id=:id LIMIT 1';
$result = $db->prepare($sql);
$result->bindValue(':id', $id, PDO::PARAM_INT);
$result->bindValue(':past_nome', $malattiaPastNome, PDO::PARAM_STR);
$result->bindValue(':past_dataInizio', $malattiaPastDataInizio, PDO::PARAM_STR);
$result->bindValue(':past_data_fine', $malattiaPastDataFine, PDO::PARAM_STR);
$result->bindValue(':presenti_nome', $malattiaPresentiNome, PDO::PARAM_STR);
$result->bindValue(':interventi_traumi', $interventiTraumi, PDO::PARAM_STR);
$result->bindValue(':traumi_altro', $interventiTraumiAltro, PDO::PARAM_STR);
$result->bindValue(':traumi_data', $interventiTraumiData, PDO::PARAM_STR);
$result->bindValue(':attuali_nome', $sintomiAttualiNome, PDO::PARAM_STR);
$result->bindValue(':attuali_nome_altro', $sintomiAttualiNomeAltro, PDO::PARAM_STR);
$result->bindValue(':attuali_frequenza', $sintomiAttualiFrequenza, PDO::PARAM_STR);
$result->bindValue(':attuali_durata', $sintomiAttualiDurata, PDO::PARAM_STR);
$result->bindValue(':attuali_intensita', $sintomiAttualiIntensita, PDO::PARAM_STR);
$result->bindValue(':colazione', $alimQuotidColazione, PDO::PARAM_STR);
$result->bindValue(':colazione_bevande', $alimQuotidColazioneBevande, PDO::PARAM_STR);
$result->bindValue(':colazione_alimenti', $alimQuotidColazioneAlimenti, PDO::PARAM_STR);
$result->bindValue(':merenda', $alimQuotidMerenda, PDO::PARAM_STR);
$result->bindValue(':merenda_bevande', $alimQuotidMerendaBevande, PDO::PARAM_STR);
$result->bindValue(':merenda_alimenti', $alimQuotidMerendaAlimenti, PDO::PARAM_STR);
$result->bindValue(':pranzo', $alimQuotidPranzo, PDO::PARAM_STR);
$result->bindValue(':pranzo_bevande', $alimQuotidPranzoBevande, PDO::PARAM_STR);
$result->bindValue(':pranzo_alimenti', $alimQuotidPranzoAlimenti, PDO::PARAM_STR);
$result->bindValue(':pomeridiano', $alimQuotidPomeridiano, PDO::PARAM_STR);
$result->bindValue(':pomeridiano_bevande', $alimQuotidPomeridianoBevande, PDO::PARAM_STR);
$result->bindValue(':pomeridiano_alimenti', $alimQuotidPomeridianoAlimenti, PDO::PARAM_STR);
$result->bindValue(':cena', $alimQuotidCena, PDO::PARAM_STR);
$result->bindValue(':cena_bevande', $alimQuotidCenaBevande, PDO::PARAM_STR);
$result->bindValue(':cena_alimenti', $alimQuotidCenaAlimenti, PDO::PARAM_STR);
$result->execute(); //line 242
I tried multiple times checking all my vars and code but can't find the error.
Thanks
You may have a typo in one of your bindParams, which means you have a mismatch in parameters.Please check and confirm
This query is not correct (or at least i don't know a possible way to use limit in UPDATE queries). So remove LIMIT 1 from the query and try again
You have used with name past_data_fine in query but while binding you used variable as past_dataFine . so
repalce your line:
$result->bindValue(':past_dataFine', $malattiaPastDataFine, PDO::PARAM_STR);
with
$result->bindValue(':past_data_fine', $malattiaPastDataFine, PDO::PARAM_STR);
Final version working:
global $db, $id;
$malattiaPastNome = $_POST['malattia_past_nome'];
$malattiaPastDataInizio = $_POST['malattia_past_data_inizio'];
$malattiaPastDataFine = $_POST['malattia_past_data_fine'];
$malattiaPresentiNome = $_POST['malattia_presenti_nome'];
$interventiTraumi = $_REQUEST['interventi_traumi'];
$interventiTraumiAltro = $_POST['interventi_traumi_altro'];
$interventiTraumiData = $_POST['interventi_traumi_data'];
$sintomiAttualiNome = $_REQUEST['sintomi_attuali_nome'];
$sintomiAttualiNomeAltro = $_POST['sintomi_attuali_nome_altro'];
$sintomiAttualiFrequenza = $_REQUEST['sintomi_attuali_frequenza'];
$sintomiAttualiDurata = $_REQUEST['sintomi_attuali_durata'];
$sintomiAttualiIntensita = $_REQUEST['sintomi_attuali_intensita'];
$alimQuotidColazione = $_REQUEST['alim_quotid_colazione'];
$alimQuotidColazioneBevande = $_REQUEST['alim_quotid_colazione_bevande'];
$alimQuotidColazioneAlimenti = $_REQUEST['alim_quotid_colazione_alimenti'];
$alimQuotidMerenda = $_REQUEST['alim_quotid_merenda'];
$alimQuotidMerendaBevande = $_REQUEST['alim_quotid_merenda_bevande'];
$alimQuotidMerendaAlimenti = $_REQUEST['alim_quotid_merenda_alimenti'];
$alimQuotidPranzo = $_REQUEST['alim_quotid_pranzo'];
$alimQuotidPranzoBevande = $_REQUEST['alim_quotid_pranzo_bevande'];
$alimQuotidPranzoAlimenti = $_REQUEST['alim_quotid_pranzo_alimenti'];
$alimQuotidPomeridiano = $_REQUEST['alim_quotid_pomeridiano'];
$alimQuotidPomeridianoBevande = $_REQUEST['alim_quotid_pomeridiano_bevande'];
$alimQuotidPomeridianoAlimenti = $_REQUEST['alim_quotid_pomeridiano_alimenti'];
$alimQuotidCena = $_REQUEST['alim_quotid_cena'];
$alimQuotidCenaBevande = $_REQUEST['alim_quotid_cena_bevande'];
$alimQuotidCenaAlimenti = $_POST['alim_quotid_cena_alimenti'];
$sql = 'UPDATE utenti2 SET malattia_past_nome=:past_nome , malattia_past_data_inizio=:past_data_inizio , malattia_past_data_fine=:past_data_fine , malattia_presenti_nome=:presenti_nome , interventi_traumi=:interventi_traumi , interventi_traumi_altro=:traumi_altro , interventi_traumi_data=:traumi_data , sintomi_attuali_nome=:attuali_nome , sintomi_attuali_nome_altro=:attuali_nomea , sintomi_attuali_frequenza=:attuali_frequenza , sintomi_attuali_durata=:attuali_durata , sintomi_attuali_intensita=:attuali_intensita , alim_quotid_colazione=:colazione , alim_quotid_colazione_bevande=:colazione_bevande , alim_quotid_colazione_alimenti=:colazione_alimenti , alim_quotid_merenda=:merenda , alim_quotid_merenda_bevande=:merenda_bevande , alim_quotid_merenda_alimenti=:merenda_alimenti , alim_quotid_pranzo=:pranzo , alim_quotid_pranzo_bevande=:pranzo_bevande , alim_quotid_pranzo_alimenti=:pranzo_alimenti , alim_quotid_pomeridiano=:pomeridiano , alim_quotid_pomeridiano_bevande=:pomeridiano_bevande , alim_quotid_pomeridiano_alimenti=:pomeridiano_alimenti , alim_quotid_cena=:cena , alim_quotid_cena_bevande=:cena_bevande , alim_quotid_cena_alimenti=:cena_alimenti WHERE id=:id LIMIT 1';
$result = $db->prepare($sql);
$result->bindValue(':id', $id, PDO::PARAM_INT);
$result->bindValue(':past_nome', $malattiaPastNome, PDO::PARAM_STR);
$result->bindValue(':past_data_inizio', $malattiaPastDataInizio, PDO::PARAM_STR);
$result->bindValue(':past_data_fine', $malattiaPastDataFine, PDO::PARAM_STR);
$result->bindValue(':presenti_nome', $malattiaPresentiNome, PDO::PARAM_STR);
$result->bindValue(':interventi_traumi', $interventiTraumi, PDO::PARAM_STR);
$result->bindValue(':traumi_altro', $interventiTraumiAltro, PDO::PARAM_STR);
$result->bindValue(':traumi_data', $interventiTraumiData, PDO::PARAM_STR);
$result->bindValue(':attuali_nome', $sintomiAttualiNome, PDO::PARAM_STR);
$result->bindValue(':attuali_nomea', $sintomiAttualiNomeAltro, PDO::PARAM_STR);
$result->bindValue(':attuali_frequenza', $sintomiAttualiFrequenza, PDO::PARAM_STR);
$result->bindValue(':attuali_durata', $sintomiAttualiDurata, PDO::PARAM_STR);
$result->bindValue(':attuali_intensita', $sintomiAttualiIntensita, PDO::PARAM_STR);
$result->bindValue(':colazione', $alimQuotidColazione, PDO::PARAM_STR);
$result->bindValue(':colazione_bevande', $alimQuotidColazioneBevande, PDO::PARAM_STR);
$result->bindValue(':colazione_alimenti', $alimQuotidColazioneAlimenti, PDO::PARAM_STR);
$result->bindValue(':merenda', $alimQuotidMerenda, PDO::PARAM_STR);
$result->bindValue(':merenda_bevande', $alimQuotidMerendaBevande, PDO::PARAM_STR);
$result->bindValue(':merenda_alimenti', $alimQuotidMerendaAlimenti, PDO::PARAM_STR);
$result->bindValue(':pranzo', $alimQuotidPranzo, PDO::PARAM_STR);
$result->bindValue(':pranzo_bevande', $alimQuotidPranzoBevande, PDO::PARAM_STR);
$result->bindValue(':pranzo_alimenti', $alimQuotidPranzoAlimenti, PDO::PARAM_STR);
$result->bindValue(':pomeridiano', $alimQuotidPomeridiano, PDO::PARAM_STR);
$result->bindValue(':pomeridiano_bevande', $alimQuotidPomeridianoBevande, PDO::PARAM_STR);
$result->bindValue(':pomeridiano_alimenti', $alimQuotidPomeridianoAlimenti, PDO::PARAM_STR);
$result->bindValue(':cena', $alimQuotidCena, PDO::PARAM_STR);
$result->bindValue(':cena_bevande', $alimQuotidCenaBevande, PDO::PARAM_STR);
$result->bindValue(':cena_alimenti', $alimQuotidCenaAlimenti, PDO::PARAM_STR);
$result->execute();
Please help me! I have a trouble with error
PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in
I use the query:
public function SaveStep1($data)
$id = intval($data['id']);
$query = $this->db->prepare("UPDATE `user_general_info` SET `soname` = :soname, `last_soname` = :last_soname, `name` = :name,
`middle_name` = :middle_name, `birthday` = :birthday, `birthplace` = :birthplace, `city_code` = :city_code,
`city` = :city, `street` = :street, `house` = : house, `corp` = :corp, `litera` = :litera, `flat` = :flat WHERE `id` = :id");
$query->bindParam(":soname", $data[0], PDO::PARAM_STR);
$query->bindParam(":last_soname", $data[1], PDO::PARAM_STR);
$query->bindParam(":name", $data[2], PDO::PARAM_STR);
$query->bindParam(":middle_name", $data[4], PDO::PARAM_STR);
$query->bindParam(":birthday", $data[5], PDO::PARAM_STR);
$query->bindParam(":birthplace", $data[6], PDO::PARAM_STR);
$query->bindParam(":city_code", $data[7], PDO::PARAM_STR);
$query->bindParam(":city", $data[8], PDO::PARAM_STR);
$query->bindParam(":street", $data[9], PDO::PARAM_STR);
$query->bindParam(":house", $data[10], PDO::PARAM_STR);
$query->bindParam(":corp", $data[11], PDO::PARAM_STR);
$query->bindParam(":litera", $data[12], PDO::PARAM_STR);
$query->bindParam(":flat", $data[13], PDO::PARAM_STR);
$query->bindParam(":id", $data['id'], PDO::PARAM_INT);
//print_r($data[0]);
return $query->execute();
You have a typo in the SQL statement:
`house` = : house
should be
`house` = :house
I'm newbie with pdo. Here i'm trying to insert the datas into database using this below coding. But, i cannot able to insert the datas into database. I'm getting this following error
Fatal error: Call to a member function prepare() on a non-object
I searched on SO and internet about this error. Some people says add global $conn; at the top of your code. I added these code but i'm getting same error. Anyone tell me what should i do if i want to clear this error?
Config.php
<?php
$user = "root";
$password = "password";
try
{
$conn = new PDO('mysql:host=localhost;dbname=evouchers', $user, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
'DATABASE CONNECTION ERROR' .$e->getMessage();
}
?>
**Database.php**
<?php
session_start();
include('config.php');
if(isset($_POST['submit_val']))
{
$cmeal = $_POST['meal'];
try
{
$stmt = $conn->prepare("INSERT INTO ebmealplans ( MealPlanName, CreatedOn ) VALUES ( :cmeal, NOW() )");
$conn->errorInfo();
$stmt->bindParam(':cmeal', $cmeal, PDO::PARAM_STR);
$stmt->execute();
}
catch(PDOException $e)
{
'Query failed to insert into database ' .$e->getMessage();
}
$croom = $_POST['room'];
$ref_key = $conn->lastInsertId();
try
{
$stmt = $conn->prepare("INSERT INTO ebroomtypes ( RoomTypeName, CreatedOn ) VALUES ( :croom, NOW() )");
$conn->errorInfo();
$stmt->bindParam(':croom', $croom, PDO::PARAM_STR);
$stmt->execute();
}
catch(PDOException $e)
{
'Query failed to insert into database ' .$e->getMessage();
}
<*************** UPDATED CODES ***************>
$creference = $_POST['reference'];
$crefdate = $_POST['refdate'];
$ccin = $_POST['cin'];
$cout = $_POST['out'];
$cgname = $_POST['gname'];
$ctotaladults = $_POST['totaladults'];
$cchildrens = $_POST['childrens'];
$cinfants = $_POST['infants'];
$cgphone = $_POST['gphone'];
$cgemail = $_POST['gemail'];
$cgfax = $_POST['gfax'];
$cgaddress1 = $_POST['gaddress1'];
$cgaddress2 = $_POST['gaddress2'];
$cregion = $_POST['region'];
$ccity = $_POST['city'];
$cstate = $_POST['city_state'];
$ccountry = $_POST['country'];
$ccurrency = $_POST['currency'];
$ccurrencyto = $_POST['tocurrency'];
$camount = $_POST['camount'];
$ccurrencyvalue = $_POST['currencyvalue'];
$voucher_fk = $conn->lastInsertId();
try
{
$stmt = $conn->prepare("INSERT INTO ebvouchers ( VoucherReference, BookingDate, CheckIndate, CheckOutDate, MealPlanID_Fk, RoomTypeID_Fk, GuestName, TotalAdults, Childrens, Infants, GuestPhone, GuestEmail, GuestFax, GuestAddressLine1, GuestAddressLine2, GuestRegion, GuestCity, GuestState, GuestCountry, GuestCurrency, GuestCurrencyTo, CurrencyAmount, GuestCurrencyValue, VoucherCreatedOn ) VALUES ( :reference, :refdate, :ccin, :cout, :r_key, :r_key, :gname, :totaladults, :childrens, :infants, :gphone, :gemail, :gfax, :gaddress1, :gaddress2, :gregion, :city, :state, :country, :currency, :currencyto, :amount, :currencyvalue, NOW() )");
$conn->errorInfo();
$stmt->bindParam(':reference', $creference, PDO::PARAM_STR);
$stmt->bindParam(':refdate', $crefdate, PDO::PARAM_STR);
$stmt->bindParam(':ccin', $ccin, PDO::PARAM_STR);
$stmt->bindParam(':cout', $cout, PDO::PARAM_STR);
$stmt->bindParam(':r_key', $ref_key, PDO::PARAM_STR);
$stmt->bindParam(':r_key', $ref_key, PDO::PARAM_STR);
$stmt->bindParam(':gname', $cgname, PDO::PARAM_STR);
$stmt->bindParam(':totaladults', $ctotaladults, PDO::PARAM_STR);
$stmt->bindParam(':childrens', $cchildrens, PDO::PARAM_STR);
$stmt->bindParam(':infants', $cinfants, PDO::PARAM_STR);
$stmt->bindParam(':gphone', $cgphone, PDO::PARAM_STR);
$stmt->bindParam(':gemail', $cgemail, PDO::PARAM_STR);
$stmt->bindParam(':gfax', $cgfax, PDO::PARAM_STR);
$stmt->bindParam(':gaddress1', $cgaddress1, PDO::PARAM_STR);
$stmt->bindParam(':gaddress2', $cgaddress2, PDO::PARAM_STR);
$stmt->bindParam(':gregion', $cregion, PDO::PARAM_STR);
$stmt->bindParam(':city', $ccity, PDO::PARAM_STR);
$stmt->bindParam(':state', $cstate, PDO::PARAM_STR);
$stmt->bindParam(':country', $ccountry, PDO::PARAM_STR);
$stmt->bindParam(':currency', $ccurrency, PDO::PARAM_STR);
$stmt->bindParam(':currencyto', $ccurrencyto, PDO::PARAM_STR);
$stmt->bindParam(':amount', $camount, PDO::PARAM_STR);
$stmt->bindParam(':currencyvalue', $ccurrencyvalue, PDO::PARAM_STR);
$stmt->execute();
}
catch(PDOException $e)
{
'Query failed to insert into database ' .$e->getMessage();
}
<*************** UPDATED CODES ***************>
foreach ( $_POST['slno'] as $key=>$slno )
{
$date = $_POST['date'][$key];
$particulars = $_POST['particulars'][$key];
$noofnights = $_POST['noofnights'][$key];
$rate = $_POST['rate'][$key];
$price = $_POST['price'][$key];
$tax = $_POST['tax'][$key];
$nettotal = $_POST['nettotal'];
$totalamount = $_POST['totalamount'];
$finaltotal = $_POST['finaltotal'];
$c_date = $date;
$c_slno = $slno;
$c_particulars = $particulars;
$c_noofnights = $noofnights;
$c_rate = $rate;
$c_price = $price;
$c_tax = $tax;
$c_nettotal = $nettotal;
$c_totalamount = $totalamount;
$c_finaltotal = $finaltotal;
try
{
$stmt = $conn->prepare("INSERT INTO ebvouchertariffs ( TariffSlNo, TariffDate, TariffParticulars, NoOfNights, TariffRate, TariffPrice, TariffTax, TariffNetTotal, TariffAddTotal, TariffFinalTotal, VoucherID_Fk, CreatedOn ) VALUES ( :c_slno, :c_date, :c_particulars, :c_noofnights, :c_rate, :c_price, :c_tax, :c_nettotal, :c_totalamount, :c_finaltotal, :voucher_fk, NOW() )");
$conn->errorInfo();
$stmt->bindParam(':c_slno', $c_slno, PDO::PARAM_STR);
$stmt->bindParam(':c_date', $c_date, PDO::PARAM_STR);
$stmt->bindParam(':c_particulars', $c_particulars, PDO::PARAM_STR);
$stmt->bindParam(':c_noofnights', $c_noofnights, PDO::PARAM_STR);
$stmt->bindParam(':c_rate', $c_rate, PDO::PARAM_STR);
$stmt->bindParam(':c_price', $c_price, PDO::PARAM_STR);
$stmt->bindParam(':c_tax', $c_tax, PDO::PARAM_STR);
$stmt->bindParam(':c_nettotal', $c_nettotal, PDO::PARAM_STR);
$stmt->bindParam(':c_totalamount', $c_totalamount, PDO::PARAM_STR);
$stmt->bindParam(':c_finaltotal', $c_finaltotal, PDO::PARAM_STR);
$stmt->bindParam(':voucher_fk', $voucher_fk, PDO::PARAM_STR);
$stmt->execute();
}
catch(PDOException $e)
{
'Query failed to insert into database ' .$e->getMessage();
}
$conn = null;
}
}
?>
You did not print your exception. If you have any exception you will not get it. and i think problem in your $conn= null;
$conn = null; this line makes your connection object invalid and after executing this line you have invalid pdo object.for this you have got this error in loop. it should execute all other query before this line is executed. just remove this line. and print your exception message like this:
Try this:
<?php
session_start();
include('config.php');
if(isset($_POST['submit_val']))
{
$cmeal = $_POST['meal'];
try
{
$stmt = $conn->prepare("INSERT INTO ebmealplans ( MealPlanName, CreatedOn ) VALUES ( :cmeal, NOW() )");
$conn->errorInfo();
$stmt->bindParam(':cmeal', $cmeal, PDO::PARAM_STR);
$stmt->execute();
}
catch(PDOException $e)
{
die('Query failed to insert into database ' .$e->getMessage());
}
$croom = $_POST['room'];
$ref_key = $conn->lastInsertId();
try
{
$stmt = $conn->prepare("INSERT INTO ebroomtypes ( RoomTypeName, CreatedOn ) VALUES ( :croom, NOW() )");
$conn->errorInfo();
$stmt->bindParam(':croom', $croom, PDO::PARAM_STR);
$stmt->execute();
}
catch(PDOException $e)
{
die( 'Query failed to insert into database ' .$e->getMessage());
}
foreach ( $_POST['slno'] as $key=>$slno )
{
$date = $_POST['date'][$key];
$particulars = $_POST['particulars'][$key];
$noofnights = $_POST['noofnights'][$key];
$rate = $_POST['rate'][$key];
$price = $_POST['price'][$key];
$tax = $_POST['tax'][$key];
$nettotal = $_POST['nettotal'];
$totalamount = $_POST['totalamount'];
$finaltotal = $_POST['finaltotal'];
$c_date = $date;
$c_slno = $slno;
$c_particulars = $particulars;
$c_noofnights = $noofnights;
$c_rate = $rate;
$c_price = $price;
$c_tax = $tax;
$c_nettotal = $nettotal;
$c_totalamount = $totalamount;
$c_finaltotal = $finaltotal;
try
{
$stmt = $conn->prepare("INSERT INTO ebvouchertariffs ( TariffSlNo, TariffDate, TariffParticulars, NoOfNights, TariffRate, TariffPrice, TariffTax, TariffNetTotal, TariffAddTotal, TariffFinalTotal, VoucherID_Fk, CreatedOn ) VALUES ( :c_slno, :c_date, :c_particulars, :c_noofnights, :c_rate, :c_price, :c_tax, :c_nettotal, :c_totalamount, :c_finaltotal, :voucher_fk, NOW() )");
$conn->errorInfo();
$stmt->bindParam(':c_slno', $c_slno, PDO::PARAM_STR);
$stmt->bindParam(':c_date', $c_date, PDO::PARAM_STR);
$stmt->bindParam(':c_particulars', $c_particulars, PDO::PARAM_STR);
$stmt->bindParam(':c_noofnights', $c_noofnights, PDO::PARAM_STR);
$stmt->bindParam(':c_rate', $c_rate, PDO::PARAM_STR);
$stmt->bindParam(':c_price', $c_price, PDO::PARAM_STR);
$stmt->bindParam(':c_tax', $c_tax, PDO::PARAM_STR);
$stmt->bindParam(':c_nettotal', $c_nettotal, PDO::PARAM_STR);
$stmt->bindParam(':c_totalamount', $c_totalamount, PDO::PARAM_STR);
$stmt->bindParam(':c_finaltotal', $c_finaltotal, PDO::PARAM_STR);
$stmt->bindParam(':voucher_fk', $voucher_fk, PDO::PARAM_STR);
$stmt->execute();
}
catch(PDOException $e)
{
die('Query failed to insert into database ' .$e->getMessage());
}
//$conn = null;
}
}
?>