Delete dir and data from mysql - php

I use form which one upload file into folder and info(name, size) into database.So i want to make deleting form which one delete file and data from database.
<?php
include '../dbc.php';
$tbl_name="image"; // Table name
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
//detect file
$directory = "../../Upload/";
$images = scandir($directory);
$ignore = Array(".", "..");
?>
<table width="800px" style="background:white;margin:50px auto; font-size:13px;" border="1" cellpadding="3">
<tr>
<td colspan="3"><strong>Delete:</strong> </td>
</tr>
<tr>
<td width="40%" align="center"><strong>Name</strong></td>
<td width="10%" align="center"><strong>Size</strong></td>
<td width="5%" align="center"><strong>Delete</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr id='del'>
<td><?php echo $rows['name']; ?></td>
<td><?php echo number_format($rows['imgSize'], 2) ?> MB</td>
<td><a name="delete" href="delData1.php?id=<?php echo $rows['imgId'];
?>">Delete</a></td>
</tr>
<?php
}
?>
</table>
<?php
mysql_close();
?>
delData1.php
<?php
include '../dbc.php';
$tbl_name="image"; // Table name
// get value of id that sent from address bar
$id=$_GET['id'];
$filename = $_POST['filename'];
$path = $_POST['directory'];
if(file_exists($path."/".$filename)) {
unlink($path."/".$filename); //delete file
}
// Delete data in mysql from row that has this id
$sql="DELETE FROM $tbl_name WHERE imgId='$id'";
$result=mysql_query($sql);
// if successfully deleted
if($result){
rmdir('$name');
echo "Deleted";
echo "<BR>";
header("Location:Data.php");
}
else {
echo "ERROR";
}
?>
It's delete data from database, but file stay in folder.

Related

Inserting foreign keys into a table from the website using php (phpMyAdmin)

So, I have 3 tables in my database (phpMyAdmin) bikes, book and users. I have made bike_id and user_id a foreign key in the book table. Now I want these 2 to get inserted into the book table from the web page.
I've got 3 php files which are book1.php, book2.php and book3.php. In book1, you can select a bike which you want to book which results in showing that particular bike in new page. In book2, you select the date when you want to book and when you go to book3, the user_id and bike_id should be automatically inserted into the book table? But I only get ERROR when I press book in book2 page.
Can someone help me plz.. Below is the php codes for 3 php files.
book1.php:
<?php
require 'connect.php';
$select_posts = "select * from bikes ";
$run_posts = mysql_query($select_posts);
?>
<table cellpadding="2" cellspacing="2" border="2">
<tr>
<th>Name</th>
<th>Image</th>
<th>Available</th>
<th>Select Bike</th>
</tr>
<?php while($row=mysql_fetch_array($run_posts)){ ?>
<tr>
<td><?php echo $row[bike_name]; ?></td>
<?php echo "<td>";?> <img src="<?php echo $row[bike_image]; ?>" height="250" width="300"> <?php echo "</td>";?>
<td><?php echo $row[avail]; ?></td>
<td><a href="book2.php?id=<?php echo $row[bike_id];?>">Select</td>
</tr>
<?php } ?>
book2.php:
<?php
session_start();
?>
<?php
require 'connect.php';
if(isset($_GET['id'])){
$took_id = $_GET['id'];
$select_query = "select * from bikes where bike_id='$took_id'";
$run_query = mysql_query($select_query);
?>
<table cellpadding="2" cellspacing="2" border="2">
<tr>
<th>Name</th>
<th>Image</th>
<th>Available</th>
<th></th>
<th>Select Date</th>
<th>Book</th>
</tr>
<?php while($row=mysql_fetch_array($run_query)){ ?>
<tr>
<form action="book3.php" method="POST">
<td><?php echo $row[bike_name]; ?></td>
<?php echo "<td>";?> <img src="<?php echo $row[bike_image]; ?>" height="250" width="300"> <?php echo "</td>";?>
<td><?php echo $row[avail]; ?></td>
<td><?php echo $took_id; ?></td>
<td>Select Date: <input type="text" id="datepicker" name="datepicker"></td>
<td><input type="submit" name="Submit" value="Book"></td>
</tr>
<?php }} ?>
</table>
book3.php:
<?php
session_start();
?>
<?php
require 'connect.php';
// Get values from form
$datepicker = $_POST['datepicker'];
$sql="INSERT INTO $tbl_name(datepicker)VALUES('$datepicker')";
$sql="INSERT INTO $tbl_name(user_id)VALUES(select user_id from users)";
$sql="INSERT INTO $tbl_name(bike_id)VALUES(select bike_id from bikes)";
$result=mysql_query($sql);
// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful";
echo "<BR>";
}
else {
echo "ERROR";
}
?>

