Jump to content

Recommended Posts

Hi all,
I'm fairly new with PHP/HTML and I'm working on implementing a simple comment field for a website I'm designing. The issue I'm having is that the comment field seems to only display integers, and not strings. Other then not displaying words, I'm not getting any errors. The database seems to be working fine, however it's only saving Integers, and not Strings. I've checked the columns and the one which is holding the comments is using type "text", and the ID is "int". I don't believe the issue is with the table, but rather somewhere with the PHP.
 

Here is the code below:

 

 

<?php
02 mysql_connect("localhost","root","changeme");
03 mysql_select_db("certestdb");
04 $comment=$_POST['comment'];
05 $submit=$_POST['submit'];
06  
07  
08 $dbLink = mysql_connect("localhost", "root", "changeme");
09 mysql_query("SET character_set_client=utf8", $dbLink);
10 mysql_query("SET character_set_connection=utf8", $dbLink);
11  
12  
13  
14 if (!empty($_POST)) {
15     //$comment=$_POST['comment'];
16     //$submit=$_POST['submit'];
17  
18  
19         if($submit) {
20             if($comment) {
21                 $insert=mysql_query("INSERT INTO comment (comment) VALUES ($comment) ");
22             } else {
23                 echo "please fill out all fields";
24             }
25         }
26 }       
27  
28 ?>
29  
30 <html>
31 <head>
32 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
33 <title>Comment box</title>
34 </head>
35 <div style="width:100px; height:100px; margin: 2 auto;">
36 <body>
37 <center>
38 <form name="comment.php" method="post" action="">
39 <table>           
40 <tr><td colspan="2">Comments: </td></tr>
41 <tr><td colspan="5"><textarea name="comment" rows="5" cols="150"></textarea></td></tr>
42 <tr><td colspan="2"><input type="submit" name="submit" value="Comment"></td></tr>
43 </table>
44 </form>
45  
46 <?php
47 $dbLink = mysql_connect("localhost", "root", "changeme");
48 mysql_query("SET character_set_results=utf8", $dbLink);
49  
50  
51  
52 $getquery=mysql_query("SELECT * FROM comment ORDER BY id DESC");
53 while($rows=mysql_fetch_array($getquery)) {
54     $id=$rows['id'];
55     $comment=$rows['comment'];
56     echo $comment . '<br/>' . '<br/>' . '<hr size="1"/>';
57 }
58  
59 ?>
60  
61 </body>
62 </html>

 

 

If anyone might know what is causing this and could help, it would be much appreciated!
Thanks!
 

Link to comment
https://forums.phpfreaks.com/topic/287107-issue-with-simple-comment-box/
Share on other sites

mysql_select_db extension should be set after mysql_connect().

mysql_connect("localhost","root","changeme");
mysql_select_db("certestdb"); 
$dbLink = mysql_connect("localhost", "root", "changeme");

You don't have to open more than one connection to database server and using mysql_error() function to debug your queries.

All mysql_* extensions are deprecated as of PHP 5.5.0, and will be removed in the future, instead you should consider using PDO or MySQLi libraries.

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.