My content in a mysql database duplicates when I edit articles

Go To StackoverFlow.com

0

I have a basic CMS for some news articles, basically some of my content contains links as the

    <a href=""></a> 

html so that the links are added when written to the page. Could this be why my content is being randomly duplicated? when edited?

Here's the php I'm using to edit.

          include'includes/connection.php';
          $pName = $_POST['pName'];
         $pItem = $_POST['pItem'];
         $pCategory = $_POST['pCategory'];
         $pDate = $_POST['pDate'];
         $pAuthor = $_POST['pAuthor'];
         $pContent = $_POST['pContent'];
         $Page = $_POST['Page'];
         $id = $_POST['id'];
         $pApproval = $_POST['pApproval'];
         $pPriority = $_POST['pPriority'];
         $pImage_Name = $_POST['pImage_Name'];

         if($pItem != 2){

         $updateq = "UPDATE sitecontent SET ID = '$pID', Post_Title = '$pName',              Post_Year = '$pCategory', Date = '$pDate', Post_Author = '$pAuthor', Post_Content = '$pContent', Page = '$Page', Post_Approval = '$pApproval', Priority = '$pPriority', Image_Name = '$pImage_Name' WHERE ID = '$_POST[id]'";

         $result = mysql_query($updateq) or die (mysql_error());
         header("Location:admin.php");

         }
         if($pCategory = 2){
     $updateq = "UPDATE reviews SET review_id = '$pID', review_title = '$pName', review_category = '$pCategory', review_content = '$pContent', review_image = '$piName', review_approval = '$pApproval' WHERE ID = '$_POST[id]'";

         $result = mysql_query($updateq) or die (mysql_error());
         header("Location:reviews-category.php");
         }

This is the page before that sends the data.

    <form action="editp.php" method="POST" name="editform">
    <table cellpadding="10" >
        <tr>
        <td width="86" valign="top" >
        <label for="pName" style="padding:10px; ">Post Title</label></td>
        <td width="550">
        <input type="text" name="pName" style=" width:550px;border:#000099; margin:10px;" value="<?php echo $post['Post_Title']; ?>"/>
        </td>
        </tr>
        <tr>

         <td width="86" valign="top" >
        <label for="pCategory" style="padding:10px; ">Category</label></td>
        <td width="550">
        <input type="text" name="pCategory" style=" width:50px;border:#000099; margin:10px;" value="<?php echo $post['Post_Year']; ?>"/>
        </td>
        </tr>
        <tr>
        <td>
        <label for="pItem" style="padding:10px;">Item Type</label>
        </td>
        <td>
        <select name="pItem" style="border:#000099; margin:10px;">
            <option value="1">News</option>
            <option value="2">Review</option>
        </select>
        </td>
        </tr>
        <tr>
        <td>
        <label for="pName" style="padding:10px;">Article ID</label>
        </td>
        <td>
        <input type="text" name="pID" style="border:#000099; margin:10px;" value="<?php echo $post['ID']; ?>"/>
        </td>
        </tr>
        <tr>
          <td>
            <label for="pName" style="padding:10px;">Post Date</label>
            </td>
          <td>
                <input type="text"  name="pDate" style="border:#000099; margin:10px;" value="<?php echo $post['Date']; ?>">

            </td>
        </tr>
        <td>
        <label for="pName" style="padding:10px;">Post Author</label>
        </td>
        <td>
        <input type="text" name="pAuthor" style="border:#000099; margin:10px;" value="<?php echo $post['Post_Author']; ?>"/>
        </td>
        </tr>
         <tr>
        <td>
        <label for="pName" style="padding:10px;">Home Page</label>
        </td>
        <td>
        <select name="Page" style="border:#000099; margin:10px;">
            <option value="0">None</option>
            <option value="1">Home</option>
        </select>
        </td>
        </tr>
        <tr>
        <td>
        <label for="pPriority" style="padding:10px;">Home Priority</label>
        </td>
        <td>
        <select name="pPriority" style="border:#000099; margin:10px;">
            <option value="0">None</option>
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
        </select>
        </td>
        </tr>
        <tr>
        <td>

          <label for="pName" style="padding:10px;">Post Content</label>
          </td>
          <td>
            <textarea style="width:550px; height:200px;border:#000099; margin:10px;" type="text" name="pContent" id="pContent" value="<?php echo $post['Post_Content']; ?>"><?php echo $post['Post_Content']; ?></textarea>
            </td>
            </tr>
               <tr>
            <td>
            </td>
            <td style="padding:10px;">
            <span id="btnStrong" style="  padding: 2px 8px;background-color:#C00;font-family:'Trebuchet MS', Arial, Helvetica, sans-serif; color:#FFF; cursor:pointer;">Bold</span> &nbsp;
             <span id="btnItalic" style=" padding: 2px 8px; background-color:#C00;font-family:'Trebuchet MS', Arial, Helvetica, sans-serif; color:#FFF; cursor:pointer;">Italic</span> 
             <span id="btnPara" style=" padding: 2px 8px; background-color:#C00;font-family:'Trebuchet MS', Arial, Helvetica, sans-serif; color:#FFF; cursor:pointer; margin-left:10px;">Paragraph</span>
             <span id="btnBreak" style=" padding: 2px 8px; background-color:#C00;font-family:'Trebuchet MS', Arial, Helvetica, sans-serif; color:#FFF; cursor:pointer; margin-left:10px;">Line Break</span>

             <span id="btnLink" style="  padding: 2px 8px;background-color:#C00;font-family:'Trebuchet MS', Arial, Helvetica, sans-serif; color:#FFF; cursor:pointer;">Link</span>
           </td>
        </tr>
        <td>
        <label for="pImage_Name" style="padding:10px;">Image Name</label>
        </td>
        <td>
        <input type="text" name="pImage_Name" style="border:#000099; margin:10px; width:550px;" value="<?php echo $post['Image_Name']; ?>"/>
        </td>
        </tr>
         <tr>
           <td>
             <label for="pApproval" style="padding:10px;">Approval</label>
             </td>
           <td>
             <select name="pApproval" style="border:#000099; margin:10px;">
               <option value="0">Pending</option>
               <option value="1">Approved</option>
               </select>
             </td>
         </tr>

         <tr align="right">
         <td><input type="hidden" name="id" value="<?php echo $_GET['id']; ?>"/></td>
        <td colspan="2"><span style="margin-left:10px;">Please check the changes above before submitting</span><br/>

        <input type="submit" name="go" value="Submit Changes" style=" padding: 2px 8px;background-color:#C00; color:#FFF; margin:10px;"/>
        </td>
       </tr>
    </table>
    </form>