Connetion to database PHP (empty database)

Sorry, but i ve got another problem... with connection, I guess.
I try to delete record, but my database is empty. And look like this:
And this is the code. I know, this probably trivial matter but I'm amateur in PHP.
<?php
$con=mysqli_connect("localhost","root","","kluby ranking");
// select record from mysql
$sql="SELECT * FROM europa";
$result=mysqli_query($con,$sql);
?>
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td colspan="5" bgcolor="#FFFFFF"><strong>Delete data in mysql</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF"><strong>ID</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Nacja</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>LiczbaPkt</strong></td>
<td align="center" bgcolor="#FFFFFF"> </td>
</tr>
<?php
while($rows=mysqli_fetch_array($result)){
?>
<tr>
<td bgcolor="#FFFFFF"><? echo $rows['ID']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['Nacja']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['LiczbaPkt']; ?></td>
<td bgcolor="#FFFFFF">delete</td>
</tr>
<?php
// close while loop
}
?>
</table>
<?php
// close connection;
mysqli_close($con);
?>
<?php
$con=mysqli_connect("localhost","root","","kluby ranking");
// get value of id that sent from address bar
$id=$_GET['ID'];
// Delete data in mysql from row that has this id
$sql="DELETE FROM europa WHERE ID='ID'";
$result=mysqli_query($con,$sql);
// if successfully deleted
if($result){
echo "Deleted Successfully";
echo "<BR>";
echo "<a href='usuw.php'>Back to main page</a>";
}
else {
echo "ERROR";
}
?>
<?php
// close connection
mysqli_close($con);
?>

Sending over URL 2 Var (1 selected and 1 logged in user)

I am trying to send over 2 vars to a page - 1 being the logged in user and the other is the ID of a registered user which is having there profile updated by the logged in user.
Im not sure how to change the page i have.
<?php
include("includes/connect.php");
//logged in users ID from logged in page.
$id = $_GET['id'];
$sql="SELECT * FROM guild_apply";
$result=mysql_query($sql);
?>
<blockquote>
<table border='1' width='100%'>
<tr>
<th>ID </th>
<th>Real Name</th>
<th>Character Name</th>
<th>Class</th>
<th>Rank</th>
</tr>
<?php
while($row = mysql_fetch_array($result)){
?>
//info from all users registered with default rank (which is going to be changed)
<tr>
<td><?php echo $row['id']; ?> </td>
<td><?php echo $row['realname']; ?> </td>
<td><?php echo $row['charname']; ?> </td>
<td><?php echo $row['class']; ?> </td>
<td><?php echo $row['rank']; ?> </td>
<td align="center">update</td>
</tr>
<?php
}
?>
</table>
</blockquote>
?>
------update.php-----
<?php
// Connect to server and select database.
include("connect.php");
$id =$_GET['id'];
$rank =$_POST['rank'];
$charname =$_GET ['charname'];
// update data in mysql database
$sql="UPDATE guild_apply SET rank='$rank' WHERE charname='$charname'" or die ("cant find learner");
$result=mysql_query($sql) or die ("this stuffedup");
if ($result){
// if successfully updated.
header ("Location:../change.php?id=$id");
}else{
echo "Update failed";
}
?>
Add an &id= parameter to the URL.
<td align="center">update</td>

i am unable to view the mysql Data in my table made on php .mysql table primary key intake. 2. I have created 2 php file.first delete.php and delq.php

