Sunday, March 14, 2010

Reading and Updating a mysql table using php

<?php
//Establishing connection with database and including functionalities
define('DB_HOST','localhost');
define('DB_NAME','mydb');
define('DB_USER','root');
define('DB_PASS','');
$con=@mysql_connect(DB_HOST,DB_USER,DB_PASS);
$bd=@mysql_select_db(DB_NAME) or die ("Cannot connect to the database".mysql_error());
?>


<?php

if (isset($_POST['Submit']))
{

  $title=$_REQUEST['txtitle'];
$desc=$_REQUEST['txtdesc'];
{
$sql1=@mysql_query("Update `about` Set `title` = '$title' , `desc` = '$desc' where id='1'");
if(!$sql1)
{
@mysql_query("ROLLBACK");
}
}
}

$sql2=@mysql_query("Select * from `about` where id='1'");
if(mysql_num_rows($sql2)>0)
{
$rs=@mysql_fetch_object($sql2);
}

?>

<html>
<body>
<form action="one.php" method="post">
<table width="100%" border="1">
  <tr>
    <th scope="row">Title</th>
    <td><input name="txtitle" type="text" id="textfield" value="<?php echo $rs->title;?>" /></td>
  </tr>
  <tr>
    <th scope="row">Desc</th>
    <td><textarea name="txtdesc" id="txtdesc" cols="45" rows="5"><?php echo $rs->desc;?></textarea></td>
  </tr>
  <tr>
    <th scope="row">&nbsp;</th>
    <td><input type="submit" name="Submit" id="button" value="Send" /></td>
  </tr>
</table>
</form>
</body>
</html>

No comments:

Post a Comment