Php ile MySql Datebase İşlemleri

Programlama15/11/2007


Php ile MySql datebase üzerinden yapılan ekleme-silme-düzeltme işlemleri;
Kayıt Listeleme;

$connect = mysql_connect("localhost", "root", `pass`);
if ($connect){
mysql_select_db(`myweb`) or die(`Could not select database`);
$query = `SELECT * FROM users`;
$result = mysql_query($query) or die(`Query Hatası: ` . mysql_error());
$satirsayisi=@mysql_num_rows($result);
?>
<table width="50%" border="1" align="center" cellpadding="0" cellspacing="0">
<tr>
<td align="center" bgcolor="#0099CC">KAYITLI ÜYELERİMİZ</td></tr>
<tr>
<td height="57">

    <table width="100%" border="1" align="center" cellpadding="0" cellspacing="0">
    <tr bgcolor="orange">
    <td width="25%">ID</td>
    <td width="25%">USERNAME</td>
    <td width="25%">PASSWORD</td>
    <td width="25%">&nbsp;</td>
    </tr>
    <?php
    for ($i=0;$i<$satirsayisi;$i++)
    {
    $satir=mysql_fetch_array($result);
    ?>
    <tr>
    <td width="25%"><?php echo($satir["id"]); ?> </td>
    <td width="25%"><?php echo($satir["name"]); ?></td>
    <td width="25%"><?php echo($satir["pass"]); ?></td>
    <td width="25%" align="center"><a href="?islem=del&id=<?php echo($satir["id"]); ?>">Sil</a></td>
    </tr>
    <?php
    }
    ?>

    </table>

</td>
</tr>
</table>

<?
$close = mysql_close($connect);
if ($close==false){
echo "MYSQL Bağlantısı Kapatılırken Hata Oluştu.\n<br>";
}
exit();?>

Yeni Kayıt;

$connect = mysql_connect("localhost", "root", `pass`) or die("bağlanamadı");
if ($connect){
mysql_select_db(`myweb`) or die(`Could not select database`);
$query = "insert into users(name,pass) values(`".$_POST[`username`]."`,`".$_POST[`password`]."`)";
//echo $query;
$result = mysql_query($query) or die(`Query Hatası: ` . mysql_error());
echo "<center>İşlem Tamamlandı</center>";
kapat($connect);
}else{
$error = mysql_error();
echo "<center>Database`e bağlanamadı. Hata = $error.</center>";
exit();
}

Kayıt Düzeltme;

$connect = mysql_connect("localhost", "root", `pass`);
if ($connect){
mysql_select_db(`myweb`) or die(`Could not select database`);
$query = "update pages set comment=`".$_POST[`comment`]. "`,code=`".$_POST[`code`]."` where id=".$_POST[`id`];
mysql_query($query) or die(`Query Hatası: `.mysql_error());
Header("Location:aaa.php?islem=read");
kapat($connect);
}else{
$error = mysql_error();
echo "<center>Database`e bağlanamadı. Hata = $error.</center>";
exit();

Kayıt Silme;

$connect = mysql_connect("localhost", "root", `pass`);
if ($connect){
mysql_select_db(`myweb`) or die(`Could not select database`);
$query = `DELETE  FROM pages where id=`.$_GET[`id`];
$result = mysql_query($query) or die(`Query Hatası: ` . mysql_error());
Header("Location:aaa.php?islem=read");
kapat($connect);
}else{
$error = mysql_error();
echo "<center>Database`e bağlanamadı. Hata = $error.</center>";
exit();
}
Etiketler: