Have you connected to your MySQL database?
PHP Code:if(!mysql_connect('localhost', 'my_username', 'my_password')) exit('Unable to connect to DB');
mysql_select_db('my_db_name');
| SitePoint Sponsor |





Have you connected to your MySQL database?
PHP Code:if(!mysql_connect('localhost', 'my_username', 'my_password')) exit('Unable to connect to DB');
mysql_select_db('my_db_name');
Yea I have connected..
Cranial look at this
I put the '' for the id field in INSERT INTO comments VALUES (", etc.....PHP Code:<?php
if(isset($_POST['submit' ])){
$nickname = mysql_real_escape_string($_POST['nickname']);
$fullname = mysql_real_escape_string($_POST['fullname']);
$pros = mysql_real_escape_string($_POST['pros']);
$cons = mysql_real_escape_string ($_POST['cons']);
$id2 = (int) $_GET['id'];
$query = "INSERT INTO comments VALUES ('','$shoname_id', NOW(), '$nickname', '$fullname' ,'$pros', '$cons')";
echo $query;
$result = mysql_query($query) or die (mysql_error());
echo "Thanks for your comment";
}
?>
Then Now it is inserting the comments yea!!!
I have the connection coming from this file
PHP Code:<?php require_once("../includes/connection.php");?>
Now is displaying the comments... I was thinking to display the comments above the comment form. Now I know a foreach loop is needed and a query similar to SELECT * FROM comments WHERE id= '$id'";
I know it is something similar I have not idea how to form the foreach loop for this case and where to put and how to form the query to find the comments.
Please have some inputs on this.





You should be using null instead of an empty string as the value of your auto increment field. When you upgrade MySQL the empty string will break your query. I mentioned this in my previous post.
Code SQL:INSERT INTO comments VALUES (NULL, $shoename_id, NOW(), '$nickname', '$fullname' ,'$pros', '$cons')
To select:
If you want more fields from the DB add them to the SELECT query and they'll be available inside the loop.PHP Code://You will need to define $id2 BEFORE this code
$query = "SELECT nickname, pros, cons FROM comments WHERE shoename_id = $id2";
if($result = mysql_query($query)) {
while($r = mysql_fetch_assoc($result)) {
/* we have these variables:
$r['nickname']
$r['pros']
$r['cons']
display them as you wish
*/
}
}
I put NULL instead
Thank you..
I have a question what does the %id and %s means and does in this case?
PHP Code:$rResult = mysql_query(
sprintf(
"SELECT * FROM comments WHERE shoename_id = %d ORDER BY created ASC",
$id
)
);
if(mysql_num_rows($rResult) > 0)
{
while($aComment = mysql_fetch_assoc($rResult))
{
printf(
'
<div class="comment" style="margin-bottom: 2em;">
<div class="author">
<strong>%s</strong> wrote:
</div>
<div class="body">
<strong>Pros:</strong>%s
</div>
<div class="body">
<strong>Cons:</strong>%s
</div>
<div class="meta-info" style="font-size: 0.8em;">
<strong>Time:</strong>%s
</div>
</div>
',
$aComment['fullname'],
$aComment['pros'],
$aComment['cons'],
$aComment['created']
);
}
}
This is the loop printing all the message working perfectly but now it keep repeating the same message everytime the page is refreshed in firefox is there a way to avoid that? by redirecting the page to avoid repetition?
The following message appears when I refresh the browser:
How can it be avoided so it just save the data and then continue without repeating the previous message over?To display this page, Firefox must send information that will repeat any action (such as search or order confirmation) that was performed earlier.
this is the while loop and the mysql query statement to call the comments from the database
PHP Code:$rResult = mysql_query(
sprintf(
"SELECT * FROM comments WHERE shoename_id = %d ORDER BY created ASC",
$id
)
);
if(mysql_num_rows($rResult) > 0)
{
while($aComment = mysql_fetch_assoc($rResult))
{
printf(
'
<div class="comment" style="margin-bottom: 2em;">
<div class="author">
<strong>%s</strong> wrote:
</div>
<div class="body">
<strong>Pros:</strong>%s
</div>
<div class="body">
<strong>Cons:</strong>%s
</div>
<div class="meta-info" style="font-size: 0.8em;">
<strong>Time:</strong>%s
</div>
</div>
',
$aComment['fullname'],
$aComment['pros'],
$aComment['cons'],
$aComment['created']
);
}
}





sprintf and printf return and echo (respectively) a formatted string.
The %s and %d are like placeholders. They get replaced with the other arguments in the function call. %s gets formatted as string, %d gets formatted as a number. It's a way of inserting variables into a string.
Read the manual here:
sprintf
printf
To avoid double posting you should redirect the browser after inserting the comment.
PHP Code:if(isset($_POST['submit'])) {
//add comment here
header("Location: http://example.com/my_script.php");
exit;
}
httif(isset($_POST['submit'])) {
//add comment here
header("//localhost/shoes/stores/itemdetails.php?id=1");
exit;
}
What do you mean by //add comment here you mean the loop goes there?





No, the code you have to add a new comment into the database.
The Firefox warning about resending data relates to when a form is posted (sending data to the script). When you refresh after submitting a form you are basically submitting the form again.
So modify your code so that instead of echoing Thanks for your comment you redirect back to that script.
The URL should also be a full absolute URL (include the http://)
It would be something like this right?
Now just the header appears it doesn't resend the data in the database but instead of the whole script appearing it just the header on top but the rest of the script don't executePHP Code:<?php require_once("../includes/initialize.php");?>
<?php require_once("../includes/connection.php");?>
<?php require_once("../includes/functions.php");?>
<?php include("../includes/header.php");?>
<?php
if( isset($_GET['id']))
{
$id = $_GET['id'];
}
?>
<?php
if(isset($_POST['submit' ])){
$nickname = mysql_real_escape_string($_POST['nickname']);
$fullname = mysql_real_escape_string($_POST['fullname']);
$pros = mysql_real_escape_string($_POST['pros']);
$cons = mysql_real_escape_string ($_POST['cons']);
$appetizers_id = (int) ($_POST['shoename_id']);
$id2 = (int) $_GET['id']; $query = "INSERT INTO comments VALUES (NULL,'$shoename_id', NOW(), '$nickname', '$fullname' ,'$pros', '$cons')";
echo $query;
$result = mysql_query($query) or die (mysql_error());
header("//localhost/shoes/stores/itemdetails.php?id=1");
exit;
}
?>
// Rest of the code but it doesn't appear this time after the submission of the comment.
I know it is exiting and not letting the rest of the comment to display in the screen how
can it be possible to print the rest of the code and still exit to avoid to resend it again





I feel like I'm saying a lot of things twice.
You should use the full absolute URL, starting with http
Is the redirection working?
If you put the form insertion code before you start the markup (before the doctype) then the user won't double download the page. The comment will insert and they'll be redirected before the browser downloads any content.
Surely the location you are redirecting to should use $id or $id2 to redirect to the correct product page? If you hard code 1 into the URL users will end up there regardless of which product they commented on.
You should also read over your code. You have $id and $id2 which are the same. You have $appetizers_id which is not used at all, and you are trying to use $shoename_id in the query, which has not been created.






Also, we're lucky that FF bothers to give us that warning. Guess what IE does? "Page expired". Nothing else, just a warning, no page. Without a redirect after post, the user can't use the back button. And that's a Bad Thing.The Firefox warning about resending data relates to when a form is posted (sending data to the script). When you refresh after submitting a form you are basically submitting the form again.
I have done the last fixes you guys told me. I have erease the $id2 and its values which was repeating I have fixed the type with shoename_id, and I have a try to understand a couple of term even google them but still don't get it.
Here are some questions
Where you talk about form insertion you mean when php read the form field or the form field hard code? what form insertion is?
you also said if I put the the form insertion before the markup or doctype then the user won't double download the form. in other words in won't re-read the form input again. Where is the markup and doctype?
THe whole code is below the redirection still is not working. Please take a look in the action in the form field at the bottom of the script, I have putand I don't know if somehow might be comflicting in the redirection.PHP Code:action="itemdetails.php?id=<?php echo $id ?>"
Thank you guys
I hope i don't have any more mistakes that you have previous told me in the code I think i have proof read the code enough if there is still a mistake excuse me.
The code above this point:PHP Code:<?php require_once("../includes/initialize.php");?>
<?php require_once("../includes/connection.php");?>
<?php require_once("../includes/functions.php");?>
<?php include("../includes/header.php");?>
<?php
if( isset($_GET['id']))
{
$id = $_GET['id'];
}
?>
<?php
if(isset($_POST['submit' ])){
$nickname = mysql_real_escape_string($_POST['nickname']);
$fullname = mysql_real_escape_string($_POST['fullname']);
$pros = mysql_real_escape_string($_POST['pros']);
$cons = mysql_real_escape_string ($_POST['cons'])
;
$shoename_id = (int) ($_POST['shoename_id']);
$query = "INSERT INTO comments VALUES (NULL,'$shoename_id', NOW(), '$nickname', '$fullname' ,'$pros', '$cons')";
echo $query;
$result = mysql_query($query) or die (mysql_error());
header("http://localhost/shoes/stores/itemdetails.php?id=<?php echo $id ?>");
exit;
}
?>
<?php
$query = 'SELECT * FROM menu WHERE id = '.intval($id). ' LIMIT 1 ;';
// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
// see if any rows were returned
if (mysql_num_rows($result) > 0) {
$row = mysql_fetch_row($result); {
echo '<table width="100%" border="0" cellspacing="0" cellpadding="0" class="itemdetails">
<tr><td width="1100" height="417" bgcolor="#FFFFFF" class="tento">
<table class="cafe"><tr><td width="547">
<a href="#"><h3 align="justify" style="position:relative; height:5px; top: 10px;">',$row[3] ,'</h3></a>
</td>
</tr>
</table>
<table width="1215" height="609" class="chencho" >
<td class="largethumb" rowspan="8" align="center">
<a href="#"><img src=',$row[0] ,' width="270" height="160" alt="coloe"/></a></td>
<td width="544" rowspan="8" padding="0" ><table width="252" style="font-size:12px; position:relative; top:-6px;">
<td width="1"> </td>
<td width="54" bgcolor="#FFFFFF"><strong>Price:</strong></td>
<td colspan="7">$<span class="style3">',$row[4] ,'</span></td>
<tr>
<td class="style1"> </td>
<td colspan="7" class="style3"> </td>
</tr>
<tr><td> </td><td><strong>Raiting:</strong></td>
<td width="18" class="rating2">*</td>
<td width="18" class="rating2">*</td>
<td width="18" class="rating2">*</td>
<td width="18" class="rating2">*</td>
<td width="18" class="rating2">*</td>
<td width="71"></td>
</tr><tr>
<td width="12"><span class="style2">coloso</span></td>
</tr>
<tr>
<td > </td>
</tr></table>
*</td>
<tr>
<td width="224" height="40" rowspan="3"><strong>Details:</strong></td>
</tr>
<tr>
<td width="106" height="28"><a href="#"><img src="../images/add to Car.gif" alt="df" width="99" height="28" /></a></td>
</tr><tr>
<td height="25"><a href="#"><img src="../images/viewcart.gif" alt="rt" width="99" height="28" /></a></td>
</tr>
<tr>
<td width="224" height="29"><ul>
<li>coloso mentiroso</li>
</ul></td>
</tr>
<tr>
<td width="224" height="29"><ul>
<li>coloso mentiroso</li>
</ul></td>
</tr>
<tr>
<td width="224" height="21"><ul>
<li>coloso mentiroso</li>
</ul></td>
</tr><tr>
<td height="12" colspan="2"><img src="../images/line..gif" alt="as" width="300" height="7" /></td>
</tr>
<tr></tr><td rowspan="2">
<table width="162" align="center" class="smallthumbs">
<tr>
<td width="46" height="65"><a href="#"><img src=',$row[0] ,' alt="df" width="50" height="50"/></a></td>
<td width="36"><a href="#"><img src="../images/image1.jpg" alt="we" width="50" height="50" /></a></td>
<td width="57"><a href="#"><img src="../images/launch.jpg" alt="bn" width="50" height="50" /></a></td>
<td width="36"><a href="#"><img src="../images/image1.jpg" alt="we" width="50" height="50" /></a></td>
</tr>
</table></td>
<td rowspan="4"> </td>
<td height="49"><strong>Rating and Review:</strong></td><td align="center"><a href="#">Add Review</a></td>
<tr>
<td rowspan="1" height="4" ><table style="font-size:10; position:relative; left:26px;">
<td width="58">One star</td>
<td width="40">*****</td>
<td width="25">[23]</td>
</table></td>
</tr><td rowspan="2"></td>
<tr><td height="4"><table style="font-size:10; position:relative; left:26px; ">
<td width="58">One star</td>
<td width="40">*****</td>
<td width="25">[23]</td>
</table></td>
</tr>
<td width="321" rowspan="7"></td>
<td width="544" rowspan="7"> </td>
<td width="224" height="4"><table style="font-size:10; position:relative; left:26px;">
<td width="58">One star</td>
<td width="40">*****</td>
<td width="25">[23]</td>
</table></td>
<tr><td width="224" height="4"><table style="font-size:10; position:relative; left:26px;">
<td width="58">One star</td>
<td width="40">*****</td>
<td width="25">[23]</td>
</table></td></tr>
<td width="224" height="4"><table style="font-size:10; position:relative; left:26px;">
<td width="58">One star</td>
<td width="40">*****</td>
<td width="25">[23]</td>
</table></td>
<tr>
<td height="4" colspan="2"><img src="../images/line..gif" alt="df" width="330" height="7" /></td>
</tr>
<tr>
<td width="224" height="52"><strong>Items Specifications:</strong></td>
</tr>
<td width="224" height="4" style="font-size:11;"><ul>
<li>Lemon</li>
</ul></td>
<tr>
<td width="224" height="4" style="font-size:11;"><ul>
<li>Marincra</li>
</ul></td>
</tr>
<td width="321" height="29" rowspan="5"> </td>
<td width="544" height="29" rowspan="5"> </td>
<td height="1" colspan="1" style="font-size:11;"><ul>
<li>Sal</li>
</ul></td>
<tr>
<td height="1" style="font-size:11;"><ul>
<li>Tomatos</li>
</ul></td>
</tr> <tr>
<td height="1" style="font-size:11;"><ul>
<li>Plums</li>
</ul></td>
</tr> <tr>
<td height="1" style="font-size:11;"><ul>
<li>Saludos</li>
</ul></td>
</tr> <tr>
<td height="1" style="font-size:11;"><ul>
<li>Asucar</li>
</ul></td>
</tr>
</table>
';}
}
?>
<tr><td><table width="487" style="top:850px;" class="calamar"><td width="479" style=" line-height:3;"><strong>Comments:</strong></td>
<tr></tr><td style="font-family:\'Times New Roman\', Times, serif; font-size:14px;">
<form id="itemcomments" action="itemdetails.php?id=<?php echo $id2;?>" method="post">
<fieldset>
<legend>Make a comment on this item</legend>
<div>
<label for="nickname">Nickname:</label>
<input type="text" name="nickname" id="nickname" maxlength="85" />
</div>
<div>
<label for="fullname">Full Name:</label>
<input type="text" name="fullname" id="fullname" maxlength="85" />
</div>
<div>
<label for="pros">Pros:</label>
<textarea name="pros" id="pros" cols="35" rows="5"></textarea>
</div>
<div>
<label for="cons">Cons:</label>
<textarea name="cons" id="cons" cols="35" rows="5"></textarea>
</div>
<input type="submit" name="submit" value="Add Comment">
<input type="reset" value="Reset Fields">
</fieldset>
</form>
</table>
</td>
</tr>
<?php
echo'</td>';
echo'</tr>';
echo '</table>';
?>
Above the that code everything execute. that's why the headerPHP Code:header("http://localhost/shoes/stores/itemdetails.php?id=<?php echo $id ?>");
exit;
is the only thing displaying after the form submission.PHP Code:<?php include("../includes/header.php");?>
But scripts and codes below the re-direction
is not displaying, you have explain me about the form insertion being before the markup, that way the user won't have to insert the comment again. but I don't undertand that part that's why I have formulate the question above.PHP Code:header("http://localhost/shoes/stores/itemdetails.php?id=<?php echo $id ?>");
exit;
This is the error displaying in the browser after I have redirected the form.
error
redirection:Not Found
The requested URL /shoes/stores/itemdetails.php was not found on this server.
PHP Code:header("Location: http://localhost/shoes/stores/itemdetails.php?id=$id");exit;
Hey guys
I had a typo inside the url string inside the header(): that's find it was not able to find the webpage I was trying to output inside the header function
Now it is redirecting.........
I was not using the (Location: url....); inside the header function's braces either... I though it was part of the url string when you put it inside the braces...
Thank you guys.
Bookmarks