14 Haziran 2022 Salı

PHP crud işlemleri

PHP crud işlemler Create Read Update Delete işlemleri yapılmaktadir.

user.php

<?php

$db = mysqli_connect("localhost", "root","","phpcrud");

?>

<html>

<head>

<title> Crud </title>

</head>

<body>

<form method="POST">

<label>isim</label>

<input type="text" name="name" placeholder="isim giriniz"><br><br>

<label>Email</label>

<input type="email" name="email" placeholder="Email giriniz"><br><br>

<label>Adres</label>

<input type="text" name="address" placeholder="Adres giriniz"><br><br>

<input type="submit" name="submit" value="Kaydet">

</form>

<hr>

<h3> Kayit listesi </h3>

<table style="width:80%" border="1">

<tr>

<th>Sira </th>

<th>Isim </th>

<th> Email</th>

<th> Adress</th>

<th>Menu</th>

</tr>

<?php

$i =1;

$qry = "select * from user";

$run = $db -> query($qry);

if($run -> num_rows > 0)

{

while($row = $run -> fetch_assoc())

{

$id = $row['user_id'];

?>

<tr>

<td><?php echo $i++; ?> </td>

<td><?php echo $row['user_name']; ?> </td>

<td><?php echo $row['user_email']; ?> </td>

<td><?php echo $row['user_address']; ?> </td>

<td>

<a href="edit.php?id=<?php echo $id; ?>">Edit</a>

<a href="delete.php?id=<?php echo $id; ?>" onclick="return confirm('Emin misiniz?')">Delete</a>

</td>

</tr>

<?php

}

}

?>

</table>

</body>

</html>

<?php

if(isset($_POST['submit']))

{

$name = $_POST['name'];

$email = $_POST['email'];

$address = $_POST['address'];

$qry = "insert into user values (null, '$name','$email','$address')";

if(mysqli_query($db, $qry))

{

echo '<script>alert("User registred basarili.");</script>';

header('location: user.php');

}

else

{

echo mysqli_error($db);

}

}

?>

edit.php

<?php

$db = mysqli_connect("localhost", "root","","phpcrud");

if(!$db)

{

die('error in db'. mysqli_error($db));

}

else

{

$id = $_GET['id'];

$qry = "select * from user where user_id =$id";

$run = $db -> query($qry);

if($run -> num_rows > 0)

{

while($row = $run -> fetch_assoc())

{

$username = $row['user_name'];

$useremail = $row['user_email'];

$useraddress = $row['user_address'];

}

}

}

?>

<html>

<head>

<title> Crud </title>

</head>

<body>

<form method="POST">

<label>isim</label>

<input type="text" name="name" value="<?php echo $username; ?>"><br><br>

<label>Email</label>

<input type="email" name="email" value="<?php echo $useremail; ?>"><br><br>

<label>Adres</label>

<input type="text" name="address" value="<?php echo $useraddress; ?>"><br><br>

<input type="submit" name="update" value="Guncelle">

</form>

</body>

</html>

<?php

if(isset($_POST['update']))

{

$name = $_POST['name'];

$email = $_POST['email'];

$address = $_POST['address'];

$qry = "update user set user_name='$name',user_email='$email',user_address='$address' where user_id= $id ";

if(mysqli_query($db,$qry))

{

header('location:user.php');

}

else

{

echo mysqli_error($db);

}

}

?>

delete.php

<?php

$db = mysqli_connect("localhost", "root","","phpcrud");

if(!$db)

{

die('Database hatası'. mysqli_error($db));

}

$id = $_GET['id'];

$qry = "delete from user where user_id = $id";

if(mysqli_query($db, $qry))

{

header('location:user.php');

}

else

{

echo mysqli_error($db);

}

?>



Hiç yorum yok:

Yorum Gönder

Her yorum bilgidir. Araştırmaya devam...