Jump to content

Remove data


Html

Recommended Posts

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

# Access session.
session_start() ;

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

throw new Exception( message: 'Cannot delete';

     }
}
?>

This is the code for the remove.php link on the setting page, it doesn't do anything, so the code is sloppy or incomplete in someway. I did add the $user_id as that is what it is in the table.

I tried replacing the $user_id with $q as that is listed in other files. I kept the $user_id for the Where part of that code.

I also added c to db, that is how it is in the other files of the login files.

And his is the simple link on setting.php

<a href="remove.php"><img src="remove.jpg"></a>
<br />
<br />
<br />
Link to comment
Share on other sites

  • Replies 56
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Posted Images

That's correct, the code has syntax errors.  You are also not executing the function, so even if the syntax errors were fixed it still wouldn't do anything because all that file does is require another file, then start the session, then define a function.  You don't run the function.

Link to comment
Share on other sites

Hmm, okay, so I'm in the correct direction then.

I was only going from the tutorial, what I could sort of picture, and understand to try and get what I wanted working. So then, to run this function and sort out the errors where do I go from here?

Link to comment
Share on other sites

You can't run anything with syntax errors, so start by fixing those.  Count the number of parentheses if you're not sure, and look into exceptions.  You're using syntax that PHP doesn't even use, like you're trying to mix Javascript syntax.

Once you figure out the syntax issues, put this at the top of your PHP code:

ini_set('display_errors', 1);
	error_reporting(E_ALL);

Link to comment
Share on other sites

I remembered to check a phpcodechecker site,

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

# Access session.
session_start() ;

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

throw new Exception( message  'Cannot  delete' );

     }
}
?>

Still gives ''Cannot delete'' (T_CONSTANT_ENCAPSED_STRING) in your code on line 13 

Link to comment
Share on other sites

I tried looking at the 3, 4, 5, not to sure on those.

Then code 1 in the comment section below.

<?php
    function foo(){
        $bar = 1;
        try{
            throw new Exception('I am Wu Xiancheng.');
        }catch(Exception $e){
            return $bar;
            $bar--; // this line will be ignored
        }finally{
            $bar++;
        }
    }
    echo foo(); // 2
?>

 

Link to comment
Share on other sites

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

throw new Exception( message  'Cannot  delete' );

     }
}
function deleteRecord(mysqli $dbc, $user_id){
        $sql ="DELETE FROM 'users' WHERE id ='".$user_id."'";
        $user_id = 1;
        try{
            throw new Exception('cannot delete.');
        }catch(Exception $q){
            return $user_id;
            $user_id--; // this line will be ignored
        }finally{
            $bar++;
        }
    }
    echo deleterecord(); // 2

I don't quite know, this going anywhere?🤨

Edited by Html
Added some more example code.
Link to comment
Share on other sites

That example isn't really necessary, it's just trying to describe what exceptions do and what they're for.

You're missing the point.  You have a syntax error in your code.  I showed you the manual so you can see examples of using exceptions.  I did not intend for you to just copy and paste one of the examples into your code.  That's not the point.  You need to identify and fix the syntax error in your code.  The manual shows you how to correctly use exceptions.  Look at that code, look at your code, and figure out what the syntax error is in your code.

The point is to understand the code you're using, not just copy and paste anything I link you to.

Link to comment
Share on other sites

I decided to take a glance at the book Php 7 In easy steps,

There is a catching exceptions example in it,  how ever I don't quite get that.

The example is more descriptive than the php manual.

<?php
function check_size( $num )
{
  if( $num < 10 )
  {
     throw new Exception( "Number : $num<br>Value must exceed 10" ) ;
  }
}
try
{
  check_size( 5 ) ;
}
catch( Exception $e )
{
  echo '<b>Size Exception!</b><br>'.$e->getMessage().'<hr>';
}
class CustomException extends Exception
{
   public function get_details()
  {
    $details = 'File : '.$this->getFile().
	'<br>Line : '.$this->getLine().
	'<br>'.$this->getMessage();
     return $details ;
   }
 }
function check_parity( $num )
{
  if( $num % 2 !== 0 )
  {
     throw new CustomException( "Number : $num<br>Value must be even" ) ;
  }
}
try
{
  check_parity( 5 ) ;
}
catch( CustomException $e )
{
  echo '<b>Parity Exception!</b><br>'.$e->get_details().'<hr>';
}

 

Link to comment
Share on other sites

If you go back and look at the syntax error, it's on the line where you throw the exception.  You don't have any code to catch it.

You need to focus on the line of code with the error.  It's a really simple, basic error and you need to be able to spot it.

Link to comment
Share on other sites

Sure I understand the error was on line 13, the cannot delete part. It can only be the word outside of the quote marks. The examples don't show what I have.

 

throw new Exception( message  'Cannot  delete' );
Edited by Html
Link to comment
Share on other sites

Yes, before I change my code, I will want to add a time setting to removing data, and possibly the option to reverse that. I will need to take the next step carefully, where I go with that or how I add to the code, I'm not sure.

Once the setting to remove data works, a new page would state the data has been removed. So logging in would bring up a new screen to end the timed setting before removal from the db.

I guess for the moment, er, get this code changed, but certainly the next step is a timed removal, with a statement. As it stands just going to remove.php only gives a blank screen, so suspect that would be the result with the change in code. I imagine so.

As for message that was from the video tutorial, I did comment on it, but the narrator never responded back.

Edited by Html
Added
Link to comment
Share on other sites

If you want to do a soft delete then add a field to the database to say whether it is deleted, and change that to true when you delete something rather than actually deleting it.  When you get data from the database only get rows where the deleted field is false.  You can also store a timestamp of when it was deleted and run a script occasionally to delete records that are old enough.  I've never heard of people doing a delayed delete, but a soft delete is common.

Link to comment
Share on other sites

For example, the account will be removed in three days from the time it was removed by the user.

The code has been changed, but no change in what it should do. 🤨

Edited by Html
Link to comment
Share on other sites

https://youtu.be/W-FkqWUz0eE?t=87

This clip here on removing comments, is similar to what I am trying to do with removing from the db, but what the narrator has displayed, isn't what I have in the code, now the code he uses is php 5 I would imagine, sort of similar to what I have from the book example code, Php & Mysql from the in Easy steps series.

The difference between the function I have from another video, and this one, is there is no variable defined, just $sql =

if(isset($post['comments])) {

$ = $_post

That is out of date.

At eight minutes

Edited by Html
Added some more outlook on this
Link to comment
Share on other sites

I really doubt you need Yet Another Video to tell you how to do this:

DELETE FROM table WHERE field = 'value'

Or, a soft delete:

UPDATE table SET deleted = 1 WHERE field = 'value'

It's just not a complex thing to do.

Are you really still not sure how to delete something from the database?  Are you still stuck on that?  If you have questions about your code, you need to post your current code and the questions you have.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...