php-crud/query.php

132 lines
2.9 KiB
PHP
Raw Permalink Normal View History

2020-01-02 23:21:46 +07:00
<?php
//koneksi database
$con = mysql_select_db('rai_crud',mysql_connect('localhost','project',''));
if (mysql_errno())
{
echo "Failed to connect to MySQL: " . mysql_connect_error();
}
//fungsi update mahasiswa
function updmhs($id){
$nim = $_POST['nim'];
$nama = $_POST['nama'];
$telp = $_POST['telp'];
$asal = $_POST['asal'];
$alamat = $_POST['alamat'];
$sql = "UPDATE mahasiswa
SET nim=$nim, nama='$nama', telp='$telp', alamat='$alamat', asal='$asal'
WHERE nim=$id";
if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}
header('Location: index.php');
}
//fungsi hapus mahasiswa
function delmhs($id){
$sql = "DELETE FROM mahasiswa WHERE nim=$id";
if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}
header('Location: index.php');
echo "<script type='text/javascript'>alert('One Record Deleted');</script>";
}
//fungsi cari mahasiswa
function srchmhs($search){
$sql = "";
$query = mysql_query($sql);
return $query;
}
//fungsi select mahasiswa
function slcmhs($id){
$sql = "SELECT * FROM mahasiswa WHERE nim=$id";
//echo $sql;
$query = mysql_query($sql);
//var_dump($query);
return $query;
}
//fungsi insert mahasiswa
function insmhs(){
$nim = $_POST['nim'];
$nama = $_POST['nama'];
$telp = $_POST['telp'];
$asal = $_POST['asal'];
$alamat = $_POST['alamat'];
$foto = foto($_FILES,$nim);
$sql = "INSERT INTO mahasiswa (nim,nama,alamat,telp,asal,foto) VALUES ($nim,'$nama','$alamat','$telp','$asal','$foto')";
//echo $sql;
if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}
header('Location: index.php');
}
function foto($file,$id){
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $file["file"]["name"]);
$extension = end($temp);
if ((($file["file"]["type"] == "image/gif")
|| ($file["file"]["type"] == "image/jpeg")
|| ($file["file"]["type"] == "image/jpg")
|| ($file["file"]["type"] == "image/pjpeg")
|| ($file["file"]["type"] == "image/x-png")
|| ($file["file"]["type"] == "image/png"))
//&& ($file["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
{
if ($file["file"]["error"] > 0)
{
echo "Return Code: " . $file["file"]["error"] . "<br>";
}
else
{
if (file_exists("upload/" . $id.".".$extension))
{
echo $id . " already exists. ";
}
else
{
move_uploaded_file($file["file"]["tmp_name"],
"upload/" . $id.".".$extension);
$lokasi = "upload/" . $id.".".$extension;
return $lokasi;
}
}
}
else
{
echo "Invalid file";
}
}
if (isset($_GET['action'])) $func=$_GET['action'];
else $func='';
switch($func){
case 'delete' :
$id = $_GET['id'];
delmhs($id);
break;
case 'update' :
$id = $_GET['id'];
updmhs($id);
break;
case 'insert' :
insmhs();
break;
default :
//echo 'no run';
}
?>