I am having trouble trying to update my comment, well I also don't know why my table isn't displaying on my updatecomment.php page. How do you delete a comment too. If you see any other errors please feel free to fix it.

Thanks in advance

<!-- start dbconfig.inc.php page -->
<?php
$dbserver = '---';
$dbusername = '---';
$dbpassword = '---';
$dbdatabase = '---';
?>
<!-- end dbconfig.inc.php page -->

<!-- start dbconnect.inc.php page -->
<?php 
require_once("dbconfig.inc.php");

$link = mysql_connect($dbserver, $dbusername, $dbpassword);
if (!$link) {
    die('Could not connect to MySQL: ' . mysql_error());
}

mysql_select_db($dbdatabase, $link);
?>
<!-- end dbconnect.inc.php page -->

<!-- start dbdisconnect.inc.php page -->
<?php
mysqli: mysql_close($link); 
?>
<!-- end dbdisconnect.inc.php page -->

<!-- start index.php page -->
<html>
<head>
<title>
My Guestbook - index page
</title>
</head>
<body>
<h1>My Guestbook</h1>
<h2>Recent Comments</h2>

<script type="text/javascript">
function update_me(obj) {
 var selected_form = obj.form;
 var id = selected_form.id.value;
 selected_form.action = 'updatecomment.php';
 selected_form.submit();
}

function delete_me(obj) {
 var selected_form = obj.form;
 var id = selected_form.id.value;
 selected_form.action = 'deletecomment.php';
 selected_form.submit();
}
</script>


<?php

    date_default_timezone_set('America/New_York'); 
    $today = date("Y-n-j H:i:s");

    require('dbconnect.inc.php');
        $entriesq = mysql_query("SELECT body, author_name FROM `Posts` LIMIT 0, 10 ") or die(mysql_error());  

            while($entriesr = mysql_fetch_array($entriesq)){
                $author_name = $entriesr["author_name"];
                $post_body = $entriesr["body"];

              echo '<form method="post" >';
                echo '<table>';
                    echo '<tr>';
                        echo '<td>';
                            echo '<input type="hidden" name="id" value="$id" />';
                            echo '<input type="button" name="Update" onClick="update_me(this)" value="Update" />';
                            echo '<input type="button" name="Delete" onClick="delete_me(this)" value="Delete" />';
                            echo " On $today $author_name said: $post_body";
                        echo '</td>';
                    echo '</tr>';
                echo '</table>';
            echo '</form>';
              }

    require('dbdisconnect.inc.php');
?>

<br>
<a href="addcomment.php">Add a comment</a>
</body>
</html>
<!--end index.php page -->

<!-- start addcomment.php page -->
<html>
<head>
<title>
    My Guestbook - Add Comment
</title>
</head>
<body>

<?php

    if(isset($_POST['submit']))
    {
            $body = $_POST['post_body'];
            $author_name = $_POST['author_name'];

        require('dbconnect.inc.php');

            $body = mysql_real_escape_string(htmlspecialchars($_POST['post_body']));
            $author_name = mysql_real_escape_string(htmlspecialchars($_POST['author_name']));

            $sql = "INSERT INTO `Posts` (id,body,post_date,author_name) VALUES ('', '$body', '', '$author_name')";

            $result = mysql_query($sql);

        require('dbdisconnect.inc.php');
    }
?>

<h1>Add a New Comment</h1>
<form action="addcomment.php" method="post">
    <table>
        <tr>
            <td>
                Name:
            </td>
            <td>
                <input type="text" name="author_name" value="">
            </td>
        </tr>
        <tr>
            <td>
                Message:
            </td>
            <td>
                <textarea name="post_body">

Recommended Answers

All 19 Replies

Member Avatar for LastMitch

@wackyal

I am having trouble trying to update my comment, well I also don't know why my table isn't displaying on my updatecomment.php page. How do you delete a comment too. If you see any other errors please feel free to fix it.

When you run the query was there any errors? If so what line?

I get this error: The website cannot display the page (HTTP 500 Internal Server Error)

It is working now, where I would be able to see errors but there are no errors displayed. The only thing that is showing is the "Add Comment" link from the updatecomment.php which would be the html code, not the php code.

Member Avatar for LastMitch

@wackyal

It is working now, where I would be able to see errors but there are no errors displayed

Did you fill out this:

<!-- start dbconfig.inc.php page -->
<?php
$dbserver = '---';
$dbusername = '---';
$dbpassword = '---';
$dbdatabase = '---';
?>
<!-- end dbconfig.inc.php page -->
<!-- start dbconnect.inc.php page -->
<?php 
require_once("dbconfig.inc.php");
$link = mysql_connect($dbserver, $dbusername, $dbpassword);
if (!$link) {
    die('Could not connect to MySQL: ' . mysql_error());
}

Another words do you have a database to store your data?

If you didn't fill in the info then my suggestion is to fill it in.

Did you create a table in the database? If not create one too.

Once you do that, run the query again. Then tell me what errors you get.

hi,
did you fill into this fields?

<?php
$dbserver = '---';
$dbusername = '---';
$dbpassword = '---';
$dbdatabase = '---';
?>

Yes I have already filled in this information:

