pluto Posted December 16, 2008 Share Posted December 16, 2008 Dear all, I've never done anything in PHP before, so please bear with me. I've been asked to write something that will allow a user to manage a dictionary, and I've written a bit of code that'll take input from a form, and store it in a mySQL data base. The data base structure is as follows: ID - AutoIncremement Term - Varchar (100) Definition - Varchar (150) Chapter - Integer Section - Integer I've written the following code, which loads fine. And will allow the user to seemingly go through the data entry bit. But when I look in MySQL table, there's no records there. I'm using PHP version 5.2.6 which came with my XAMP installation. And I think I'm using mySQL 5.067 Any help would be awesome. Thanks Pluto <?php if($_POST[op] != "add") { $display_block = "<h1>Add an Entry</h1> <form method=\"post\" action=\"$_SERVER[php_SELF]\"> <p><strong>Term:</strong><br> <input type=\"text\" name=\"term\" size=50 maxlength=75> <p><strong>Definition:</strong><br> <input type=\"text\" name=\"definition\" size=50 maxlength=75> <p><strong>Chapter:</strong><br> <input type=\"text\" name=\"chapter\" size=30 maxlength=75> <p><strong>Section:</strong><br> <input type=\"text\" name=\"section\" size=30 maxlength=75> <p><input type=\"submit\" name=\"submit\" value=\"Add Word\"></p> </form>"; } else if($_POST[op]=="add") { // time to add to tables, so check for fields if(($P_POST[term]=="")||($P_POST[definition]=="")) { header("Location: test.php"); exit; } // connect to the data base. $conn = mysql_connect("localhost","root","") ; mysql_select_db("v1test",$conn); // add to records to table $add_word = "insert into englishtest (id, term,definition, chapter, section) values (' ','$_POST[term]','$_POST[definition]','$_POST[chapter]','$_POST[section]')"; mysql_query($add_word) ; //$word_id = mysql_insert_id(); } ?> <HTML> <HEAD> <TITLE>Add an Entry </TITLE> <BODY> <?php echo $display_block;?> </BODY> </HTML> Quote Link to comment https://forums.phpfreaks.com/topic/137254-solved-inserting-a-record-in-to-a-mysql-data-base/ Share on other sites More sharing options...
trq Posted December 16, 2008 Share Posted December 16, 2008 There is nothing in your form named 'op' so your query section is never executing. Be aware also that associative indexes need to be surrounded by quotes. eg $_POST[op] should be $_POST['op']. Quote Link to comment https://forums.phpfreaks.com/topic/137254-solved-inserting-a-record-in-to-a-mysql-data-base/#findComment-717062 Share on other sites More sharing options...
Maq Posted December 16, 2008 Share Posted December 16, 2008 Out of curiosity, why would you write this in a language you've never touched before? Quote Link to comment https://forums.phpfreaks.com/topic/137254-solved-inserting-a-record-in-to-a-mysql-data-base/#findComment-717067 Share on other sites More sharing options...
pluto Posted December 16, 2008 Author Share Posted December 16, 2008 Out of curiosity, why would you write this in a language you've never touched before? Well I did try and let the dude let me write this in ASP.NET, but he said no, PHP... So I'm learning as I go... and not doing a good job it would seem lol Quote Link to comment https://forums.phpfreaks.com/topic/137254-solved-inserting-a-record-in-to-a-mysql-data-base/#findComment-717074 Share on other sites More sharing options...
pluto Posted December 16, 2008 Author Share Posted December 16, 2008 Must confess that much of this I've learned from a book, and I've looked and looked, and it doesn't seem to define op anywhere. Which is kind of annoying. Which makes me wonder how this script ran in the first place. I've tried a few bits, but the book isn't forthcoming. If anyone has any advice, I'd be greatful. And thanks for the responses already. Pluto Quote Link to comment https://forums.phpfreaks.com/topic/137254-solved-inserting-a-record-in-to-a-mysql-data-base/#findComment-717087 Share on other sites More sharing options...
premiso Posted December 16, 2008 Share Posted December 16, 2008 $display_block = "<h1>Add an Entry</h1> <form method=\"post\" action=\"$_SERVER[php_SELF]\"> <p><strong>Term:</strong><br> <input type=\"text\" name=\"term\" size=50 maxlength=75> <p><strong>Definition:</strong><br> <input type=\"text\" name=\"definition\" size=50 maxlength=75> <p><strong>Chapter:</strong><br> <input type=\"text\" name=\"chapter\" size=30 maxlength=75> <p><strong>Section:</strong><br> <input type=\"text\" name=\"section\" size=30 maxlength=75> <input type=\"hidden\" name=\"op\" value=\"yes\" /> <p><input type=\"submit\" name=\"submit\" value=\"Add Word\"></p> </form>"; Add it via a hidden input box. I would also surround it in ' (single quotes) when you access it via post: $_POST['op'] Quote Link to comment https://forums.phpfreaks.com/topic/137254-solved-inserting-a-record-in-to-a-mysql-data-base/#findComment-717091 Share on other sites More sharing options...
pluto Posted December 16, 2008 Author Share Posted December 16, 2008 So sorry for my ineptitude at this, but I've made the changes that you suggested, and it still won't send anything to the data base. Even though the user has rights to insert etc. I'm now at a loss as to why this shouldn't work. Also when I add single quotes (') to the $_POST[fieldname] lines, e.g term, etc. It throws a hissy. The error I get is as follows. Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\xampp\htdocs\test.php on line 38 also I'm on msn messenger. emyrwilliams at hotmail dot com Pluto Quote Link to comment https://forums.phpfreaks.com/topic/137254-solved-inserting-a-record-in-to-a-mysql-data-base/#findComment-717113 Share on other sites More sharing options...
Maq Posted December 16, 2008 Share Posted December 16, 2008 Try this: if(isset($_POST['submit'])) { echo "You just tried to submit hahaha"; } if($_POST['op'] != "add") { $display_block = "Add an Entry </pre> <form method='\"post\"' action="%5C%22%24_SERVER%5Bphp_SELF%5D%5C%22"> Term: Definition: Chapter: Section: </form>";<br>}<br>else if($_POST['op']=="add")<br>{<br>$display_block = "<h1>Got to the if statement!!!</h1>";<br> // time to add to tables, so check for fields<br> if(($P_POST['term']=="")||($P_POST['definition']==""))<br> {<br> header("Location: test.php");<br> exit;<br> }<br> <br> // connect to the data base.<br> $conn = mysql_connect("localhost","root","") ;<br> mysql_select_db("v1test",$conn);<br> <br> // add to records to table<br> $add_word = "insert into englishtest (id, term,definition, chapter, section) values ('','{$_POST['term']}','{$_POST['definition']}','{$_POST['chapter']}','{$_POST['section']}')";<br> mysql_query($add_word) or die(mysql_error());<br> //$word_id = mysql_insert_id();<br>}<br>?><br><br><br><br><title>Add an Entry </title> <br><br><b Quote Link to comment https://forums.phpfreaks.com/topic/137254-solved-inserting-a-record-in-to-a-mysql-data-base/#findComment-717119 Share on other sites More sharing options...
pluto Posted December 16, 2008 Author Share Posted December 16, 2008 Try this: <?php if(isset($_POST['submit'])) { echo "You just tried to submit hahaha"; } if($_POST['op'] != "add") { $display_block = "<h1>Add an Entry</h1> <form method=\"post\" action=\"$_SERVER[php_SELF]\"> <p><strong>Term:</strong><br> <input type=\"text\" name=\"term\" size=50 maxlength=75> <p><strong>Definition:</strong><br> <input type=\"text\" name=\"definition\" size=50 maxlength=75> <p><strong>Chapter:</strong><br> <input type=\"text\" name=\"chapter\" size=30 maxlength=75> <p><strong>Section:</strong><br> <input type=\"text\" name=\"section\" size=30 maxlength=75> <input type=\"hidden\" name=\"op\" value=\"yes\" /> <p><input type=\"submit\" name=\"submit\" value=\"Add Word\"></p> </form>"; } else if($_POST['op']=="add") { $display_block = "<h1>Got to the if statement!!!</h1>"; // time to add to tables, so check for fields if(($P_POST['term']=="")||($P_POST['definition']=="")) { header("Location: test.php"); exit; } // connect to the data base. $conn = mysql_connect("localhost","root","") ; mysql_select_db("v1test",$conn); // add to records to table $add_word = "insert into englishtest (id, term,definition, chapter, section) values ('','{$_POST['term']}','{$_POST['definition']}','{$_POST['chapter']}','{$_POST['section']}')"; mysql_query($add_word) or die(mysql_error()); //$word_id = mysql_insert_id(); } ?> <HTML> <HEAD> <TITLE>Add an Entry </TITLE> <BODY> <?php echo $display_block;?> </BODY> </HTML> Weird...that didn't do it either....got the data base name right, the table name right and everything.... I got the echo message at the top, however nothign about got to the if statement, so it's not getting to the bit that's actually inserting the records for some reason. Also in the course of looking through it, I found that op value was Yes rather than add, so I changed that over, then got this error Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\test2.php:4) in C:\xampp\htdocs\test2.php on line 32 Thanks Pluto Quote Link to comment https://forums.phpfreaks.com/topic/137254-solved-inserting-a-record-in-to-a-mysql-data-base/#findComment-717124 Share on other sites More sharing options...
Maq Posted December 16, 2008 Share Posted December 16, 2008 Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\test2.php:4) in C:\xampp\htdocs\test2.php on line 32 Yes, you get this error message because you're echoing before your redirect (my fault). Take out these lines: if(isset($_POST['submit'])) { echo "You just tried to submit hahaha"; } Where does the the $P_POST array come from? $P_POST['term']; $P_POST['definition']; Quote Link to comment https://forums.phpfreaks.com/topic/137254-solved-inserting-a-record-in-to-a-mysql-data-base/#findComment-717132 Share on other sites More sharing options...
pluto Posted December 16, 2008 Author Share Posted December 16, 2008 My understanding is that they're superglobals, so I don't need to define them anywhere. And my (limited) understanding was that you use the $_POST on the variables you want to add to the POST request to the server. Or am I wrong? Sorry, this is the first thing I've ever written in PHP Quote Link to comment https://forums.phpfreaks.com/topic/137254-solved-inserting-a-record-in-to-a-mysql-data-base/#findComment-717142 Share on other sites More sharing options...
pluto Posted December 17, 2008 Author Share Posted December 17, 2008 Dear all, I have since resolved the issue. I found a good book on the subject, and figured I'd follow their example. They seperated the HTML from the PHP, so that way, it would be clearer to edit and maintain. So I now have two seperate files. Their code is as follows. insert_test.html - This will be re-written to include java script at some point. As I'm going to be doing things in different languages. <!DOCTYPE html SYSTEM "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Insert a record one word at a time</title> </head> <body> <FORM ACTION="insert_word.php" METHOD=POST> <p> Text to add:<br/> <input type=text name="word" size=30><br> <input type=text name="definition" size=30><br> <input type=text name="chapter" size=30><br> <input type=text name="section" size=30><br> <p><input type=submit name="submit" value="Insert Record"></input> </FORM> </body> </html> insert_word.php <?php // Open the data base connection $conn = mysql_connect("localhost","root",""); mysql_select_db("test",$conn); $sql = "INSERT INTO test2 values ('','$_POST[word]','$_POST[definition]','$_POST[chapter]','$_POST[section]')"; if(mysql_query($sql, $conn)) { echo "record added"; } else { echo "Error in entering the record"; } ?> So I would like to thank everyone for their help. You've certainly got me on the right track, and I figured I should try and keep front and back end stuff seperate where possible. This is the first step of the system I'm writing, so I'm sure you're going to see some more posts from me. I'm looking forward to being part of the community. Thanks once again Pluto Quote Link to comment https://forums.phpfreaks.com/topic/137254-solved-inserting-a-record-in-to-a-mysql-data-base/#findComment-717893 Share on other sites More sharing options...
Maq Posted December 17, 2008 Share Posted December 17, 2008 Glad everything is working now. I think the design phrase you were looking for is MVC. Quote Link to comment https://forums.phpfreaks.com/topic/137254-solved-inserting-a-record-in-to-a-mysql-data-base/#findComment-717896 Share on other sites More sharing options...
pluto Posted December 17, 2008 Author Share Posted December 17, 2008 Thanks Maq, Now to check if the word is already in the data base... fun... Pluto Quote Link to comment https://forums.phpfreaks.com/topic/137254-solved-inserting-a-record-in-to-a-mysql-data-base/#findComment-717908 Share on other sites More sharing options...
Maq Posted December 17, 2008 Share Posted December 17, 2008 Thanks Maq, Now to check if the word is already in the data base... fun... Pluto You should create a new thread for this particular problem. Quote Link to comment https://forums.phpfreaks.com/topic/137254-solved-inserting-a-record-in-to-a-mysql-data-base/#findComment-717913 Share on other sites More sharing options...
pluto Posted December 17, 2008 Author Share Posted December 17, 2008 Thanks Maq, Now to check if the word is already in the data base... fun... Pluto You should create a new thread for this particular problem. I was going to have a stab at it myself first before posting. I've gotta learn to do it, or at least have a go, or I'll learn nothing, and it'll be like others writing the code for me, and that's not good. But if it comes to it, there will be another thread. Quote Link to comment https://forums.phpfreaks.com/topic/137254-solved-inserting-a-record-in-to-a-mysql-data-base/#findComment-717918 Share on other sites More sharing options...
Maq Posted December 17, 2008 Share Posted December 17, 2008 Thanks Maq, Now to check if the word is already in the data base... fun... Pluto You should create a new thread for this particular problem. I was going to have a stab at it myself first before posting. I've gotta learn to do it, or at least have a go, or I'll learn nothing, and it'll be like others writing the code for me, and that's not good. But if it comes to it, there will be another thread. Yes, I definitely agree with you. That's a good mindset to have when you are learning to program. What I really meant was, if you have a problem/issue/concern whatever, start a new thread for it. Quote Link to comment https://forums.phpfreaks.com/topic/137254-solved-inserting-a-record-in-to-a-mysql-data-base/#findComment-717926 Share on other sites More sharing options...
pluto Posted December 17, 2008 Author Share Posted December 17, 2008 Ah ok Maq, Sorry, my bad. You can mark this as solved if you wish? Or is that something I do? Quote Link to comment https://forums.phpfreaks.com/topic/137254-solved-inserting-a-record-in-to-a-mysql-data-base/#findComment-717931 Share on other sites More sharing options...
Maq Posted December 17, 2008 Share Posted December 17, 2008 Something you do. Bottom right tab, I think ??? Quote Link to comment https://forums.phpfreaks.com/topic/137254-solved-inserting-a-record-in-to-a-mysql-data-base/#findComment-717935 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.