Jump to content

inserting form data into mysql db: can't find the mistake


rysio

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.