html - Using PHP to insert data into a MySQL Database -
i'm trying create self-submitting page create form user fill out. information stored in mysql database. form seems working, can't insert information form database reason. here's have:
<!doctype html> <html> <head> <title>mysql test</title> </head> <body> <h1>mysql test</h1> <?php if($_server["request_method"] == "get") { ?> <form action="" method="post"> <input type="text" name="name" placeholder="name" /><br /> <input type="submit" value="send" /> </form> <?php } else if ($_server["request_method"] == "post") { $name = $_post["name"]; $server = new pdo("mysql:dbname=test;host=localhost", "root", "root"); $server->execute("insert test ('name') values $name;"); } ?> </body> </html>
what should change?
please insert this:
$name = "john"; $query = "insert test(col) values(:name);"; $statement = $server->prepare($query); $statement->execute(array(":name" => $name));
this called using prepared statements, it's easy , avoid sql-injection. can execute multiple variables on multiple cols on query separating commas after each ":col" => $colval, that's not needed here, tip.
you can updates aswell.
remember check if being-inserted value empty or not.
Comments
Post a Comment