Jump to content

[SOLVED] what do i use for this to work ?


jamesxg1

Recommended Posts

what sort of syntax or code would i use to display the comments from the system im making, heres the code.

 

Get's The Comments:

 

<?php
$sql = "SELECT * FROM `comments` WHERE comto = '$username'";
$query = mysql_query($sql);
while ($row = mysql_fetch_assoc($query)) {
$from = $row['comfrom'];
$message = $row['commen'];
}
?>

 

This is the form:

 

<?php
if(isset($_POST['submit'])) {
  $comto = strip_tags(mysql_real_escape_string($_POST['comto']));
  $comfrom = strip_tags(mysql_real_escape_string($_POST['comfrom']));
  $commen = strip_tags(mysql_real_escape_string($_POST['commen']));
  $sql = "INSERT INTO comments (comto, comfrom, commen) VALUES('$comto', '$comfrom', '$commen')";
  mysql_query($sql) or die(mysql_error());
  echo ("<P ALIGN=CENTER>Comment Sent</P>");
  exit;
}
?>

<form name="comments" action="<? $_SERVER['PHP_SELF']; ?>" method="post">
<input type="hidden" name="comto" value="<? echo $username ?>" id="comto">
<input type="hidden" name="comfrom" value="<? echo($_SESSION['username']); ?>" id="$comfrom">
Comment:
<br><textarea name="commen" cols="45" rows="5" wrap="VIRTUAL" id="$commen"></textarea>
<br>   
        <input type="submit" name="submit" value="Add Comment"></td>
</form>				

 

and i try to put

 

<?php print("$message"); ?>

 

but nothing does anyone know how ?

 

Link to comment
https://forums.phpfreaks.com/topic/143203-solved-what-do-i-use-for-this-to-work/
Share on other sites

>>$sql = "SELECT * FROM `comments` WHERE comto = '$username'";

i believe the main problem in here

 

try

<?php
echo "view comment from $username<hr/>";
$sql = "SELECT * FROM `comments` WHERE comto = '$username'";
$query = mysql_query($sql);
while ($row = mysql_fetch_assoc($query)) {
$from = $row['comfrom'];
$message = $row['commen'];
print_r($row);echo "<br/>\n";
print("$message");
}
?>

if this goes well.. perhaps..

 

>><? $_SERVER['PHP_SELF']; ?>

i think something missing here?

 

>>id="$comfrom"

about this id.. better give name that's not use $ in the front

 

>>mysql_query($sql) or die(mysql_error());

change this into

<?php $con=mysql_query($sql) or die(mysql_error()); ?>

i think the problem (query) not show.

 

suggestion:

if you want advance php. try remember this. to test script.. u must give FALSE input.. and then have error message in it. well.. you done a pretty damn good script but nobody perfect

Hiya,

 

Still nothing i have edited it abit more as i didnt want it to post to another page to add them comment, so heres the result but i still can get any thing to be viewed :S,

 

 

Form:

<div id="insert_response"></div>

<form action="javascript:insert()" method="post">
<input type="hidden" name="comto" value="<? echo $username ?>" id="comto">
<input type="hidden" name="comfrom" value="<? echo($_SESSION['username']); ?>" id="$comfrom">
Comment:
<br><textarea name="commen" cols="45" rows="5" wrap="VIRTUAL" id="$commen"></textarea>
<br>   
         <input type="submit" name="submit" value="Send">
</form>	

 

Java script that the forms sent to:

 

/* ---------------------------- */
/* XMLHTTPRequest Enable */
/* ---------------------------- */
function createObject() {
var request_type;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
request_type = new ActiveXObject("Microsoft.XMLHTTP");
}else{
request_type = new XMLHttpRequest();
}
return request_type;
}

var http = createObject();
/* -------------------------- */
/* INSERT */
/* -------------------------- */
/* Required: var nocache is a random number to add to request. This value solve an Internet Explorer cache issue */
var nocache = 0;
function insert() {
// Optional: Show a waiting message in the layer with ID login_response
document.getElementById('insert_response').innerHTML = "Just a second..."
// Required: verify that all fileds is not empty. Use encodeURI() to solve some issues about character encoding.
var comto = encodeURI(document.getElementById('comto').value);
var comfrom = encodeURI(document.getElementById('comfrom').value);
var commen = encodeURI(document.getElementById('commen').value);

// Set te random number to add to URL request
nocache = Math.random();
// Pass the login variables like URL variable
http.open('get', 'insert.php?comto='+comto+'&comfrom=' +comfrom+'&commen='+commen+'&nocache=' +nocache);
http.onreadystatechange = insertReply;
http.send(null);
}
function insertReply() {
if(http.readyState == 4){ 
var response = http.responseText;
document.getElementById('insert_response').innerHTML = 'Your comment' +response+' has been posted.';
}
}

 

 

 

the script that actually inputs it:

<?php
if(isset($_GET['comto']) && isset($_GET['comfrom']) && isset($_GET['commen'])){

$comtoo= $_GET['comto'];
$comfromm= $_GET['comfrom'];
$commenn= $_GET['commen'];

$insertSite_sql = "INSERT INTO comments (comto, comfrom, commen) VALUES('{$comtoo}', '{$comfromm}', '{$commenn}')";
$insertSite= mysql_query($insertSite_sql) or die(mysql_error());
echo '';
} else { 
echo 'Error please fill enter a comment.';
}
?>

 

And this is the script that should be able to display the comments when i add the $message var in the main page:

 

<?php
$sql = "SELECT * FROM `comments` WHERE comto = '$username'";
$query = mysql_query($sql);
while ($row = mysql_fetch_assoc($query)) {
$from = $row['comfrom'];
$message = $row['commen'];
print_r($row);echo "<br/>\n";
print("$message");
}
?>

 

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.