2012-04-03 21:47
by huddds
When you say duplicated, do you mean the row is duplicated, or the content in each cell is duplicated - BenOfTheNorth 2012-04-03 21:50
Are you sure you want if($pCategory = 2){ and not: if($pCategory == 2){ - chown 2012-04-03 21:50
I mean random bits of text in my varchar field when content is stored so when it writes to my page its all messed up. Its fine on the initial upload but when i edit, it messes up - huddds 2012-04-03 21:55


5

Are you sure you want if($pCategory = 2){ and not: if($pCategory == 2){?

Your if($pCategory = 2){ will always evaluate to true, hence the duplicates.

2012-04-03 21:53
by chown
its only some content in the field Post_Content in my table, not a duplicate of the whole row and id - huddds 2012-04-03 22:00
I changed that to == and I still have the same problem. It's only the Post_Content that has any html code being added to the data so it must be to do with adding links. Any ideas on how people tend to add links where ever they want within their content when its data driven - huddds 2012-04-03 22:16


0

Your UPDATE is potentially changing the ID of the post

SET ID = '$pID'

I cannot see where $pID is set in your code, but I bet it's not the same as $_POST[id].

You do assign $id = $_POST['id'];

Perhaps you meant to assign to $pID rather than to $id?

Either way, I would not include setting the ID in your UPDATE at all. No upside, and risks introducing errors.

2012-04-03 21:49
by Eric J.
sorry, all are sent from a form the id is in the url, all the pVariables are editable fields in my edit form on the previous page - huddds 2012-04-03 21:51
Even worse if that's real code is the SQL injection vulnerability - PeeHaa 2012-04-03 21:51
P.S. I would say borderline comment - PeeHaa 2012-04-03 21:52
Sorry, why the downvote? It's not at all clear where $pID is being set from your question. If it is not set properly, you will change the ID of the post - Eric J. 2012-04-03 21:54
how do you mean - huddds 2012-04-03 21:56
If $pID is not set the same as $POST[id] you will find the row with the ID $POST[id] and then update the ID of that row to whatever is in $pID. $pID is not initialized in the code you show. I think chown provided the correct solution (+1), but I think my input is correct and valuable, too - Eric J. 2012-04-03 21:57
it is the same though, it can be edited though. It's literally only when I have an article that has HTML in the database that some text gets messed up when the Post_Content variable is written to the page and edited after the initial create - huddds 2012-04-03 22:03
Ads