Sigmahokies Posted March 28, 2021 Share Posted March 28, 2021 Hi everyone, I'm trying to use PDO to insert data in database, seem none of them success, it just breakdown after I click button to submit. what did I do wrong? [php] <?php /* check to see connection is working */ if(isset($_POST['submit'])) { $server = "localhost"; $username = "Sigma"; $password = ""; $dbname = "Register"; try { $connect = new PDO ("mysql:host=$server;dbname=$dbname", $username, $password); } catch (PDOException $error){ echo $error->getMessage(); } /* Insert data into database */ $firsts = $_POST['first']; $lasts = $_POST['last']; $town = $_POST['town']; $states = $_POST['state']; $zip = $_POST['zip']; $insert = "INSERT INTO `Members`(`firstName`, `lastName`, `town`, `states`, `zip`) VALUES (:firsts,:lasts,:town,:states,:zip)"; $result = $connect->prepare($insert); $result_run = $result->execute(array(":first"=>$firsts, ":last"=>$lasts, ":town"=>$town, ":state"=>$states, ":zip"=>$zip)); if($result_run) { echo '<script>alert("successfully added")"</script>'; } else { echo '<script>alert("failed added")"</script>'; } } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Gary's PDO website</title> <link rel="stylesheet" type="text/css" href="CSS/insert.css"> </head> <body> <p> <h2>Welcome to Gary Taylor's Demonstration</h2> <p> <h3>Insert Data in Database by PDO</h3> <form action="index.php" method="post"> <table> <tr><th colspan="2">Register here</th></tr> <tr><td>First Name:</td><td><label><input type="text" name="first" placeholder="Ex: Gary"> </label></td></tr> <tr><td>Last Name:</td><td><label><input type="text" name="last" placeholder="Ex: Taylor"> </label></td></tr> <tr><td>Town:</td><td><label><input type="text" name="town" placeholder="Ex: Richmond"> </label></td></tr> <tr><td>State:</td><td><label><input type="text" name="state" placeholder="Ex: VA"> </label></td></tr> <tr><td>Zip Code:</td><td><label><input type="text" name="zip" placeholder="Ex: 23238"> </label></td></tr> <tr><td colspan="2"><label><input type="submit" name="submit" value="Insert into database"></label></td></tr> </table> </form> </body> </html> [/php] Link to comment Share on other sites More sharing options...
Ingolme Posted March 28, 2021 Share Posted March 28, 2021 The keys in your array don't match the placeholders in the SQL query. Link to comment Share on other sites More sharing options...
Sigmahokies Posted March 29, 2021 Author Share Posted March 29, 2021 So, i remove placeholder? Link to comment Share on other sites More sharing options...
Ingolme Posted March 29, 2021 Share Posted March 29, 2021 No, just update the keys of the data array to be the same as the placeholders. There is some information about PDO placeholders on this page: https://www.w3schools.com/php/php_mysql_prepared_statements.asp Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now