jmr3460 Posted March 10, 2009 Share Posted March 10, 2009 I have been working on this for a little while. I am following instructions on a tutorial I am taking. I have had some trouble with my testing environment, but it is now solved. I installed WAMP 2.0 and now my php and mysql are connecting. I can also edit my index.php page now. I am going slow but sure. I had the same problem on my other test environment (my rented server) anyway, I can't get this line to validate. I listened to the tutorial and I was told that I have to comment out a single quote string if it is inside another single quote string. I am not even sure if this is the problem, but when I move VALUES down to the next line then my problem is shifted down to that line. I am trying to INSERT $_POST[field] into the TABLE members. This is what I have created: <?php if (isset($_POST['submit'])){ if ($_POST['submit'] == 'save'){ query('INSERT INTO members(fname, lname, address, city, state, zip) VALUES (\'$_POST['fname']\', \'$_POST['lname']\', \'$_POST['address']\', \'$_POST['city']\', \'$_POST['state']\', \'$_POST['zip']\')'); } } ?> This is my form: <form method="POST" action="index.php"> <table> <tr><td>First Name:</td><td><input type="text" name="fname" /></td></tr> <tr><td>Last Name:</td><td><input type="text" name="lname" /></td></tr> <tr><td>Address:</td><td><input type="text" name="address" /></td></tr> <tr><td>City:</td><td><input type="text" name="city" /></td></tr> <tr><td>State:</td><td><input type="text" name="state" /></td></tr> <tr><td>Zip Code:</td><td><input type="text" name="zip" /></td></tr> </table> <input type="submit" name="submit" value="Save" /> </form> Sorry for all the mess I am trying to learn this so I can add my own code (if you will) to my web sites. Thanks very much for any help I could get!!! JMR3460 Quote Link to comment https://forums.phpfreaks.com/topic/148692-solved-insert-please/ Share on other sites More sharing options...
ngreenwood6 Posted March 10, 2009 Share Posted March 10, 2009 I saw that you said that you got an error but you never said what it was. It is always helpful if we can see the error. Anyways if you use double quotes on the outside you can then use single quotes inside the double quotes with out having to escape(the escape character '\') them. Try this: <?php $first_name = $_POST['fname']; $last_name = $_POST['lname']; $address = $_POST['address']; $city = $_POST['city']; $state = $_POST['state']; $zip = $_POST['zip']; if (isset($_POST['submit'])){ if ($_POST['submit'] == 'save'){ query("INSERT INTO members(fname,lname,address,city,state,zip) VALUES ('$first_name','$last_name','$address','$city','$state','$zip')"); } } ?> That is alot easier to read and makes it easier to troubleshoot. Also if you didn't notice I removed the spaces frome the VALUES() section. You cannot have spaces in that section. I also just removed them from the other section, its just good to have a standard. If you get any errors let me know what they are. Quote Link to comment https://forums.phpfreaks.com/topic/148692-solved-insert-please/#findComment-780793 Share on other sites More sharing options...
jmr3460 Posted March 10, 2009 Author Share Posted March 10, 2009 Hey Thanks, That got rid of my syntax issue but Now the form doesn't insert anything. The only way I can insert data into the table is through phpmyadmin. This is a block that helps me see if some happened: <?php $mysql_query_error = ''; function query($mysql_query){ global $mysql_query_error; $data = mysql_query($mysql_query); $mysql_query_error .= $mysql_query.'<br/>'; if (mysql_errno() !=0){ $mysql_query_error .= '<strong>Query failed: error# '.mysql_errno(). ' -- ' .mysql_error().'</strong><br/>'; } return $data; } ?> This is how I am opening the Database and selecting everything in the table: <?php $host = 'localhost'; $username = 'root'; $password = ''; $database ='contacts'; mysql_connect($host, $username, $password); mysql_select_db($database); $first_name = $_POST['fname']; $last_name = $_POST['lname']; $address = $_POST['address']; $city = $_POST['city']; $state = $_POST['state']; $zip = $_POST['zip']; if (isset($_POST['submit'])){ if ($_POST['submit'] == 'save'){ mysql_query("INSERT INTO 'contacts'.'members'(fname,lname,address,city,state,zip) VALUES ('$first_name','$last_name','$address','$city','$state','$zip')"); } } ?> I also have another snippet of code that that tells me at the bottom of the page what queries were taken. <?php $contacts = query('SELECT * FROM members'); while($contact = mysql_fetch_assoc($contacts)){ print('<div style="float:left; border:thin solid #cccccc; width:100px; clear:both;">'.$contact['fname'].'</div> <div style="float:left; border:thin solid #cccccc; width:150px;">'.$contact['lname'].'</div> <div style="float:left; border:thin solid #cccccc; width:150px;">'.$contact['address'].'</div> <div style="float:left; border:thin solid #cccccc; width:150px;">'.$contact['city'].'</div> <div style="float:left; border:thin solid #cccccc; width:50px;">'.$contact['state'].'</div> <div style="float:left; border:thin solid #cccccc; width:100px;">'.$contact['zip'].'</div>'); } print('<div style="clear:both"></div><br/><br/>'.$mysql_query_error); ?> Can you tell me why nothing happens on the insert? Again there is no warning now just nothing is inserted. Thanks again for your help, jmr3460 Quote Link to comment https://forums.phpfreaks.com/topic/148692-solved-insert-please/#findComment-780856 Share on other sites More sharing options...
jmr3460 Posted March 10, 2009 Author Share Posted March 10, 2009 Hey I got it. if $_POST['submit']; if $_POST['submit'] =='Save'; In the code at first save on the form was with lower case 's'. Basically I changed the case. I am learning. Everything in its own time. Thanks for all the help. This forum if anything has helped me find the solutions. Thanks Again I will also learn where to post, SORRY! Quote Link to comment https://forums.phpfreaks.com/topic/148692-solved-insert-please/#findComment-780896 Share on other sites More sharing options...
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.