darbok 1 Posted May 28, 2015 Report Share Posted May 28, 2015 <!doctype html> <html> <head> <link rel="stylesheet" href="style.css"> <meta charset="utf-8"> <title> My Practice Page </title> </head> <body> <? $first_name = ''; $last_name =''; $city =''; ?> $db = MYSQLI_CONNECT('localhost','root','','practiceforms'); $sql = "INSERT INTO users (first name, last name, city) VALUES ('$first_name', '$last_name', '$city')"; mysqli_query($db, $sql); mysqli_close($db); echo '<p> User added. </p>'; <form method="post" action=""> First Name: <input type="text" name="First Name" value="<?php $first_name;?>" <br> Last Name: <input type="text" name="Last Name" value ="<?php $last_name;?>"<br> City: <input type ="text" name="city" value=<?php $city; ?>" <br> <input type="submit" name="submit" value="submit"> </body> </html> Quote Link to post Share on other sites
davej 251 Posted May 28, 2015 Report Share Posted May 28, 2015 Perhaps you should begin with something like this... <?phpecho "<p>Php is running</p>";error_reporting(E_ALL);ini_set('display_errors','On');$db = mysqli_connect("localhost","root","mypassword");if(!$db){ echo "<p>Unable to connect to MySQL</p>";}else if(!mysqli_select_db($db,"mydatabasename")){ echo "<p>Unable to open database</p>";}else{ echo "<p>database connected</p>"}?> Quote Link to post Share on other sites
justsomeguy 1,135 Posted May 28, 2015 Report Share Posted May 28, 2015 First, you should use the regular PHP tags (<?php) instead of the short tags. Short tags can be disabled. Second, your code to connect to the database is not inside a PHP block, the server thinks that is HTML instead of PHP. If you're trying to process that form, you're not getting the information that was submitted. Since the form uses the post method then the values will be in the $_POST array. You can use print_r to print the entire array to see what's in it: print_r($_POST);Also, this isn't doing anything:<?php $first_name;?>If you want to print something then use echo to print it. Quote Link to post Share on other sites
darbok 1 Posted May 28, 2015 Author Report Share Posted May 28, 2015 hmm, ok... so then the info that is entered into the form, how then does it get connected to the variables like $first_name, $last_name, $city ? Quote Link to post Share on other sites
justsomeguy 1,135 Posted May 28, 2015 Report Share Posted May 28, 2015 It will all be in $_POST, so you can either use it straight from there or copy it into the other variables. PHP used to have a feature that would automatically create variables from things in $_POST, $_GET, $_COOKIE, etc, but that feature is pretty ancient and mostly unused because it presents security problems in poorly-written code. Quote Link to post Share on other sites
darbok 1 Posted May 28, 2015 Author Report Share Posted May 28, 2015 (edited) So, I've revised the code to this, I get no more error messages, but my input is still not going into the table. <!doctype html> <html> <head> <link rel="stylesheet" href="style.css"> <meta charset="utf-8"> <title> My Practice Page </title> </head> <body> <?php $first_name = $_POST['first name']; $last_name = $_POST['last name']; $city = $_POST['city']; $db = MYSQLI_CONNECT('localhost','root','','practiceforms'); if (!$db) { die("Connection failed: " . mysqli_connect_error()); } $sql = "INSERT INTO users (first name, last name, city) VALUES ('$first_name','$last_name','$city')"; mysqli_query($db, $sql); mysqli_close($db); ?> <form method="post" action=""> First Name:<br> <input type="text" name="First Name" value=""> <br> Last Name:<br> <input type="text" name="Last Name" value =""><br> City:<br> <input type ="text" name="city" value=""> <br> <input type="submit" name="submit" value="submit"> </body> </html> Edited May 28, 2015 by darbok Quote Link to post Share on other sites
davej 251 Posted May 28, 2015 Report Share Posted May 28, 2015 You have spaces in your table column names? Quote Link to post Share on other sites
darbok 1 Posted May 28, 2015 Author Report Share Posted May 28, 2015 (edited) yes I do. I get this error. Notice: Undefined index: first name in C:xampphtdocspracticeformspracticeforms.php on line 11Notice: Undefined index: last name in C:xampphtdocspracticeformspracticeforms.php on line 12Notice: Undefined index: city in C:xampphtdocspracticeformspracticeforms.php on line 13 Edited May 28, 2015 by darbok Quote Link to post Share on other sites
justsomeguy 1,135 Posted May 28, 2015 Report Share Posted May 28, 2015 Those errors mean that you're trying to access things in $_POST that aren't there. That will happen if you try to access $_POST without the form being submitted. You can use isset to check if something is set before trying to access it, you can use that to figure out if the form was submitted. Also, case matters. If you name your field "First Name" then you need to access it exactly like that. For the spaces in the column names in your database table, you need to surround those with backquotes. Quote Link to post Share on other sites
dsonesuk 925 Posted May 28, 2015 Report Share Posted May 28, 2015 Don't use spaces in input name attribute either, also check $_POST values hvae been set, before inserting in database table.if(isset($_POST['first_name']) && isset($_POST['last_name']) &&(isset($_POST['city'])){Code for inserting into table...} Quote Link to post Share on other sites
darbok 1 Posted May 28, 2015 Author Report Share Posted May 28, 2015 I see I think. I guess I was just trying to make a simple form to practice doing the basics of putting something into a table. Quote Link to post Share on other sites
davej 251 Posted May 28, 2015 Report Share Posted May 28, 2015 You are also not being careful to exactly match your capitalization. Php is case-sensitive like most modern languages. Quote Link to post Share on other sites
darbok 1 Posted May 29, 2015 Author Report Share Posted May 29, 2015 this is the error I get now. Parse error: syntax error, unexpected ';' in C:xampphtdocspracticeformspracticeforms.php on line 24 though i think it's gotten better. <!doctype html> <html> <head> <link rel="stylesheet" href="style.css"> <meta charset="utf-8"> <title> My Practice Page </title> </head> <body> <?php $first_name = $_POST["firstname"]; $last_name = $_POST["lastname"]; $city = $_POST["city"]; $db = MYSQLI_CONNECT('localhost','root','','practiceforms'); if (!$db) { die("Connection failed: " . mysqli_connect_error()); } echo 'Connected successfully'; if(isset($_POST['first_name']) && isset($_POST['last_name']) &&(isset($_POST['city'])); { $sql = "INSERT INTO users ('FIRST NAME', 'LAST NAME', 'CITY') VALUES ('$first_name','$last_name','$city')"; } mysqli_query($db, $sql); mysqli_close($db); ?> <form method="post" action=""> First Name:<br> <input type="text" name="firstname" value=""> <br> Last Name:<br> <input type="text" name="lastname" value =""><br> City:<br> <input type ="text" name="city" value=""> <br> <input type="submit" name="submit" value="submit"> </form> </body> </html> Quote Link to post Share on other sites
darbok 1 Posted May 29, 2015 Author Report Share Posted May 29, 2015 thanks for the help, much appreciated, the hints and such were a good deal. To make it work I turned it into two files, one for the form and one for the php, I also changed the SQL column names to be one word all lower case instead of two words uppercase. I also took the single quote marks out of my column names. Quote Link to post Share on other sites
dsonesuk 925 Posted May 29, 2015 Report Share Posted May 29, 2015 Remove ';‘ at end of isset if conditionif(isset($_POST['first_name']) && isset($_POST['last_name']) &&(isset($_POST['city'])); Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.