<!-- start dbconfig.inc.php page -->
<?php
$dbserver = '---';
$dbusername = '---';
$dbpassword = '---';
$dbdatabase = '---';
?>
<!-- end dbconfig.inc.php page -->
<!-- start dbconnect.inc.php page -->
<?php 
require_once("dbconfig.inc.php");
$link = mysql_connect($dbserver, $dbusername, $dbpassword);
if (!$link) {
die('Could not connect to MySQL: ' . mysql_error());
}

I just did not want everybody to know what my username and password was. Do you need that information to figure out the code?

Member Avatar for LastMitch

@wackyal

I just did not want everybody to know what my username and password was. Do you need that information to figure out the code?

No, you don't need to post your username and password and other stuff. I just need to know if you fill it out yet.

Did you create a table?

Member Avatar for LastMitch

@wackyal

I realized something is this your whole code you are missing a </form>

This wrong:

<form action="addcomment.php" method="post">
<table>
<tr>
<td>
Name:
</td>
<td>
<input type="text" name="author_name" value="">
</td>
</tr>
<tr>
<td>
Message:
</td>
<td>
<textarea name="post_body">

Also take this off the qoute for Posts

Instead of this:

$entriesq = mysql_query("SELECT body, author_name FROM 'Posts' LIMIT 0, 10 ") or die(mysql_error());

Try this

$entriesq = mysql_query("SELECT body, author_name FROM Posts LIMIT 0, 10 ") or die(mysql_error());

Here is my code that I would like checked:

<html>
<head>
<title>
    My Guestbook - Update Comment
</title>
</head>
<body>

<?php
if (isset($_POST['submit'])) {
$id=($_POST['id']);
$author_name=($_POST['author_name']);
$body=($_POST['post_body']);

require('dbconnect.inc.php');
$body = mysql_real_escape_string(htmlspecialchars($_POST['post_body']));
$author_name = mysql_real_escape_string(htmlspecialchars($_POST['author_name']));
$query1 = "UPDATE Posts SET author_name='$author_name' , body='$body' WHERE id = '$id'";
mysql_query($query1) or die('Error, query failed');


require('dbdisconnect.inc.php');

echo 'The comment was updated.';
};  

?>
<?php
require('dbconnect.inc.php');
$query2="SELECT id, author_name, body FROM Posts WHERE id='$id'";
$results=mysql_query($query2) or die('Error, query failed');
while ($row=mysql_fetch_assoc($result))
{
    $row = $id['id'];
    $row = $author_name['author_name'];
    $row = $post_body['body'];

echo 
"<h1>Update Comment</h1>
<form action=\"updatecomment.php\" method=\"post\">
<table>
<tr>
<td><input type=\"hidden\" name=\"id\" value=\"{htmlspecialchars($row[\"id\"])}\"></td>
</tr>
<tr>
<td>Name:</td>
<td><input type=\"text\" name=\"author_name\" value=\"{htmlspecialchars($row[\"author_name\"])}\"></td>
</tr>
<tr>
<td>Message:</td>
<td><textarea name=\"body\" value=\"{htmlspecialchars($row[\"body\"])}\">{htmlspecialchars($row[\"body\"])}</textarea></td>
</tr>
</table>
<input type=\"submit\" name=\"submit\" value=\"Update\">
</form>";
}
require('dbdisconnect.inc.php');
?>
<a href="index.php">Back to Comments</a>
</body>


</html>
Member Avatar for LastMitch

@wackyal

Apparently you post a different code. So the new code that you posted is the one that has an issue?

When you run the code what errors you get?

Did you create a table Posts? If so post the table.

this is the new code that that has the issue
I do not get any errors
what do you mean by "Did you create a table Posts?"

Member Avatar for LastMitch

@wackyal

what do you mean by "Did you create a table Posts?"

This is your query:

SELECT id, author_name, body FROM Posts WHERE id='$id'";

Posts is your table. Did you create a table called Posts?

If you didn't created a table called Posts then how can you stored any data?

Member Avatar for LastMitch

@wackyal

You didn't answer my question.

Did you create a table called Posts?

It's not updating and deleting because your query is not working.

I feel you didn't create a table Posts.

Here is my table called Posts php_table_Posts

Member Avatar for LastMitch

@wackyal

Why do you have to different query when you should have 1 query!

This is wrong:

    $query1 = "UPDATE Posts SET author_name='$author_name' , body='$body' WHERE id = '$id'";
    mysql_query($query1) or die('Error, query failed');


$query2="SELECT id, author_name, body FROM Posts WHERE id='$id'";
$results=mysql_query($query2) or die('Error, query failed');

You need to correct it.

Read this and look closely how a queryshould be used:

http://www.daniweb.com/web-development/php/code/434415/using-phpmysql-with-error-checking

it doesn't have to do with the query because whenever I do get an error message it is coming from the query2. There is something wrong with the while loop but I just can't figure it out

Member Avatar for LastMitch

it doesn't have to do with the query because whenever I do get an error message it is coming from the query2. There is something wrong with the while loop but I just can't figure it out

So there is something wrong with the query. You said there was no errors but now you said there's something is wrong? Did you write this code? I find it very strange that you post 2 different codes and ask the same question?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.