tony_leic Posted November 6, 2007 Share Posted November 6, 2007 First, just to say hello to everyone I am brand new to php so getting a bit mixed up. i am trying to do something most of you might find real simple i have set my database up correct and am trying to add a bit of info to it, read around the net but can't find my problem, can you help? this is my html form <html><body> <h4>test</h4> <form action="process.php" method="post"> Message: <input message="message" type="text" /> <input type="submit" /> </form> </body></html> this is my process php <?php // Open database connection mysql_connect("localhost", "*********", "**********") or die(mysql_error()); mysql_select_db("*******8") or die(mysql_error()); $name = $_POST['message']; // Insert data $query = "INSERT INTO friends (name) VALUES ('$name',)"; mysql_query($query); print "updated"; ?> can you see where i have gone wrong as its only showing the data i added when i set the database up? thanks for your help! Tony Quote Link to comment Share on other sites More sharing options...
atlanta Posted November 6, 2007 Share Posted November 6, 2007 <html><body> <h4>test</h4> <form action="process.php" method="post"> Message: <input name="message" type="text" /> <input type="submit" /> </form> </body></html> you had message="message" needed to be name="message" also <?php // Open database connection mysql_connect("localhost", "*********", "**********") or die(mysql_error()); mysql_select_db("*******8") or die(mysql_error()); $name = mysql_real_escape_string($_POST['message']); // Insert data $query = "INSERT INTO friends (name) VALUES ('$name')"; mysql_query($query) or die(mysql_error()); print "updated"; ?> Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted November 6, 2007 Share Posted November 6, 2007 You have a comma after your $name var in the query, shouldn't be there. You should also you mysql_real_escape_string() on all variables before insertion. Lastly, you should put a die error at the end of the query to catch errors. <?php // Open database connection mysql_connect("localhost", "*********", "**********") or die(mysql_error()); mysql_select_db("*******8") or die(mysql_error()); $name = mysql_real_escape_string($_POST['message']); // Insert data $query = "INSERT INTO friends (name) VALUES ('$name')"; $result = mysql_query($query)or die(mysql_error()); print "updated"; ?> Quote Link to comment Share on other sites More sharing options...
tony_leic Posted November 6, 2007 Author Share Posted November 6, 2007 wow fast! so now i have got html = <html><body> <h4>test</h4> <form action="process.php" method="post"> Message: <input message="message" type="text" /> <input type="submit" /> </form> </body></html> php = <?php mysql_connect("localhost", "****", "****") or die(mysql_error()); mysql_select_db("****") or die(mysql_error()); $name = mysql_real_escape_string($_POST['message']); // Insert data $query = "INSERT INTO friends (name) VALUES ('$name')"; $result = mysql_query($query)or die(mysql_error()); print "updated"; ?> but still don't work! think i messed something up? Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted November 6, 2007 Share Posted November 6, 2007 You still didn't change this line Message: <input message="message" type="text" /> To Message: <input name="message" type="text" /> Quote Link to comment Share on other sites More sharing options...
tony_leic Posted November 6, 2007 Author Share Posted November 6, 2007 oops sorry html is now <html><body> <h4>test</h4> <form action="process.php" method="post"> Message: <input name="message" type="text" /> <input type="submit" /> </form> </body></html> but still wont work i am just lost lol Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted November 6, 2007 Share Posted November 6, 2007 Are you sure the php page is called "process.php"? What exactly is happening, are you getting a blank page, error, or what? Quote Link to comment Share on other sites More sharing options...
tony_leic Posted November 6, 2007 Author Share Posted November 6, 2007 right.. hehe this is what i set my database up with CREATE TABLE friends (name VARCHAR(30), message VARCHAR(30)); INSERT INTO friends VALUES ( "123", "456") this is what i am reading it with <?php header( 'refresh: 5; url=******' ); // Connects to your Database mysql_connect("localhost", "*****", "*****") or die(mysql_error()); mysql_select_db("*****") or die(mysql_error()); // Collects data from "friends" table $data = mysql_query("SELECT * FROM friends") or die(mysql_error()); // puts the "friends" info into the $info array $info = mysql_fetch_array( $data ); // Print out the contents of the entry Print "<b>Message:</b> ".$info['name'] . " <br>"; ?> this is my html form as i have it now <html><body> <h4>test</h4> <form action="process.php" method="post"> Message: <input name="message" type="text" /> <input type="submit" /> </form> </body></html> and this is what i am trying to write to it with now <?php mysql_connect("localhost", "*****, "******") or die(mysql_error()); mysql_select_db("******") or die(mysql_error()); $name = mysql_real_escape_string($_POST['message']); // Insert data $query = "INSERT INTO friends (name) VALUES ('$name')"; $result = mysql_query($query)or die(mysql_error()); print "updated"; ?> and still not working lol maybe i should give up lol Tony Quote Link to comment Share on other sites More sharing options...
tony_leic Posted November 6, 2007 Author Share Posted November 6, 2007 Are you sure the php page is called "process.php"? What exactly is happening, are you getting a blank page, error, or what? Yep its called process.php but no, no error or blank i am uploading it to the server going to the page, writing something in the field, hit submit, it then goes to the php and says "updated" ok like at the bottom of the php file the thing i made to read it is auto refreshing every 5 seconds like it should but not showing the new data i just sent to the database. all i am doing is trying to learn and i thought just to be able to write then read from the database and have it show on the page would be good ........ but nooooooooooo my brain refuses to get around this :-\ Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted November 6, 2007 Share Posted November 6, 2007 Oh, okay...I see. Change the file you are reading it with to this <?php header( 'refresh: 5; url=******' ); // Connects to your Database mysql_connect("localhost", "*****", "*****") or die(mysql_error()); mysql_select_db("*****") or die(mysql_error()); // Collects data from "friends" table $data = mysql_query("SELECT * FROM friends") or die(mysql_error()); // puts the "friends" info into the $info array while ($info = mysql_fetch_array($data)){ // Print out the contents of the entry Print "Message: ".$info['name']."<p>"; } ?> Quote Link to comment Share on other sites More sharing options...
tony_leic Posted November 6, 2007 Author Share Posted November 6, 2007 ahaaaaaaaaaaaaaa now it works! that is what i wanted but couldn;t work it out! hehe Thanks so much both, hopefully one day i might be able to help someone with php.. will be a long time yet though lol Tony Quote Link to comment Share on other sites More sharing options...
tony_leic Posted November 6, 2007 Author Share Posted November 6, 2007 Can i ask though how come its now remembering all the previous entries ? i wanted it so it just replaces whats already in the database but instead its remembering ever message Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted November 6, 2007 Share Posted November 6, 2007 Then you will want an UPDATE query instead of an INSERT one. You are going to have to add an auto-increment field though, or else you won't know the condition to update the table by. So add a field into that table called "friendID" or soemething, and make it an auto-increment and primary key. Now change your code to this: <?php header( 'refresh: 5; url=******' ); // Connects to your Database mysql_connect("localhost", "*****", "*****") or die(mysql_error()); mysql_select_db("*****") or die(mysql_error()); // Collects data from "friends" table $data = mysql_query("SELECT * FROM friends ORDER BY friendID DESC") or die(mysql_error()); // puts the "friends" info into the $info array $info = mysql_fetch_assoc($data); Print "Message: ".$info['name']; ?> Quote Link to comment Share on other sites More sharing options...
tony_leic Posted November 6, 2007 Author Share Posted November 6, 2007 wow this is sooo much more complicated than i thought it would be lol thanks i will try that.. something else for me to try and get my head around :-\ Tony Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted November 6, 2007 Share Posted November 6, 2007 Don't worry, it's hard for everyone to grasp things like this at first. Just keep practicing your PHP and it will all start becoming second nature to you. I remember being completely confused about the simplest things at first. So just keep pushing and it will start to click. Quote Link to comment Share on other sites More sharing options...
tony_leic Posted November 6, 2007 Author Share Posted November 6, 2007 Thanks for that positive post think i need it, i thought maybe my learning capabilities where just slowing down I used to write quite advanced programs for the older pc's when younger like commodor, spectrum and amstrad, i can do so much html and i became an advanced scripter in SL but php omg! has to be the hardest one yet Quote Link to comment 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.