SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Wizard co.ador's Avatar
    Join Date
    Apr 2009
    Posts
    1,054
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem INSERTing Comment into the database

    can anybody tell me what could be missing in this code in order to display this form and make it insert the comments into the database?

    PHP Code:
    if(isset($_POST['Add Comment ' ])){ 

    $comment mysql_real_escape_string($_POST['author']);

    $author mysql_real_escape_string($_POST['author']);

    $pros mysql_real_escape_string($_POST['pros']);

    $cons mysql_real_escape_string ($_POST['cons']);

    $id = (int) $_GET['id']; 
    $query "INSERT INTO `comments` 

    (id created, author, pros, cons) 

    VALUES

    (
    $id, NOW(), '$author', '$pros', '$cons')";

    if(
    mysql_query($query)) {

        echo 
    "Thanks for your comment";

    }

    else {

        echo 
    "Unable to add your comment";

        
    //log mysql_error() here



    echo
    '<tr>';
    echo
    '<td>';
    echo
    '<table width="328" style="top:850px;" class="calamar">
    <td width="320" 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="itemdetails2.php?id=12345678" 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>'
    ;
    echo 
    '</td>';
    echo
    '</tr>'

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,460
    Mentioned
    160 Post(s)
    Tagged
    1 Thread(s)
    A comma after id?

  3. #3
    SitePoint Wizard co.ador's Avatar
    Join Date
    Apr 2009
    Posts
    1,054
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey Mittineague

    Thank you Mittineague I have come up with this fixes including the comma after the id which I was missing indeed but still not inserting any comments in the database. I have even include the fields nickname and fullname to the database table but nothing.
    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']);

    $id = (int) $_GET['id']; 
    $query "INSERT INTO `comments` 

    (id,created, nickname, fullname, pros, cons) 

    VALUES

    (
    $id, NOW(), '$nickname', '$fullname' '$pros', '$cons')";

    if(
    mysql_query($query)) {

        echo 
    "Thanks for your comment";

    }

    else {

        echo 
    "Unable to add your comment";

        
    //log mysql_error() here

    }
    }
    ?>

    <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>

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,460
    Mentioned
    160 Post(s)
    Tagged
    1 Thread(s)
    You don't show what code is before that. But maybe if you added the optional resource argument?
    resource mysql_query ( string $query [, resource $link_identifier ] )

  5. #5
    SitePoint Wizard co.ador's Avatar
    Join Date
    Apr 2009
    Posts
    1,054
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This would go at the top?

    resource mysql_query ( string $query [, resource $link_identifier ] )
    PHP Code:
    </script>
    <?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']);

    $id2 = (int) $_GET['id']; 
    $query "INSERT INTO `comments` 

    (id,created, nickname, fullname, pros, cons) 

    VALUES

    (
    $id2, NOW(), '$nickname', '$fullname' '$pros', '$cons')";

    if(
    mysql_query($query)) {

        echo 
    "Thanks for your comment";

    }

    else {

        echo 
    "Unable to add your comment";

        
    //log mysql_error() here

    }
    }
    ?>
    <?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">&nbsp;</td>
      <td width="54" bgcolor="#FFFFFF"><strong>Price:</strong></td>
      <td colspan="7">$<span class="style3">'
    ,$row[4] ,'</span></td>
    <tr>
      <td class="style1">&nbsp;</td>
      <td colspan="7" class="style3">&nbsp;</td>
    </tr>
    <tr><td>&nbsp;</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 >&nbsp;</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">&nbsp;</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">&nbsp;</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">&nbsp; </td>
    <td width="544" height="29" rowspan="5">&nbsp;</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>';
    ?>

  6. #6
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,460
    Mentioned
    160 Post(s)
    Tagged
    1 Thread(s)
    No, that isn't code, but the syntax. The code would be something like
    PHP Code:
    if(mysql_query($query$your_link_identifier)) { 

  7. #7
    SitePoint Wizard co.ador's Avatar
    Join Date
    Apr 2009
    Posts
    1,054
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the code the form uses to perform the php script it is as follow
    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` 

    (id,created, nickname, fullname, pros, cons) 

    VALUES

    (
    $id2, NOW(), '$nickname', '$fullname' '$pros', '$cons')";

    if(
    mysql_query($query)) {

        echo 
    "Thanks for your comment";

    }

    else {

        echo 
    "Unable to add your comment";

        
    //log mysql_error() here

    }
    }
    ?>

  8. #8
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,460
    Mentioned
    160 Post(s)
    Tagged
    1 Thread(s)
    What I mean, is try adding the optional argument to your code.

    I'm guessing it would be from your "connection.php" file and I'm wondering if PHP is losing it somehow during the "include". In other words, if connection.php has this
    PHP Code:
    $connection mysql_connect($host,$user,$password); 
    then the code to try would be
    PHP Code:
    if(mysql_query($query$connection)) { 

  9. #9
    SitePoint Wizard co.ador's Avatar
    Join Date
    Apr 2009
    Posts
    1,054
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, I was missing the if(mysql_query($query, $connection)) {

    and that was one of the things I was missing and a for each loop

  10. #10
    SitePoint Wizard co.ador's Avatar
    Join Date
    Apr 2009
    Posts
    1,054
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The loop below is 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:

    To display this page, Firefox must send information that will repeat any action (such as search or order confirmation) that was performed earlier.
    How can it be avoided so it just save the data and then continue without repeating the previous message over?

    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']
            );
        }



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •