Jump to content
Html

Remove data

Recommended Posts

Hi,

I just wanted some thoughts, out of these examples, what would the best choice to add to this code I have, which is code example from a book php In Easy steps

<?php
if( $_SERVER[ 'REQUEST_METHOD' ] == 'POST' )
{
  $name = $_FILES[ 'image' ][ 'name' ] ;
  $temp = $_FILES[ 'image' ][ 'tmp_name' ] ;
  $size = $_FILES[ 'image' ][ 'size' ] ;
  $ext = pathinfo( $name , PATHINFO_EXTENSION ) ;
  $ext = strtolower( $ext ) ;
  if( $ext != 'png' && $ext != 'jpg' && $ext != 'gif' )
  { echo 'Format must be PNG, JPG, or GIF' ; exit() ; }  
  if( $size > 512000)
  { echo 'File size must not exceed 500Kb' ; exit() ; }
  if( file_exists( $name ) )
  { echo 'File '.$name.' already uploaded' ; exit() ; }
  try
  {
    move_uploaded_file( $temp , $name ) ;
    echo 'File uploaded : '.$name ;
    echo '<br><img src="'.$name.'">' ;
  }
  catch( Exception $e )
  { echo 'File upload failed!' ; }
} 
?> 

https://www.w3schools.com/php/php_mysql_delete.asp

So I can tuck in a user_id code for this particular page, and have a button to remove the logged in user instantly or within a time frame.

Thanks

Share this post


Link to post
Share on other sites

I should add extensions in a array then check if file current extension is in that array using php in_array() function. Also jpeg files can have 'jpeg', 'jpe' extensions

Share this post


Link to post
Share on other sites

The image upload feature is not connected with removing a logged in user.

I want to add the remove feature as an extra, on this setting page.

Share this post


Link to post
Share on other sites

The only thing the code you posted does is (badly) process an uploaded file.  If your question doesn't have to do with uploading files, why did you post that code?  What does that code have to do with whatever else you're asking?

Share this post


Link to post
Share on other sites

Sure, well that example is from the book I have, that was how I got what I have, and the other files were from the site, which is from another book, which donesuk found, I think now.

I simply want to add to that php code, a remove data feature, a button which actives removing a logged in user from the db, isn't that possible? As you can understand, it is a setting page so upload feature and a remove button is now what I want to add.

I had tried with a user on here to get a profile page using existing code, but that unfortunately didn't work, I don't know, the code just messes up when new code is added so it was a nice try, but it didn't work out. I probably need custom code for a user site system. But for the moment, what I have works and is better than what i had used before, which was the junk from the youtube video.

So this code above needs $user_id tagged in, so the remove feature would know that the user Bob or what ever is logged in and is then removed. Where do I go with this, I looked at the W3schools, and read a few pages, but I'm not sure.

What can you hint to me or suggest?

 

Share this post


Link to post
Share on other sites

a button which actives removing a logged in user from the db, isn't that possible?

Of course it's possible.  Like the page on deleting shows, if you want to delete a particular row in the database you need to be able to uniquely identify that row in the SQL query.  So, pass whatever unique identifier you're using to a page that can get the ID and delete the corresponding row.  You can put the ID in the link in the button if you want to, or create a form and pass it that way (a dropdown to select a user to delete, or however else you want to do it).

I wouldn't recommend using that file upload code, the $_FILES array contains an error code which the code above doesn't check.

http://php.net/manual/en/features.file-upload.post-method.php

http://php.net/manual/en/features.file-upload.errors.php

Share this post


Link to post
Share on other sites

I found this video tutorial on a login system, I don't know much about php, but this looks dated. How ever, he did used an up to date encryption. Claims to be using Mysqli, and PDO or something, could be in date as well some of it out of date. Unfortunately that is all it appears to do.

https://youtu.be/LC9GaXkdxF8?t=2346

As for the a remove user link code, it will be the logged in user, like you registered and no longer want to be on the db, it isn't an admin account. Just a regular user.

Since the setting.php is a separate page and not related to the login system I have. I may need to add the session_id code that is on the actual login page and logged in subsequent page after.

# Access session.
session_start() ; 

# Redirect if not logged in.
if ( !isset( $_SESSION[ 'user_id' ] ) )
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

# Access session.
session_start() ; 

# Redirect if not logged in.
if ( !isset( $_SESSION[ 'user_id' ] ) )
  
if( $_SERVER[ 'REQUEST_METHOD' ] == 'POST' )
{
  $name = $_FILES[ 'image' ][ 'name' ] ;
  $temp = $_FILES[ 'image' ][ 'tmp_name' ] ;
  $size = $_FILES[ 'image' ][ 'size' ] ;
  $ext = pathinfo( $name , PATHINFO_EXTENSION ) ;
  $ext = strtolower( $ext ) ;
  if( $ext != 'png' && $ext != 'jpg' && $ext != 'gif' )
  { echo 'Format must be PNG, JPG, or GIF' ; exit() ; }  
  if( $size > 512000)
  { echo 'File size must not exceed 500Kb' ; exit() ; }
  if( file_exists( $name ) )
  { echo 'File '.$name.' already uploaded' ; exit() ; }
  try
  {
    move_uploaded_file( $temp , $name ) ;
    echo 'File uploaded : '.$name ;
    echo '<br><img src="'.$name.'">' ;
  }
  catch( Exception $e )
  { echo 'File upload failed!' ; }
} 
?> 

So this is what I think it should be setup like

Share this post


Link to post
Share on other sites

I added the User_id from the other core file, and that now makes it so that the setting.php page can only be accessible by logging in. So that is sorted.

Now the button or link,

# On success retrieve user_id, first_name, and last name from 'users' database.
if (empty($errors)) {
  $q = "SELECT post_date, message, comment_user_id FROM forum";
  $r = mysqli_query($dbc, $q);

  $row = mysqli_fetch_array($r, MYSQLI_ASSOC);
DELETE FROM table_name WHERE condition;
# On success retrieve user_id, first_name, and last name from 'users' database.
if (empty($errors)) {
  $q = "DELETE FROM Customers WHERE CustomerName='Alfreds Futterkiste';";
  $r = mysqli_query($dbc, $q);

  $row = mysqli_fetch_array($r, MYSQLI_ASSOC);

Taking from the core file for the user login code, I put this together, probably won't work correctly, but I think this is the correct step. May be the last line isn't necessary. So this would go below the upload code, with a bracket to separate it.

  }
  catch( Exception $e )
  { echo 'File upload failed!' ; }
} 

