Jump to content

[SOLVED] Just dont get this?


onthespot

Recommended Posts

<?
$from1=$_SESSION['username'];
$to1=$_GET['news'];
$comment1=$_POST['comment'];

$query="INSERT INTO ".TBL_NEWSCOMMENTS." VALUES (NULL '$to1','$comment1', now(), $from1)";
if (!empty($_POST)) {
if(!$comment1 || strlen($comment1 = trim($comment1)) == 0)
    echo "Comment not entered";
else if(!$comment1 || strlen($comment1 = trim($comment1)) < 10)
    echo "Comment too short, must be 10 characters at least";
else if (!$comment1 || strlen($comment1 = trim($comment1)) > 10){
    echo "".$_SESSION['username'].", you have added a comment ";
mysql_query($query);}}
?>

<form action="" method="POST">
<textarea name="comment" rows="4" cols="66" ></textarea>
<div align="right"><input type="submit" name="submit" value="Add Comment"></div>
</form>
<br>

 

Can anyone see why this wouldn't work? Just doesn't add to the DB

Link to comment
Share on other sites

$to1=$_GET['news'];
if (empty($to1)) /*do something*/;

$from1=$_SESSION['username'];
if (!empty($_POST)) {
    $comment1=$_POST['comment'];
    if(!$comment1 || strlen($comment1 = trim($comment1)) == 0) {
        echo "Comment not entered";
    } else if(!$comment1 || strlen($comment1 = trim($comment1)) < 10) {
        echo "Comment too short, must be 10 characters at least";
    } else if (!$comment1 || strlen($comment1 = trim($comment1)) > 10) {
        echo "".$from1.", you have added a comment ";
        $query="INSERT INTO ".TBL_NEWSCOMMENTS." VALUES (NULL '$to1','$comment1', now(), $from1)";
        mysql_query($query);
    }
}

Link to comment
Share on other sites

ok so this is using two tables in one.

 

<?
$from1=$_SESSION['username'];
$to1=$_GET['news'];
$comment1=$_POST['comment'];

$query="INSERT INTO ".TBL_NEWSCOMMENTS." VALUES (NULL '$to1','$comment1', now(), $from1)";
if (!empty($_POST)) {
if(!$comment1 || strlen($comment1 = trim($comment1)) == 0)
    echo "Comment not entered";
else if(!$comment1 || strlen($comment1 = trim($comment1)) < 10)
    echo "Comment too short, must be 10 characters at least";
else if (!$comment1 || strlen($comment1 = trim($comment1)) > 10){
    echo "".$_SESSION['username'].", you have added a comment ";
mysql_query($query);}}
?>

<form action="" method="POST">
<textarea name="comment" rows="4" cols="66" ></textarea>
<div align="right"><input type="submit" name="submit" value="Add Comment"></div>
</form>
<br>

 

That is the code for the comment being entered into the newscomments table.

 

The newsid field in that table is a foreign key to a table called news.

The newsid field in table news is a primary key.

So as the URL displays news=newsid from the news table, GET is used to carry that over when I submit the form.

 

As you can see this isn't populating the table that looks like the following,

 

id | newsid | comment | date | user(that posts the comment)

 

Anyone got any idea? thanks

Link to comment
Share on other sites

$to1=$_GET['news'];
if (empty($to1)) /*do something*/;

$from1=$_SESSION['username'];
if (!empty($_POST)) {
    $comment1=$_POST['comment'];
    if (!$comment1) { //=strlen($comment1)==0
        echo "Comment not entered";
    // odd at first but this checks if their is a character at position 10 (array is 0-based) if their isn't $comment1 < 10 if their is it atleast contains 10 characters.
    } else if (!isset($comment1[9])) {
        echo "Comment too short, must be 10 characters at least";
    } else {
        echo "".$from1.", you have added a comment ";
        $query="INSERT INTO ".TBL_NEWSCOMMENTS." VALUES (NULL '$to1','$comment1', now(), $from1)";
        mysql_query($query);
    }
}

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.