This is the delete page where i want to view the mysql stored data in table but I am unable view. I have created 7 columns for table name registration but I can't fetch the data from database.
<div id="main">
<div id="formainsupport"></div>
<div id="forreg">
<?Php
$tbl_name="registration"; // Table name
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("examonline.", $con);
// select record from mysql
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
?>
<table width="900" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td colspan="8" bgcolor="#FFFFFF"><strong>Delete data in mysql</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF"><strong>intakeid</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>firstname</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>lastname</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>password</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>confirmpassword</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>homeaddress</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>email</strong></td>
<td align="center" bgcolor="#FFFFFF"> </td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td bgcolor="#FFFFFF"><? echo $rows['intakeid']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['firstname']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['lastname']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['password']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['confirmpassword']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['homeaddress']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['email']; ?></td>
<td bgcolor="#FFFFFF">delete</td>
</tr>
<?php
// close while loop
}
?>
</table>
<?php
// close connection;
mysql_close();
?>
Here is code for the to delete the data from database. My sql delete query is here but I think it's not working although it's a fine code. I am unable to find the problem.
<h1>delq.php</h1>
<?php
$tbl_name="registration"; // Table name
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("examonline.", $con);
// get value of id that sent from address bar
$intakeid=$_GET['intakeid'];
// Delete data in mysql from row that has this id
$sql="delete from $tbl_name where intakeid='$intakeid'";
$result=mysql_query($sql);
// if successfully deleted
if($result){
echo "Deleted Successfully";
echo "<BR>";
echo "<a href='Delete.php'>Back to main page</a>";
}
else {
echo "ERROR";
}
?>
<?php
// close connection
mysql_close();
?>

If IDs from two different tables are equal, display name from another table

I'm writing a code for my little admin panel, and since I'm not that advanced of a coder, I'm experiencing some troubles with getting a name using two different tables.
Here's my code so far:
<?php
session_start();
if(!session_is_registered(myusername)){
header("location:main_login.php");
}
include 'db_connect.php';
$sql = "SELECT * FROM $tbl_name WHERE is_dead='0'";
$result=mysql_query($sql);
?>
<title>Title</title>
<center><img src="header.png"></center>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>
<table width="400" border="1" cellspacing="0" cellpadding="3">
<tr>
<? include 'menu.php';?>
</tr>
<tr>
<td align="center"><strong>ID</strong></td>
<td align="center"><strong>Unique ID</strong></td>
<td align="center"><strong>Model</strong></td>
<td align="center"><strong>Last Online</strong></td>
<td align="center"><strong>Options</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td><? echo $rows['id']; ?></td>
<td><? if ($rows['unique_id'] == 7815684) { echo '<font color="blue"><b><u>7815684</u></b></font>'; }
elseif ($rows['unique_id'] == 2312964) { echo '<font color="blue"><b><u>2312964</u></b></font>'; }
else { echo $rows['unique_id']; } ?></td>
<td><? echo $rows['model']; ?></td>
<td align='center'><font color="green"><b><? echo $rows['last_updated']; ?></b></font></td>
<td align="center">update
</tr>
<?php
}
?>
</table>
</td>
</tr>
</table>
So what I'm trying to do is to get user name, using two tables $tbl_name and $prtalbe using their unique_id. So, if unique_id from $tbl_name equals unique_id from $prtable, I want to show user's name from $prtalbe.
I've been trying another sql query:
$sql = "SELECT * FROM $tbl_name, $prtable WHERE $tbl_name.unique_id = $prtable.unique_id;
$result=mysql_query($sql);
Then doing while loop to get it working
while($rows=mysql_fetch_array($result)){
$rows['name'];
}
and it did work actually, but it didn't want to put it right into my code, since ID from $prtable and $tbl_name are different.
Try this:
$sql = "SELECT $prtable.username FROM $tbl_name INNER JOIN $prtable ON ($tbl_name.unique_id = $prtable.unique_id)";
When you call INNER JOIN you are fetching all rows from each table and combining them given the ON condition. For more information, see this: http://www.w3schools.com/sql/sql_join_inner.asp

Categories