Updating database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • abramfas
    New Member
    • May 2013
    • 9

    Updating database

    Code:
    <?php  
    $objConnect = mysql_connect("localhost","root","") or  die(mysql_error());  
    $objDB = mysql_select_db("databasetable");  
    $strSQL = "UPDATE admin SET ";  
    $strSQL .=",surname = '".$_POST["surname"]."' ";  
    $strSQL .=",othernames = '".$_POST["othernames"]."' ";  
    $strSQL .=",gender = '".$_POST["gender"]."' ";  
    $strSQL .=",username = '".$_POST["username"]."' ";  
    $strSQL .=",password = '".$_POST["password"]."' ";  
    $strSQL .="WHERE id = '".$_GET["id"]."' ";  
    $objQuery = mysql_query($strSQL);  
    if($objQuery)  
    {  
    echo "Save completed.";  
    }  
    else  
    {  
    echo "Error Save [".$strSQL."]";  
    }  
    mysql_close($objConnect);  
    ?>
    I have this code to update tables in the database but it is giving me some errors. can any one please help me find where the problem is? Thanks
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    We need to know what the error message says.

    Comment

    • alexsts2013
      New Member
      • Jun 2013
      • 6

      #3
      try to replace
      Code:
       $strSQL = "UPDATE admin SET ";  
      $strSQL .=",surname = '".
      with following
      Code:
       $strSQL = "UPDATE admin SET ";  
      $strSQL .="surname = '".

      Comment

      • alexsts2013
        New Member
        • Jun 2013
        • 6

        #4
        Also replace
        Code:
        "' "
        with following

        Code:
        "'"
        Except last one ($_POST["password"]."' "; ) because on front of where clause you will need space like " where"; right now if you look at print of your $strSQL you will see some pretty ugly string.
        Use my suggestion and it will look much better and fix syntax errors as well.

        Comment

        • abramfas
          New Member
          • May 2013
          • 9

          #5
          Hi Alexsts,

          I have corrected all the things you said but am still having the same errors...

          Code:
          Error Save [UPDATE admin SET surname = 'adam'othernames = 'apple'gender = 'male'username = 'admin'password = 'password'WHERE id = '7']

          Comment

          • alexsts2013
            New Member
            • Jun 2013
            • 6

            #6
            try this:
            Code:
            $strSQL = "UPDATE admin SET ";  
            $strSQL .="surname = '".$_POST["surname"]."'";  
            $strSQL .=",othernames = '".$_POST["othernames"]."'";  
            $strSQL .=",gender = '".$_POST["gender"]."' ";  
            $strSQL .=",username = '".$_POST["username"]."'";  
            $strSQL .=",password = '".$_POST["password"]."' ";  
            $strSQL .="WHERE id = '".$_GET["id"]."' ";
            You removed to much from your string.
            I told you to remove single comma and empty spaces, not all commas.
            Let me know how new string will do.
            By the way your ID is it string or integer?
            If it is integer than following should be insted:
            Code:
            $strSQL .="WHERE id = ".$_GET["id"];
            Last edited by alexsts2013; Jun 10 '13, 04:58 PM. Reason: additional data needed

            Comment

            Working...