rysio Posted December 21, 2007 Share Posted December 21, 2007 hi, this is what i came up with trying to make a form which will insert data into the mysql db i'd created: <html> <head> <meta http-equiv="Content-Type" CONTENT="text/html: charset=iso-8859-2"> <title>czytanie z bazy</title> <link rel="stylesheet" type="text/css" href="styl.css"> </head> <body> <table cellpadding="20" cellspacing="0" class="szara"><tr><td> <? mysql_connect ("localhost", "root", "krasnal") or die ("nie mozna polaczyc sie z mysql"); mysql_select_db ("cwphp") or die ("nie mozna polaczyc sie z baza cwphp"); if ($_POST['co'] == 'dodaj') { if ($_POST['user'] && $_POST['ip'] && $_POST['comment'] && $_POST['data'] && $_POST['group_id']) { $query = "INSERT INTO comments (user, ip, comment, e-mail, strona_www, data, "; $query .= "group_id) VALUES ('".$_POST['user']."', '".$_POST['ip']."', '".$_POST['comment']."', '".$_POST['e-mail']."', '".$_POST['strona_www']."', '".$_POST['data']."', '".$_POST['group_id']."');"; $wynik = mysql_query ($query); } } $wynik = mysql_query ("SELECT * FROM comments;") or die ("blad w pytaniu"); print "<table class=\"szara\">"; print "<tr><td><b>guest book</b></td></tr>\n"; while ($rekord = mysql_fetch_assoc ($wynik)) { $user = $rekord['user']; $comment = $rekord['comment']; $data = $rekord['data']; $ip = $_SERVER['REMOTE_ADDR']; $group_id = $rekord['group_id']; $today = date("d.m.y H:i:s"); print "<tr><td>$user</td><td>$comment</td><td>$data</td></tr>\n"; } print "</table>"; ?> <div align="center"><br /> <form method="post"> <input type="hidden" name="co" value="dodaj"><br /> nick: <input name="user" type="text" class="wpisz" size="32"><br /><br /> e-mail: <input name="e-mail" type="text" class="wpisz" size="30"><br /><br /> strona: <input name="strona_www" type="text" class="wpisz" value="http://" size="30"><br /> twój komentarz:<br /> <textarea name="comment" cols="30" rows="5" class="prawa"></textarea><br /><br /> <input type="hidden" name="ip" value="<? $ip; ?>"> <input type="hidden" name="group_id" value="<? $group_id; ?>"> <input type="hidden" name="data" value="<? $today; ?>"> <input type="submit" class="zapisz" value="zapisz"><br /><br /> </form></div> </td></tr> </table> </body> </html> what it does is read from db fine, cause having added records manually i can see results all right, but the form doesn't insert the data to the db as i thought i had it all figured out can you see what might be a problem? Quote Link to comment https://forums.phpfreaks.com/topic/82707-inserting-form-data-into-mysql-db-cant-find-the-mistake/ Share on other sites More sharing options...
fenway Posted December 21, 2007 Share Posted December 21, 2007 Echo $query. Quote Link to comment https://forums.phpfreaks.com/topic/82707-inserting-form-data-into-mysql-db-cant-find-the-mistake/#findComment-420661 Share on other sites More sharing options...
rysio Posted December 21, 2007 Author Share Posted December 21, 2007 should i understand it? i am trully sorry, but i am a total amateur, that is why i came here. i really don't understand what to do with this reply. could you please be a little more specific? p.s. yes - i did try to put the echos before the $queries, and it didn't work... which $query did you mean? i really don't get it. Quote Link to comment https://forums.phpfreaks.com/topic/82707-inserting-form-data-into-mysql-db-cant-find-the-mistake/#findComment-420682 Share on other sites More sharing options...
fenway Posted December 21, 2007 Share Posted December 21, 2007 There's only one $query variable in your entire code block. Quote Link to comment https://forums.phpfreaks.com/topic/82707-inserting-form-data-into-mysql-db-cant-find-the-mistake/#findComment-420683 Share on other sites More sharing options...
rysio Posted December 22, 2007 Author Share Posted December 22, 2007 if you say so, there must be only one. i take your word for it, but i do see it appear three times physically, and really am an amateur, snd do not know which place is the best to insert the echo. i have tried several versions adding echo to all the $query's i can see there, but no one worked. Quote Link to comment https://forums.phpfreaks.com/topic/82707-inserting-form-data-into-mysql-db-cant-find-the-mistake/#findComment-421150 Share on other sites More sharing options...
revraz Posted December 22, 2007 Share Posted December 22, 2007 Where are the 2 others besides this one? $query = "INSERT INTO comments (user, ip, comment, e-mail, strona_www, data, "; $query .= "group_id) VALUES ('".$_POST['user']."', '".$_POST['ip']."', '".$_POST['comment']."', '".$_POST['e-mail']."', '".$_POST['strona_www']."', '".$_POST['data']."', '".$_POST['group_id']."');"; If you notice the .= That means it's appending to the $query above it. So after those two lines, insert echo $query; $query = "INSERT INTO comments (user, ip, comment, e-mail, strona_www, data, "; $query .= "group_id) VALUES ('".$_POST['user']."', '".$_POST['ip']."', '".$_POST['comment']."', '".$_POST['e-mail']."', '".$_POST['strona_www']."', '".$_POST['data']."', '".$_POST['group_id']."');"; echo $query; Quote Link to comment https://forums.phpfreaks.com/topic/82707-inserting-form-data-into-mysql-db-cant-find-the-mistake/#findComment-421257 Share on other sites More sharing options...
rysio Posted December 26, 2007 Author Share Posted December 26, 2007 umm, well still nothing. any other thing that is wrong with it? Quote Link to comment https://forums.phpfreaks.com/topic/82707-inserting-form-data-into-mysql-db-cant-find-the-mistake/#findComment-423662 Share on other sites More sharing options...
revraz Posted December 26, 2007 Share Posted December 26, 2007 Not sure what you mean by "still nothing". Are you saying echo $query does not show you the sql statement? Quote Link to comment https://forums.phpfreaks.com/topic/82707-inserting-form-data-into-mysql-db-cant-find-the-mistake/#findComment-423670 Share on other sites More sharing options...
fenway Posted December 27, 2007 Share Posted December 27, 2007 umm, well still nothing. any other thing that is wrong with it? Yes, echo can't do "nothing". Quote Link to comment https://forums.phpfreaks.com/topic/82707-inserting-form-data-into-mysql-db-cant-find-the-mistake/#findComment-424090 Share on other sites More sharing options...
rysio Posted December 28, 2007 Author Share Posted December 28, 2007 what i'm saying is that this script doesn't insert data into the database. i don't know what that echo was supposed to do. anyway adding the echo where showed didn't change what the script does. it still doesn't write the data into the db. Quote Link to comment https://forums.phpfreaks.com/topic/82707-inserting-form-data-into-mysql-db-cant-find-the-mistake/#findComment-424581 Share on other sites More sharing options...
revraz Posted December 28, 2007 Share Posted December 28, 2007 The echo will output your SQL Query to your browser, so you can look at it and see if it's doing what you expect. Quote Link to comment https://forums.phpfreaks.com/topic/82707-inserting-form-data-into-mysql-db-cant-find-the-mistake/#findComment-424650 Share on other sites More sharing options...
fenway Posted December 28, 2007 Share Posted December 28, 2007 what i'm saying is that this script doesn't insert data into the database. i don't know what that echo was supposed to do. anyway adding the echo where showed didn't change what the script does. it still doesn't write the data into the db. I really hope you didn't think the echo would... it just text to the screen. Quote Link to comment https://forums.phpfreaks.com/topic/82707-inserting-form-data-into-mysql-db-cant-find-the-mistake/#findComment-424716 Share on other sites More sharing options...
rysio Posted January 4, 2008 Author Share Posted January 4, 2008 what i'm saying is that this script doesn't insert data into the database. i don't know what that echo was supposed to do. anyway adding the echo where showed didn't change what the script does. it still doesn't write the data into the db. I really hope you didn't think the echo would... it just text to the screen. i try no to think too much when i do what you tell me to do. it did cross my mind that "echo" has little to do with inserting data into the db, but being so amateur in the subject as i am i try to refrain from judging, and since there has been very little progress with this little project of mine lately i just give anything you will say a go, and see what happens. and because my original question concerned inserting data into a mysql db i thought what you suggested might help. i really do not know a lot on the subject, even studying from the books on php i have at home is difficult. it seems to me i have done everything as instructed, but anyway it doesn't work. so you see: having followed the instructions from the php manual i have at home didn't give me the results i was hjoping. that is why i am here asking experts. please do understand that i am simply trying to complete this simple (one could think) task. i did start this as an attempt to learn php, but this seems so hard with finding myself in so many dead ends so often. still i do not give up. do you know the feeling? well that's it for whining over my insufficient skills. back to the question: how will it help me to see the text on the screen? (when echo does its job) Quote Link to comment https://forums.phpfreaks.com/topic/82707-inserting-form-data-into-mysql-db-cant-find-the-mistake/#findComment-430306 Share on other sites More sharing options...
fenway Posted January 4, 2008 Share Posted January 4, 2008 You're running a query -- it doesn't work. You don't have a "or die(mysql_error())" so you can't see what mysql is complaining about. The least you can do is show *us* the query so that we can tell you what might be wrong. Without echo, that's impossible. Quote Link to comment https://forums.phpfreaks.com/topic/82707-inserting-form-data-into-mysql-db-cant-find-the-mistake/#findComment-430379 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.