Jump to content

Reply To Post ID Question


twilitegxa

Recommended Posts

  • Replies 57
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

 

 

lets do some error outputting then

<?php
if (!isset($_GET['id'])) {
    // id is not set, do something
}
elseif (!ctype_digit($_GET['id'])) {
    // the id is not a valid number, do something
}
else {
    $id = (int) $_GET['id'];
}?>

Link to comment
Share on other sites

Finally got it figured out:

 

<?php
$comment_id = $_GET['comment_id'];

//connect to server and select database
$conn = mysql_connect("localhost", "root", "")
    or die(mysql_error());
$db = mysql_select_db("testimonials", $conn) or die(mysql_error());

if (isset($_POST['submit'])) {

//create and issue the first query
$name=mysql_real_escape_string($_POST['comment_owner']);
$email=mysql_real_escape_string($_POST['comment_owner_email']); 
$reply=mysql_real_escape_string($_POST['reply']);
$comment_id = $_GET['comment_id'];

$error='';//initialize $error to blank
  if(trim($name)=='' ){
      $error.="Please enter your name!<br />"; //concatenate the $error Message with a line break
  }
   if(trim($email)==''){
    $error.="Plese enter your e-mail address!<br />";
    
  }  elseif (!preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $email)) {

        $error.="The e-mail you entered was not valid!<br />";
        
    }
    if(trim($reply)=='' ){
      $error.="Please enter your reply!<br />"; //concatenate the $error Message with a line break
  }
  if($error==''){


$sql="INSERT INTO replies (comment_id, reply, reply_create_time, reply_owner, reply_owner_email) VALUES ( '$comment_id', '$reply', now(), '$name','$email')"; 
  
mysql_query($sql,$conn) or die(mysql_error());

header('Location: testimonials.php');

  // mysql_query($sql,$conn) or die(mysql_error());
  }
    else{
       echo "<div class=error><span style=color:red>$error</span><br /></div>";
    }    
} else {

$name= '';
$email= '';
$reply= '';
}
?>
<form name="comment" id="comment" onsubmit="return validateFormOnSubmit(this)" action="reply.php?comment_id=<?php echo $comment_id; ?>" method="post">
<table border="0" cellspacing="0" cellpadding="5" width="662" class="style2">
<tr>
<td align="left"><label for="name"> Name:</label></td>
<td>
<div class="c2"><input type="text" name="comment_owner" id="comment_owner" size="30" value="<?php echo $name; ?>"/></div></td>
</tr>
<tr>
<td align="left"><label for="email">E-mail:</label></td>
<td>
<div class="c2"><input type="text" name="comment_owner_email" id="comment_owner_email" size="30" value="<?php echo $email; ?>"/></div></td>
</tr>
<tr>
<td align="left">
<label for="reply">Reply:</label></td>
<td>
<textarea name="reply" id="reply" rows="5" cols="30" value="<?php echo $reply; ?>"></textarea></td>
</tr>
<tr>
<td colspan="4">
<div class="c1"><input name="submit" type="submit" value="Submit" /> <input type="reset" name="reset" id="reset" value="Reset" /></div></td>
<td width="2"></td>
</tr>
</table>
</form>

 

Thanks for the help with this Hussam!

Link to comment
Share on other sites

okay guys, the problem is solved and everything was tested and its all working fine on the server.

 

I had to change the names of the fields a little bit, I like to follow the conventions.

reply_id in the replies table was changed to id

comment_id in the user_comments table was changed to id

 

both of them are auto increment and primary key's

 

Now the problem was:

the condition on top

if (isset($_POST['submit']))

this condition  makes the page instantiate the values AFTER the submission not before.

after the submission there is no GET values.

so I added the GET value to the action attribute of the form

therefore I have the GET values anyway, if the form is submitted or not.

and that won't be a problem after the submission because there is redirection anyway.

 

the code should be like this:

 

<?php
$comment_id = $_GET['comment_id'];
//
//echo $comment_id;
//
//exit();

//connect to server and select database
$conn = mysql_connect("localhost", "webdes17_twilite", "minimoon")
    or die(mysql_error());
$db = mysql_select_db("webdes17_testimonials", $conn) or die(mysql_error());

if (isset($_POST['submit'])) {

//create and issue the first query
$name=mysql_real_escape_string($_POST['comment_owner']);
$email=mysql_real_escape_string($_POST['comment_owner_email']); 
$reply=mysql_real_escape_string($_POST['reply']);
$comment_id = $_GET['comment_id'];

$error='';//initialize $error to blank
  if(trim($name)=='' ){
      $error.="Please enter your name!<br />"; //concatenate the $error Message with a line break
  }
   if(trim($email)==''){
    $error.="Plese enter your e-mail address!<br />";
    
  }  elseif (!preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $email)) {

        $error.="The e-mail you entered was not valid!<br />";
        
    }
    if(trim($reply)=='' ){
      $error.="Please enter your reply!<br />"; //concatenate the $error Message with a line break
  }
  if($error==''){


$sql="INSERT INTO replies (comment_id, reply, reply_create_time, reply_owner, reply_owner_email) VALUES ( '$comment_id', '$reply', now(), '$name','$email')"; 
  
mysql_query($sql,$conn) or die(mysql_error());

header('Location: testimonials.php');

  // mysql_query($sql,$conn) or die(mysql_error());
  }
    else{
       echo "<div class=error><span style=color:red>$error</span><br /></div>";
    }    
} else {

$name= '';
$email= '';
$reply= '';
}
?>
<form name="comment" id="comment" onsubmit="return validateFormOnSubmit(this)" action="reply.php?comment_id=<?php echo $comment_id; ?>" method="post">
<table border="0" cellspacing="0" cellpadding="5" width="662" class="style2">
<tr>
<td align="left"><label for="name"> Name:</label></td>
<td>
<div class="c2"><input type="text" name="comment_owner" id="comment_owner" size="30" value="<?php echo $name; ?>"/></div></td>
</tr>
<tr>
<td align="left"><label for="email">E-mail:</label></td>
<td>
<div class="c2"><input type="text" name="comment_owner_email" id="comment_owner_email" size="30" value="<?php echo $email; ?>"/></div></td>
</tr>
<tr>
<td align="left">
<label for="reply">Reply:</label></td>
<td>
<textarea name="reply" id="reply" rows="5" cols="30" value="<?php echo $reply; ?>"></textarea></td>
</tr>
<tr>
<td colspan="4">
<div class="c1"><input name="submit" type="submit" value="Submit" /> <input type="reset" name="reset" id="reset" value="Reset" /></div></td>
<td width="2"></td>
</tr>
</table>
</form>

 

not very clean but tested and working!

 

good luck!

 

 

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.