Board index   FAQ   Search  
Register  Login
Board index php forum :: php and mysql Web Hosting Hosting - Shared and Dedicated server Solutions

newb help question: php mysql comment box?

Where may I host my php/mysql web site ?

Moderators: macek, egami, gesf

newb help question: php mysql comment box?

Postby WebDeveloperNewb » Fri Jan 13, 2012 1:37 pm

hi,


i found the code below (i also uploaded the files in the attached "php_mysql_help.zip") . I altered it a little , but even before i edited it i got these error messages :

"Notice: Undefined index: comment in C:\xampp\htdocs\0_test_delete\comment.php on line 5

Notice: Undefined index: submit in C:\xampp\htdocs\0_test_delete\comment.php on line 6

Notice: Undefined variable: dname in C:\xampp\htdocs\0_test_delete\comment.php on line 40" .

I'm still in the infancy stage of .php & mysql , if you have some suggestion please respond to me as if i am a complete imbecile & don't know anything. How do I fix it ?









Code: Select all
<?php /*?>file name "comment.php"<?php */?>
<?php
require('connect.php');


$comment=$_POST['comment'];
$submit=$_POST['submit'];
if($submit)
{
    if($comment)
    {
    $query=mysql_query("INSERT INTO comment (id,comment) VALUES ('','$comment')");
    header("Location: success.php");
    }
    else
    {
        echo "ADD DATA";
    }
}
?>
<html>
<head><title></title></head>
<body>
<form action="comment.php" method="POST">
 </label><br /><br /><br />
<label>Comment:  </label><br /><textarea name="comment" cols="25" rows="7"></textarea><br /><br /><br />
<input type="submit" name="submit" value="Comment" /><br />

</form>
<hr width="1100px" size="5px" />
</body>
<?php
require('connect.php');
$query=mysql_query("SELECT * FROM comment ORDER BY id DESC");
while($rows=mysql_fetch_assoc($query))
{
     $id=$rows['id'];
     
     $dcomment=$rows['comment'];
     $linkdel="<a href=\"delete.php?id=" . $rows['id'] . "\">Delete User</a>";
     echo $dname . '<br />' . '<br />' . '<font color="red">Comments:</font>  ' . '<br />' . $dcomment . '&nbsp' . '&nbsp' .
      '&nbsp' . '&nbsp' .  $linkdel . '<br />' . '<br />' .
     '<hr size="5px" width="500px" color="blue" />'  ;   
}
?>
</html>

Code: Select all
<?php /*?>file name "connect.php"<?php */?>
<?php
$connect=mysql_connect("localhost","root","") or die('mysql_error()');
$database=mysql_select_db("comment") or die('mysql_error()');
?>

Code: Select all

<?php /*?>file name "delete.php"<?php */?>


<?php
require('connect.php');
$deleteid=$_GET['id'];
mysql_query("DELETE FROM comment WHERE id='$deleteid'");
header("location: success.php");
?>






Code: Select all

<?php /*?>file name "success.php"<?php */?>
<?php
header("Location: comment.php");
?>





Code: Select all



--
-- Host: localhost
-- Generation Time: May 07, 2011 at 03:27 PM
-- Server version: 5.1.53
-- PHP Version: 5.3.4

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `comment`
--

-- --------------------------------------------------------

--
-- Table structure for table `comment`
--

CREATE TABLE IF NOT EXISTS `comment` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  `comment` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

--
-- Dumping data for table `comment`
--

INSERT INTO `comment` (`id`, `name`, `comment`) VALUES











THANKS FOR YOUR TIME !
Attachments
php_mysql_help.zip
MY .php mysql files
(2.04 KiB) Downloaded 110 times
WebDeveloperNewb
New php-forum User
New php-forum User
 
Posts: 1
Joined: Fri Jan 13, 2012 12:52 pm

Re: newb help question: php mysql comment box?

Postby L33R » Wed Jan 18, 2012 9:06 am

They are notices telling you that they haven't been set anywhere. When you first run comment.php there is no such thing as $_POST['comment']. that will be initialized when you click submit on the form.

The variable $dname just isnt getting set anywhere. This needs to be inserted into the database with the comment.

change this

Code: Select all
<?php /*?>file name "comment.php"<?php */?>
<?php
require('connect.php');


$comment=$_POST['comment'];
$submit=$_POST['submit'];
if($submit)
{
    if($comment)
    {
    $query=mysql_query("INSERT INTO comment (id,comment) VALUES ('','$comment')");
    header("Location: success.php");
    }
    else
    {
        echo "ADD DATA";
    }
}
?>
<html>
<head><title></title></head>
<body>
<form action="comment.php" method="POST">
 </label><br /><br /><br />
<label>Comment:  </label><br /><textarea name="comment" cols="25" rows="7"></textarea><br /><br /><br />
<input type="submit" name="submit" value="Comment" /><br />

</form>
<hr width="1100px" size="5px" />
</body>
<?php
require('connect.php');
$query=mysql_query("SELECT * FROM comment ORDER BY id DESC");
while($rows=mysql_fetch_assoc($query))
{
     $id=$rows['id'];
     
     $dcomment=$rows['comment'];
     $linkdel="<a href=\"delete.php?id=" . $rows['id'] . "\">Delete User</a>";
     echo $dname . '<br />' . '<br />' . '<font color="red">Comments:</font>  ' . '<br />' . $dcomment . '&nbsp' . '&nbsp' .
      '&nbsp' . '&nbsp' .  $linkdel . '<br />' . '<br />' .
     '<hr size="5px" width="500px" color="blue" />'  ;   
}
?>
</html>



(I have my id set to AUTO_INCREMENT so there is no need for it to be INSERTED)
you will have to check your settings in your database.

to something like this

Code: Select all
<?php
   require_once('connect.php');
   if (isset($_POST['comment'])) {
      $sql = "INSERT INTO comment (name,comment) VALUES ('{$_POST['name']}','{$_POST['comment']}');";
      mysql_query($sql) or die("error with insert " . mysql_error());
      
      header("Location: success.php");
      exit;
    }
      else
    {
        echo "No Comments set";
    }

?>
<html>
<head><title></title></head>
<body>
<form action="selfSubmit.php" method="POST">
 </label><br /><br /><br />
<label>Comment:  </label><br /><textarea name="comment" cols="25" rows="7"></textarea><br /><br /><br />
<label>Name:</label> <input type="text" name="name" size="30"/><br />
<input type="submit" name="submit" value="Comment" /><br />

</form>
<hr width="1100px" size="5px" />
</body>
<?php
equire('connect.php');
$query=mysql_query("SELECT * FROM comment ORDER BY id DESC");
while($rows=mysql_fetch_assoc($query))
{
     $id=$rows['id'];
     
     $dcomment=$rows['comment'];
    $dname = $rows['name'];
     $linkdel="<a href=\"delete.php?id=" . $rows['id'] . "\">Delete User</a>";
     echo $dname . '<br />' . '<br />' . '<font color="red">Comments:</font>  ' . '<br />' . $dcomment . '&nbsp' . '&nbsp' .
      '&nbsp' . '&nbsp' .  $linkdel . '<br />' . '<br />' .
     '<hr size="5px" width="500px" color="blue" />'  ;   
}
?>
</html>


You also need to be looking at Mysql_real_escape_string(); etc... for inputting into sql.

you may need to play around with it a bit.
User avatar
L33R
New php-forum User
New php-forum User
 
Posts: 132
Joined: Fri Dec 30, 2011 4:27 am
Location: Liverpool


Return to Hosting - Shared and Dedicated server Solutions

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.