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
https://forums.phpfreaks.com/topic/166336-solved-just-dont-get-this/
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);
    }
}

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

$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);
    }
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.