Nopus Nopus - 1 year ago 71
MySQL Question

Newbe asking / MySQL Update with PHP

I am new on the Platform and also new in Programming. So you need it to explain it to me on the easy way. Normally i Programm microchips and have nothing to do with DBs. ^^

I just du for myself a Homepage and try to do all by myself.
Now i try to Update my "Backend" Settings in the Database over a the "POST" Methode.

UPDATE controll SET ticket_new = $_POST["chagne_tickets_new"] WHERE language = $_POST["Change_language"]

Thats just e example how i want it but i have no idea how to do it.
i also tried it like that:

$Change_ticket_post = $_POST["Change_tickets_new"]
$Change_language = $_POST["Change_language"]

UPDATE controll SET ticket_new =". $Change_ticket_post. " WHERE language =". $Change_language;

Also that doesn`t works and i got a FATAL ERROR also.

Answer Source

You are doing it wrong.

First of all, it's best practice to use ' (single quotes) to wrap array keys.

So, you should do like this:

$Change_ticket_post = $_POST['Change_tickets_new'];
$Change_language =    $_POST['Change_language'];

Second, the values in a MySQL query should be wrapped within ' (single quotes) too.

So, your query would be:

$query = "UPDATE controll SET ticket_new='$Change_ticket_post' WHERE language='$Change_language';

And most of all, you need to learn a lot.

Coding best practices:

SQL Injection: How can I prevent SQL-injection in PHP?

Sanitize & escape user data: What's the best method for sanitizing user input with PHP?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download