# On success retrieve user_id, first_name, and last name from 'users' database.
if (empty($errors)) {
  $q = "DELETE FROM users WHERE $user_id='Alfreds';";
  $r = mysqli_query($dbc, $q);

  $row = mysqli_fetch_array($r, MYSQLI_ASSOC);
{

?>

 

Share this post


Link to post
Share on other sites

# Redirect if not logged in.
	if ( !isset( $_SESSION[ 'user_id' ] ) )

What are you doing with that code?  You have an if statement there.  If that is true, then what happens?  All you have is an if statement.  The way you have your code structured, PHP considers the if statement which follows that to be whatever you want to do if the above if statement is true.  So you are telling it to only process the uploaded file if the user is not logged in.  What's the point of that?  Are you just copying and pasting bits of code around without knowing what they do?

if (empty($errors)) {
	  $q = "DELETE FROM users WHERE $user_id='Alfreds';";
	  $r = mysqli_query($dbc, $q);

Unless a variable called $errors has a truthy value, you're either going to delete every user or no user depending on whether or not $user_id is set to "Alfreds".  If it is, then you'll delete every user.  And you always run that code unless $errors is set and has a truthy value.

Share this post


Link to post
Share on other sites

Well, that was how the profile page would of had worked with that, but the way the core files are setup is, the session and user id works by having that so the page visited isn't accessible by typing in the link, it can only be accessed by a registered user. It works for home.php which is after login.php so why not use that? Obviously you sort of understand the plan now.

As for the remove data, well I really don't know, I was just thinking may be it can work that way? Should I remove the if statement from the sql code part, if it doesn't work with it then obviously it has to go. As for the example, Alfreds isn't a registered name, so the name or an id number is what is necessary.

Share this post


Link to post
Share on other sites

It works for home.php which is after login.php so why not use that?

I don't think you're understanding what I said.  An if statement has 2 parts - the condition, and what to do if the condition is true.  

if (condition) {
	  do this
	}

Look at your code again.  You have the if statement checking if the user ID is in the session, but what does it do if it's not?  Look at the code and tell me what it's going to do if the user is not logged in.  The comment right before the if statement says to redirect if they're not logged in.  But you must have forgotten to add the actual code to redirect them, because the only thing you have is an if statement.  The end result of that code is that it will only process the uploaded file if the user is not logged in.  That seems like a weird rule.

As for the remove data, well I really don't know, I was just thinking may be it can work that way? Should I remove the if statement from the sql code part, if it doesn't work with it then obviously it has to go.

Didn't you say that you wanted a button or link to trigger the delete?  That's what your if statement should check for - did they click on that.

Share this post


Link to post
Share on other sites

The setting.php page isn't accessible without being logged in due to adding the user_id code as I displayed in the example.

And sure the code above is what I'm trying to get to.

This is on the home page after logged in, so that is where I thought to use that on the setting as I've stated,

<?php # DISPLAY COMPLETE LOGGED IN PAGE.

# Access session.
session_start() ; 

# Redirect if not logged in.
if ( !isset( $_SESSION[ 'user_id' ] ) ) { require ( 'login_tools.php' ) ; load() ; }

# Set page title and display header section.
$page_title = 'Home' ;


# Display body section.
echo "<h2>Profile home</h2><p>You are now logged in, {$_SESSION['first_name']} {$_SESSION['last_name']} </p>";

If I remove the if statement, if it doesn't have a purpose for setting page?

Share this post


Link to post
Share on other sites

That's not the same code you posted for the other page.

Post the complete file that you're working on, not only part of it.

Share this post


Link to post
Share on other sites

That is the complete file, setting.php is there, that above is from home.php, that isn't related to trying to remove a logged in user. It isn't as if that file has anything on removing a user.

Perhaps you are referring to the login_tools part, sure that is added in the setting.php page.

{ require ( 'login_tools.php' ) ; load() ; }
Edited by Html

Share this post


Link to post
Share on other sites

Sure I posted above, setting,

Well if you include not just the php code.

<?php
?>
<html>
<body>
<img src="header2018may15.jpg">
<br />
<p><b>User settings</b></p>
<form method="POST" action="<?php $_SERVER[ 'PHP_SELF' ] ?>" enctype="multipart/form-data">
Select an image to upload :
<input type="file" name="image" >
<br />
<br />
<input type="submit" value="Upload Image" >
</form> 
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

# Access session.
session_start() ; 

# Redirect if not logged in.
if ( !isset( $_SESSION[ 'user_id' ] ) ) { require ( 'login_tools.php' ) ; load() ; }
  
if( $_SERVER[ 'REQUEST_METHOD' ] == 'POST' )
{
  $name = $_FILES[ 'image' ][ 'name' ] ;
  $temp = $_FILES[ 'image' ][ 'tmp_name' ] ;
  $size = $_FILES[ 'image' ][ 'size' ] ;
  $ext = pathinfo( $name , PATHINFO_EXTENSION ) ;
  $ext = strtolower( $ext ) ;
  if( $ext != 'png' && $ext != 'jpg' && $ext != 'gif' )
  { echo 'Format must be PNG, JPG, or GIF' ; exit() ; }  
  if( $size > 512000)
  { echo 'File size must not exceed 500Kb' ; exit() ; }
  if( file_exists( $name ) )
  { echo 'File '.$name.' already uploaded' ; exit() ; }
  try
  {
    move_uploaded_file( $temp , $name ) ;
    echo 'File uploaded : '.$name ;
    echo '<br><img src="'.$name.'">' ;
  }
  catch( Exception $e )
  { echo 'File upload failed!' ; }
} 

?> 
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<img src="bottomheader.jpg">
</body>
</html>

So below the bottom curly bracket I want to add this new feature so use an image.

Share this post


Link to post
Share on other sites

The first thing you need to do is put your PHP code on the top of your file.  Sending headers like session cookies or redirecting doesn't work if you already sent a bunch of HTML or other output.  Assuming that load function actually redirects the user, that code is fine, not all of that was included in previous replies.

Share this post


Link to post
Share on other sites

Sure,  moved the code. I did find a php/sql video on removing data from a web page connected to a db on youtube. I haven't tried the example, it is narrated well I guess.

PHP MYSQL Tutorial 4 | How To Delete A MYSQL Row In PHP - YouTube

As for getting what I want, could this clip be fine to get what I need going, using what I have above.

Having watched it again, at six minutes, the narrator explains how to remove a list or id entry from the db, so I've started below the curly bracket, 'function deleteRecord' I of course will want to use an image for the link to do this. He used a simple html button for the page.

He used an id as variable, the obvious ones in the example code is $q and just the row and dbc connection listed. That was a problem with trying to get the profile page working, it needed that variable.

<?php
if( $_SERVER[ 'REQUEST_METHOD' ] == 'POST' )
{
  $name = $_FILES[ 'image' ][ 'name' ] ;
  $temp = $_FILES[ 'image' ][ 'tmp_name' ] ;
  $size = $_FILES[ 'image' ][ 'size' ] ;
  $ext = pathinfo( $name , PATHINFO_EXTENSION ) ;
  $ext = strtolower( $ext ) ;
  if( $ext != 'png' && $ext != 'jpg' && $ext != 'gif' )
  { echo 'Format must be PNG, JPG, or GIF' ; exit() ; }  
  if( $size > 512000)
  { echo 'File size must not exceed 500Kb' ; exit() ; }
  if( file_exists( $name ) )
  { echo 'File '.$name.' already uploaded' ; exit() ; }
  try
  {
    move_uploaded_file( $temp , $name ) ;
    echo 'File uploaded : '.$name ;
    echo '<br><img src="'.$name.'">' ;
  }
  catch( Exception $e )
  { echo 'File upload failed!' ; }
}
function deleteRecord(mysqli 
?>
<html>
<body>
<img src="header2018may15.jpg">
<br />
<p><b>User settings</b></p>
<form method="POST" action="<?php $_SERVER[ 'PHP_SELF' ] ?>" enctype="multipart/form-data">
Select an image to upload :
<input type="file" name="image" >
<br />
<br />
<input type="submit" value="Upload Image" >
</form>  
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<img src="bottomheader.jpg">
</body>
</html>

 

So below I made a start of adding this video tutorial's code, and as for adding an image, I left that incomplete, not sure where to go with this, may be it needs a form code tag.

<?php
if( $_SERVER[ 'REQUEST_METHOD' ] == 'POST' )
{
  $name = $_FILES[ 'image' ][ 'name' ] ;
  $temp = $_FILES[ 'image' ][ 'tmp_name' ] ;
  $size = $_FILES[ 'image' ][ 'size' ] ;
  $ext = pathinfo( $name , PATHINFO_EXTENSION ) ;
  $ext = strtolower( $ext ) ;
  if( $ext != 'png' && $ext != 'jpg' && $ext != 'gif' )
  { echo 'Format must be PNG, JPG, or GIF' ; exit() ; }  
  if( $size > 512000)
  { echo 'File size must not exceed 500Kb' ; exit() ; }
  if( file_exists( $name ) )
  { echo 'File '.$name.' already uploaded' ; exit() ; }
  try
  {
    move_uploaded_file( $temp , $name ) ;
    echo 'File uploaded : '.$name ;
    echo '<br><img src="'.$name.'">' ;
  }
  catch( Exception $e )
  { echo 'File upload failed!' ; }
}

function deleteRecord(mysqli $dbc, $){
$sql ="DELETE FROM '' WHERE id ='".$."'";
$result = $db->query($sql);

?>
<html>
<body>
<img src="header2018may15.jpg">
<br />
<p><b>User settings</b></p>
<form method="POST" action="<?php $_SERVER[ 'PHP_SELF' ] ?>" enctype="multipart/form-data">
Select an image to upload :
<input type="file" name="image" >
<br />
<br />
<input type="submit" value="Upload Image" >
</form>
<br />
<input type="image" name"image">
<input type="image" src="remove.jpg" alt="Remove">  
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<img src="bottomheader.jpg">
</body>
</html>

an If statement i guess is what is below $result

if(!result){

throw new Exception( message: 'Cannot delete'

     }

}

so after that, the narrator goes to another page delete.php so that is connected with the button. So I guess remove.jpg goes to a remove.php which contains some more code to complete the task.

Edited by Html
Watched the clip again

Share this post


Link to post
Share on other sites

As for getting what I want, could this clip be fine to get what I need going, using what I have above.

I don't know, I'm not going to watch the 20 minute video.  Deleting is simple, you just tell the database which row or rows you want to delete using the WHERE clause.  That's it, that's all there is to it.  You already know which row you want to delete because the ID is in the session.  So you don't even need to think about that.  The only thing to think about is how to tell it you want to delete the user, and you can put a variable in the URL querystring to tell the code what you're trying to do.  I don't know if it really takes 20 minutes to explain all that, I assume in that video you're picking which row to delete instead of already having that ID in the session.

Also, instead of a ton of br tags on a page, use CSS to set things like margin, padding, height, etc.  You could just put a top margin on that bottom image.

Share this post


Link to post
Share on other sites

The narrator has five pages, select, delete, db,  so it is a page with listings and the code is about removing one or any of them using a link next to them. I obviously want that but for the logged in user, so the code must remove user with id 1. And not id 2 of another user.

So the code above is a step in the right direction, it is after the remove.php I must create, but to get that image to go to the remove.php

<input type="image" src="remove.jpg" alt="Remove">  

 

 

Share this post


Link to post
Share on other sites

I know, you've already said that.  So, you don't have to figure out which user to delete because you already know what the ID is.  You just have to tell the page that the action you're trying to perform is delete, that's all.  You have all the other information the code needs to delete the current user.

So the code above is a step in the right direction, it is after the remove.php I must create, but to get that image to go to the remove.php

Yeah, make it a link.

I thought you were doing this all on one page which is why you posted the image upload code in a thread about deleting a user, are you separating it into pages now?

Share this post


Link to post
Share on other sites

No I want the link on the same page which I have displayed above, the setting page as is, doesn't work with that code I added below the upload image code.

Here is the up to date page.

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

# Access session.
session_start() ; 

# Redirect if not logged in.
if ( !isset( $_SESSION[ 'user_id' ] ) ) { require ( 'login_tools.php' ) ; load() ; }
  
if( $_SERVER[ 'REQUEST_METHOD' ] == 'POST' )
{
  $name = $_FILES[ 'image' ][ 'name' ] ;
  $temp = $_FILES[ 'image' ][ 'tmp_name' ] ;
  $size = $_FILES[ 'image' ][ 'size' ] ;
  $ext = pathinfo( $name , PATHINFO_EXTENSION ) ;
  $ext = strtolower( $ext ) ;
  if( $ext != 'png' && $ext != 'jpg' && $ext != 'gif' )
  { echo 'Format must be PNG, JPG, or GIF' ; exit() ; }  
  if( $size > 512000)
  { echo 'File size must not exceed 500Kb' ; exit() ; }
  if( file_exists( $name ) )
  { echo 'File '.$name.' already uploaded' ; exit() ; }
  try
  {
    move_uploaded_file( $temp , $name ) ;
    echo 'File uploaded : '.$name ;
    echo '<br><img src="'.$name.'">' ;
  }
  catch( Exception $e )
  { echo 'File upload failed!' ; }
} 

function deleteRecord(mysqli $dbc, $id){
$sql ="DELETE FROM 'users' WHERE id ='".$id."'";
$result = $db->query($sql);
if(!result){

throw new Exception( message: 'Cannot delete';

     }
} 
?> 
<html>
<body>
<img src="header2018may15.jpg">
<br />
<p><b>User settings</b></p>
<form method="POST" action="<?php $_SERVER[ 'PHP_SELF' ] ?>" enctype="multipart/form-data">
Select an image to upload :
<input type="file" name="image" >
<br />
<br />
<input type="submit" value="Upload Image" >
</form> 
<br />
<br />
<input type="image" src="remove.jpg" alt="Remove"> 
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<img src="bottomheader.jpg">
</body>
</html>

I don't know what the problem could be, may be no $q variable could be the reason since that is part of the core files code. But I honestly not quite sure.

As for the link, I don't know how to quite do that, I put that above as I stated, so not sure how to get this working.

The remove.php which I did create, but you're probably thinking that isn't needed? I based this off the youtube clip.

<?php
# Connect to the database.
  require ('connect_db.php');

# Access session.
session_start() ;

$id
?>

This isn't needed.

Edited by Html
Added remove.php code example

Share this post


Link to post
Share on other sites

The problem is your PHP code has syntax errors, it's not written correctly.  Your deleteRecord function isn't correct, review it.  You also are not actually running the function anywhere.

As for the link, I don't know how to quite do that, I put that above as I stated, so not sure how to get this working.

If you don't know how to put a link on a page, then you need to start with the basic HTML tutorial.

This isn't needed.

Even if you fixed the syntax error that you have there, those 3 lines of code aren't doing anything.  You require a file, start the session, and then just write a variable name.  Writing a variable name doesn't do anything.  Also for some reason your deleteRecord function is in a different file.  Shouldn't that function be defined in the file where you want to actually do the delete?

Share this post


Link to post
Share on other sites

The setting page has two features, one of them is the link as stated more than one, so I can just use hyperlink code as an image to activate the SQL. Sure I learnt the hyperlink code more than fourteen years ago, something I never forgot, I didn't know about the image code for a form submit button.

I am only going on from the tutorial, so the remove.php is what you are getting at, needs to have the function code?

I added the code into remove.php, I think I may of got it to work, just need the link in the setting.php page to activate it.

Edited by Html
Added the code to remove page

Share this post


Link to post
Share on other sites

I am only going on from the tutorial, so the remove.php is what you are getting at, needs to have the function code?

Whatever file you're going to run to do the delete needs the code in it to do the delete.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...