Unable to insert data using php and mysql

Discussion in 'PHP' started by dsmlacctv, Sep 7, 2013.

  1. #1
    <?php
      include("include/connection.php");
      session_start();
    $ses_id = session_id();
    $ID = (int)$_REQUEST['id'];
    
     ?>
    PHP:
    <?php
    
              $productdetail = mysql_query("select * from productdetail where PID = '$ID'" , $con);
    
        while($query_data = mysql_fetch_array($productdetail))
            {
                $ID = $query_data['PID'];
                $PCode = $query_data['PCode'];
                $PName = $query_data['PName'];
                $PPrice = $query_data['PPrice'];
                $Stock = $query_data['StockA'];
                $PDescription = $query_data['ProductDescription'];
                $Thumbnail = $query_data['Thumbnail'];
                $Picture1 = $query_data['Picture1'];
                $Picture2 = $query_data['Picture2'];
                $Picture3 = $query_data['Picture3'];
                $Picture4 = $query_data['Picture4'];
           
           
           
            }
           
            ?>
    PHP:
    when I click on submit nothing happens
     
    dsmlacctv, Sep 7, 2013 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,897
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #2
    There were loads of issues with your script and I've moved some things around to give you some ideas.

    Your key problem, though, was that the submit button didn't have a name and therefore wasn't in the $_POST array. The variable names in the form didn't match the script either, and you aren't saving the id of the item being reviewed so the review.
    <?php
    if (isset($_POST['submit'])) {
        // connect to database!!!
        //echo "heloo";
        $id = intval($_POST['id']);
        if ($id > 0) {
            $Rname = mysql_real_escape_string(stripslashes(htmlentities($_POST["txtname"])));
            $Reviewbtn = $_POST["reviewbtn"];
            $Txtbox = mysql_real_escape_string(stripslashes(htmlentities($_POST["txtbx"])));
    
    //$id needs to be saved into the database
            $sql = "INSERT INTO `reviews` (`Name`, `Comments`, `Rating`)
        VALUES('{$Rname}','{$Reviewbtn}','{$Txtbox}')";
            var_dump($sql);
            $sql = mysql_query($sql, $con) or die(mysql_error() . '<br/>' . $sql);
    
            mysql_close($con);
            header("location: product_detail.php?result=ok");
            exit;
        }
        else
            header("location: product_detail.php?result=fail");
    }
    
    
    if (isset($_GET['result'])) {
        echo "<div id='feedback span5'>";
        if ($_GET['result'] == 'ok')
            echo "Record added succesfully.";
        else
            echo "Failed to add";
    
        echo "</div>";
    }
    ?>
    
    <div class="span5">
    
        <div class="span2">
            <address>
                <h4 class="label-info"><span>Product Detail</span></h4>
                <strong>Product Name:</strong> <span><?php echo $PName; ?></span><br>
                <strong>Product Code:</strong> <span><?php echo $PCode; ?></span><br>
                <strong>Product Price:</strong> <span><?php echo $PPrice; ?></span><br>
                <strong>Availability in Stock:</strong><span style="color:#FF0000;">
                    <?php
                    if ($Stock == 1) {
                        echo '<strong>Available</strong>';
                    } else {
                        echo "Not Available";
                    }
                    ?> </span><br>
            </address>
        </div>
    
        <div class="span3 col">
            <div class="block">
                <h4>Product Description</h4>
                <span class="uneditable-textarea"><?php echo $PDescription; ?></span>
            </div>
    
    
        </div>
    
    </div>
    
    <div class="row">
    
    
        <div class="span9">
            <div class="span6 ">
                <h4>Review(0)</h4>
                <p>There are no review for this product</p>
                <h4>Write a Review</h4>
                <form id="Re" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?> ">
                    <p>Your Name:</p>
                    <input type="text" id="txtname" name="Rname" placeholder="write your name..." />
    
                    <p>Your Review</p>
                    <label>Excellent&nbsp;&nbsp;<input type="radio" name="reviewbtn" class="radio" value="Excelent" /></label>
                    <label>Good&nbsp;&nbsp;<input type="radio" class="radio" name="reviewbtn" value="Good" /></label>
                    <label>Poor&nbsp;&nbsp;<input type="radio" class="radio" name="reviewbtn" value="Poor" /></label><br/>
                    <textarea id="txtreview" name="txtbx" cols="50" rows="10" class="container-fluid"></textarea>
                    <input type='hidden' name='id' value="<?php echo $ID; ?>"><br/>
                    <input type="submit" value="submit" name="submit" class="btn" />
            </div>
        </div>
    </div>
    </div>
    
    PHP:
     
    sarahk, Sep 7, 2013 IP
  3. iteamweb

    iteamweb Active Member

    Messages:
    32
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #3
    mysql-real-escape-string extension is deprecated as of PHP 5.5.0
     
    iteamweb, Sep 9, 2013 IP
  4. xxxize

    xxxize Member

    Messages:
    33
    Likes Received:
    2
    Best Answers:
    2
    Trophy Points:
    38
    #4
    Use PDO it's very easy and more clean.
     
    xxxize, Sep 9, 2013 IP
    HolyRoller likes this.
  5. HolyRoller

    HolyRoller Well-Known Member

    Messages:
    552
    Likes Received:
    27
    Best Answers:
    1
    Trophy Points:
    150
    #5
    Got to agree with this, it's the only way forward.

    Just let me know if you need any help with this.
     
    HolyRoller, Sep 25, 2013 IP