0

I'm trying to update data in my database with this function

<?php
require "conn.php";
$name = $_POST["name"];
$surname = $_POST["surname"];
$age = $_POST["age"];
$username = $_POST["username"];
$password = $_POST["password"];
$mysql_qry = "UPDATE user_data (name, surname, age, username, password) values ('$name','$surname','$age','$username','$password')";
if($conn->query($mysql_qry) === TRUE) {
echo "Success!";
}
else {
echo "Something is wrong :( Error: " . $mysql_qry . "<br>" . $caonn->error;
}
$conn->close();

but it always goes to Error. Can you help me with this? I was trying to put data from keyboard but it's not working either.

I've changed this line

$mysql_qry = "UPDATE user_data SET name = '$name'";

and now it's working but its changing all the names in database and I want to change only one record...

Piotr Szczepanik
  • 361
  • 2
  • 4
  • 17

1 Answers1

0

Add WHERE id=THAT_ID and you will be fine. THAT_ID or 'THAT_ID' depending on whether the "id" field in the database is a number or text, respectively (without apostrophes if it's a number, and with apostrophes if it is a text field). Off course, change THAT_ID to whatever id you want.

Vladimir Despotovic
  • 3,200
  • 2
  • 30
  • 58
  • 1
    sure, I get it but I want – Piotr Szczepanik Dec 08 '16 at 23:49
  • 1
    sure, I get it but I want it to check it itself. I've got the login formula before that, and it suppose to take id of a user who is currently loged – Piotr Szczepanik Dec 08 '16 at 23:55
  • Try using $_SESSION variable then and putting user id inside that($_SESSION["user_id"] = SOME_NUMBER before you do anything else). Then you can (prior to it being filtered for illegal characters) put it as THAT_ID. – Vladimir Despotovic Dec 09 '16 at 00:07
  • If you want to be more specific, open another question and ask how to store user id in a session, because it really depends on how you are handling the user id in the session. But you can upvote my comments and my answer and you can feel free to accept it as an answer to THIS question. – Vladimir Despotovic Dec 09 '16 at 00:09