unable to update database. php mysql

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rsmbd
    New Member
    • Dec 2010
    • 1

    #1

    unable to update database. php mysql

    I face the problem

    To Insert data
    to update data

    When I go www.myweb.com/cpanel
    user:xxxxx
    Pass:xxxx
    Go
    Database
    PhP
    MyAdmin
    by this way I can update my database

    But when I connect by PHP,
    I failed
    Pls suggest some one.......pls !!1

    ...1. test.php.. of a row
    <a href="update.ph p?id=<? echo $rows['id']; ?>">Update</a>

    2.update.php
    Code:
    <?php
    $host="localhost"; 
    $username="xxxx"; 
    $pass="xxxx"; 
    $db="xxx"; 
    $tbl="website";
    // Connect to server and select database.
    @mysql_connect("$host", "$username", "$pass")or die("cannot connect");
    @mysql_select_db("$db")or die("cannot select DB");
    
    // get value of id that sent from address bar
    $id=$_GET['id'];
    // Retrieve data from database
    $sql="SELECT * FROM $tbl WHERE id='$id'";
    $result=@mysql_query($sql);
    
    $rows=mysql_fetch_array($result);
    ?>
    
    <form action="update_ac.php?id=<? echo $rows['id']; ?>"  method="post" enctype="multipart/form-data" >
      <table width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
        <tr>
          <td width="11%" align="center" bgcolor="#E6E6E6"><strong>S.L</strong></td>
          <td width="27%" align="center" bgcolor="#E6E6E6"><strong>Name</strong></td>
          <td width="20%" align="center" bgcolor="#E6E6E6"><strong>Description</strong></td>
          <td width="21%" align="center" bgcolor="#E6E6E6"><strong>Link  </strong></td>
          <td width="21%" align="center" bgcolor="#E6E6E6"><strong>Image</strong>  </td>
        </tr>
      
     
    	<tr>
    	
          <td bgcolor="#FFFFFF">
    	  <input type="text" name="id" value="<?php echo $rows["id"] ?>" />
    	  
    	  </td>
          <td bgcolor="#FFFFFF"><p align="center">
              <input name="nam" type="text" value="<? echo $rows['nam']; ?>" />
            </p>
              <p>&nbsp; </p></td>
          <td bgcolor="#FFFFFF"><p align="center">
            <input name="desc" type="text"  value="<? echo $rows['desc']; ?>" />
          </p></td>
          <td bgcolor="#FFFFFF"><div align="center">
            <input name="link" type="text" value="<? echo $rows['link']; ?>" />
          </div></td>
          <td bgcolor="#FFFFFF"><div align="center">
            <input name="pic" type="text"  value="<? echo $rows['pic']; ?>" />
          </div></td>
        
    	</tr>
    	
        <tr>
          <td colspan="5" align="right" bgcolor="#E6E6E6"><input type="submit" name="submit" value="Update" />      </td>
        </tr>
      </table>
      
    </form>
    
    <?php
    
    mysql_close();
    ?>
    2.update_ac.php

    Code:
    <?
    $host="localhost"; 
    $username="xxxx"; 
    $pass="xxxx"; 
    $db="xxx"; 
    $tbl="website";
    
    // Connect to server and select database.
    @mysql_connect("$host", "$user", "$pass")or die("cannot connect");
    
    @mysql_select_db("$db")or die("cannot select DB");
     
     
    if ($_POST["submit"])
       {
       
       $id=$_POST['id'];
    $nam = $_POST["nam"];
    $desc = $_POST["desc"];
    $link = $_POST["link"];
    $pic = $_POST["pic"];
      
           $sql = " UPDATE  website  SET  id='$id',   nam='$nam',  desc='$desc',  link='$link',  pic='$pic'  WHERE    id='$id'  " ;
     
          $result = @mysql_query($sql);
    	  echo "1 Row Updated";
             }
    else 
    {
    echo "ERROR";
    }
    
    if($result) 
       {
    echo "1 Row Updated";
    echo "</BR>";
      }
    else 
      {
      echo "</BR >";
    echo "ERROR Record Update.....";
    echo "</BR >";
    echo "</BR >";
    
    }
    
    ?>
    3.output

    1 Row Updated
    ERROR Record Update.....
    Last edited by Dormilich; Dec 23 '10, 10:28 AM. Reason: please use [CODE] [/CODE] tags when posting code
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    you need to figure out what the error actually is. use mysql_error() for that (or even better, use a modern database layer like PDO or MySQLi)

    Comment

    • Samishii23
      New Member
      • Sep 2009
      • 246

      #3
      Code:
      if ( $_POST["submit"] ) {
       ...
      }
      else {
      print "Error:" . mysql_error();
      }
      Report back

      Comment

      Working...