-1

hello need some help with my php queries

php code =>

try
{
  $db = new PDO('mysql:host=localhost;dbname=monprojet;charset=UTF8', 'root', 'root');
}
catch(Exception $e)
{
  die('Erreur : '.$e->getMessage());
}
$login = $_POST['loginProfil'];

$mdp = $_POST['mdpProfil'];

$email = $_POST['emailProfil'];
try {
  $rep = $db->prepare('SELECT id FROM utilisateur WHERE login= :logi');
  $rep->execute(array(
    ':logi' => $_SESSION['login']
  ));
  $repo = $rep->fetch();
} catch (Exception $e) {
  die('Erreur de requete 1 : '.$e->getMessage());
}
try {
  $reponse = $db->prepare('UPDATE utilisateur SET login= :login, mdp= :mdp, email= :email WHERE id= :id');
  $reponse->execute(array(
    ':login' => $login,
    ':mdp' => $mdp,
    ':email' => $email,
    ':id' => $repo['id']
  ));
} catch (Exception $e) {
  die('Erreur de requete : '.$e->getMessage());
}
$json = json_encode($reponse->fetchAll());
$reponse->closeCursor();
echo $json;

i can't manage to update my DB and i don't get why, need your assistance Stack

u_mulder
  • 54,101
  • 5
  • 48
  • 64
IZAD
  • 7
  • 5
  • Any errors? Or some other symptoms? – u_mulder Jan 19 '18 at 16:02
  • Sure that `$login` is not empty? Sure that `$repo` is not empty? Sure that session started? – u_mulder Jan 19 '18 at 16:04
  • You have 2 typos in your code, it should be `:login` not `:logi` – IcedAnt Jan 19 '18 at 16:08
  • 1
    You don't say what your specific problem is or show any errors. SO is unlikely to debug your code for you, so you should show what you have tried and what results you get. –  Jan 19 '18 at 16:09
  • try to change this: $repo = $rep->fetch(); to this: $repo = $rep->fetchColumn(); Since you are quering a single value you can assign it directly to the variable. So later ':id' => $repo and not ':id' => $repo['id'] – Lelio Faieta Jan 19 '18 at 16:24
  • @IcedAnt why is that a typo? they have that in their first query – Funk Forty Niner Jan 19 '18 at 17:24
  • if you checked for errors, then maybe... just maybe you'll see what is causing your query/queries to fail. But you're not doing that. – Funk Forty Niner Jan 19 '18 at 17:25

1 Answers1

0

If the table is not updating with the issued query, try checking the id value in the update query WHERE clause.

// Check if any data is found
$check = $db->prepare('SELECT COUNT(id) FROM utilisateur WHERE id= :id');

$check->execute(array(
    'id' => $repo['id']
));

var_dump( $check->fetchColumn() );

if no result returned, then you know $repo['id'] is not on the table.

John Zenith
  • 472
  • 5
  • 10