Jump to content
Html

Does a remove record link require a particular link?

Recommended Posts

Hi,

If you have remove.php or remove.php?=recordid ?> or

I've seen this in numerous clips, some also include $sql

$sql = 'SELECT * FROM users' WHERE id ='".$"'";
  $r = mysqli_query( $dbc , $sql ) ;

Quote

$num = mysqli_num_rows( $r ) ;
   if ( $num > 0 )

Is the above even needed in a function, I read in the php manual, it refers to the rows

Share this post


Link to post
Share on other sites

I don't know what you're asking.  No code is "needed," it's up to you to decide what you want your program to do.  Whether you put code in a function or not really only matters for reuse.  And if you want to refer to a specific record in the database, you just need to know how to uniquely identify it.  Most often that means passing an ID.

If you're asking about mysqli_num_rows, if you read the description in the manual it says that it returns the number of rows in the result set.  So if you're asking if you need to use that function, then I guess that depends whether you care how many rows are in the result set.  If you care, then yeah, that's how you find out.

Share this post


Link to post
Share on other sites

I never did get a response on how to remove the data from a db from the author of the example code. That was disappointing. I even went through some php 5 example code from one of their books, on the site, there is only this.

<!-- example for PHP 5.0.0 final release -->

<?php 	
	function delete( $file )
	{
	  if( unlink( $file ) )
	  { 
	    echo( "$file<br>has been deleted<hr>" ); 
	  }
	  else
	  { 
	    echo( "Unable to delete $file<hr>" ); 
	  }
	}
?>

Every where has a different way of removing data. I am trying to remove a stored user on the db, so the above code doesn't quite do that, or in my first post. I am not sure if $sql and $q is necessary in those line $r = mysqli_query( $dbc , $sql, $q ) ;

remove.php should be fine remove.php?id=1

😦

Share this post


Link to post
Share on other sites

unlink($file) file is a FILE, Not! record from database table.

WHAT did you use to add record to database table?  Maybe INSERT in an SQL perhaps?

Now which of these would you use to do what you want, once you have the id ref of the user whose record row you wish to remove

INSERT,

UPDATE,

SELECT,

DELETE.

This is very basic which you should understand, its all covered in tutorials.

Share this post


Link to post
Share on other sites

Sure, I get what you've stated, I've shown that n the code example which doesn't do anything.

Considering the code is setup with this $q variable, I guess it requires it in the code, as other examples from the book's examples files include this.

function deleteRecord( $dbc )
{
  $q = 'SELECT * FROM users' WHERE id ='".$user_id."'";
  $r = mysqli_query( $dbc , $q ) ;
  $num = mysqli_num_rows( $r ) ;
   if ( $num > 0 )
  {

 

Before I thought about this^

This was what I was using, and that obviously didn't work.

<?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('Cannot  delete' );

     }
}
?>

Well yes the session needs to be at the top.

Edited by Html

Share this post


Link to post
Share on other sites
<?php
# Access session.
session_start() ;

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

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

throw new Exception('Cannot  delete' );

     }
}
?>

 

 

<?php

# Access session. session_start() ;

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

function deleteRecord( $dbc )
{
  $q = 'SELECT * FROM users' WHERE id ='".$user_id."'";
  $r = mysqli_query( $dbc , $q ) ;
  $num = mysqli_num_rows( $r ) ;
   if ( $num > 0 )
  {

?>

Which one is correct to use? $user_id is fine instead of $id.

The second one has this error, the top code has no errors according to php checker.

Error: There are 2 more opening curly braces '{' found
This count is unaware if curly braces are inside of a string

PHP Syntax Check: Parse error: syntax error, unexpected 'WHERE' (T_STRING) in your code on line 9

$q = 'SELECT * FROM users' WHERE id ='".$user_id."'";

Edited by Html
check code result

Share this post


Link to post
Share on other sites

Ha ha so you think defining delete function around SELECT query? (WHY? are doing this anyway, its already delete fuction)  would magically make it DELETE query? THINK ABOUT IT! Which is the correct one? Bit obvious isn't it! Its all there what you want, correct type of query plus its correctly  structured with correct matching number of opening and closing curly braces and correct use and placement of single quotes. Again this very, very, very very, very very basic.

Share this post


Link to post
Share on other sites

So the second requires ten single quotes? The sql line being $q = 'SELECT * FROM users' WHERE id ='".$user_id."'";

And delete not select. Well I thought select would be fine,  considering the function is a delete one, so this should remove a user logged into the site, from the mysql db?

<?php
# Access session.
session_start() ;

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

function deleteRecord( $dbc )
{
  $q = 'DELETE * FROM users' WHERE id = '".$user_id."'";
  $r = mysqli_query( $dbc , $q ) ;
}
# Close database connection.
mysqli_close( $dbc ) ;
?>

This is okay even without the rows bit of code. Either way, the code doesn't remove the logged in user from the db of the id column.

<?php
# Access session.
session_start() ;

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

function deleteRecord( $dbc )
{
  $q = 'DELETE * FROM users' WHERE id = '".$user_id."'";
  $r = mysqli_query( $dbc , $q ) ;
   $num = mysqli_num_rows( $r ) ;
   if ( $num > 0 )
}
# Close database connection.
mysqli_close( $dbc ) ;
?>

Edited by Html
Code mess around

Share this post


Link to post
Share on other sites

Seriously? Just because the function name has delete you think dumping any sql statement  inside it, WOULD work to delete?

You want to insert a NEW record you would use INSERT sql, you want to update existing record you would use UPDATE sql, you want to select specific existing record/s you would SELECT sql, NOW then, what sql type would use to delete a existing record? And to question about single quotes, no! you don't need 10 single quotes, just required, properly placed, single quotes.

Share this post


Link to post
Share on other sites

Okay, well delete is what is the keyword to use for the sql, obviously. Well, I don't know what more the code requires, I am basing it off from videos, and just got the idea to edit the code to include what the example code has. I may not be going the correct way, but I'm close to completing this.

As for the quotes, I took the code as is from the example code, so it should be perfectly fine, just needs a row name, which is user_id

So now what is next? Does it need more code or just editing what is there?

<?php
# Access session.
session_start() ;

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

function deleteRecord( $dbc )
{
  $q = 'DELETE * FROM users' WHERE id = '".$user_id."'";
  $r = mysqli_query( $dbc , $q ) ;
   $num = mysqli_num_rows( $r ) ;
   if ( $num > 0 )
}
# Close database connection.
mysqli_close( $dbc ) ;
?>

Share this post


Link to post
Share on other sites

LOOK at original  code before the usual cut and paste without understanding  what you were doing took place.

LOOK how single quotes are placed.

JUST LOOK! and understand, and lose the PROBLEMATIC "CUT and PASTE and hope it works".

This is my last comment on this! If you can't be bothered to learn and understand, why should i/we waste  my/our time continuously going around in circles going over the same issues everytime.

Share this post


Link to post
Share on other sites

Yeah, short fuse guy.

<?php
# Access session.
session_start() ;

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

function deleteRecord( $dbc )
{
  $q = 'DELETE * FROM users' WHERE id = '".$user_id."'";
  $r = mysqli_query( $dbc , $q ) ;
   $num = mysqli_num_rows( $r ) ;
   if ( $num > 0 )
}
# Close database connection.
mysqli_close( $dbc ) ;
?>

 

This is correct to use? The problem appears to be with the sql line, as it has been.

There is ten quotes, three of them are speech marks as we have here " ' "

' FROM users etc' = ' "

The original code, I copied from one of the example files, I thought I was onto something by putting together what I have. I'm all over the place with this.

I logged into the free web host, I was lucky, no hardware failure affected the mysql db, it did on two other servers.

I tried this $q = 'DELETE * FROM users' WHERE id ="'".$user_id."'"; So two speech marks, and one quote, in the code the $user_id turned blue, in the ftp editor. That hasn't changed anything.

Edited by Html
Added quotes example

Share this post


Link to post
Share on other sites

The code in your post above has syntax errors, but even if it did not, it would not do anything because the function is not being invoked. We're going to have to go through exercises to make sure you understand all of the code that you're copying. Please go line by line and describe to me exactly what each line does and why it's there.

Here is the tutorial page for the SQL delete statement: https://www.w3schools.com/sql/sql_delete.asp Please read it any try to understand it, do not just copy the code.

Share this post


Link to post
Share on other sites

As  I have tried before, I viewed the youtube clips that is where I have got any idea on this code.

According to the example, which is different from the code example from the Php & Mysql example code.

 

<?php
# Access session.
session_start() ;

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

function deleteRecord( $dbc )
{
  $q = 'DELETE * FROM users' WHERE id = '$user_id';
  $r = mysqli_query( $dbc , $q ) ;
   $num = mysqli_num_rows( $r ) ;
   if ( $num > 0 )
}
# Close database connection.
mysqli_close( $dbc ) ;
?>

Edited by Html
Edited example code with W3schools example

Share this post


Link to post
Share on other sites

Please describe what each of those lines of code is doing and why it is needed.

I can't help you learn if I don't know which parts you don't understand.

Share this post


Link to post
Share on other sites

function deleteRecord( $dbc )

That is the function declared

Then the opening brace following, the sql line, and then rest is rows and connection as well the $q variable that is a part of the example code consistency, a closing brace, that is that at its bare bones. Not sure if that part is even necessary.   $num = mysqli_num_rows( $r ) ; if ( $num > 0 )
{
  $q = 'DELETE * FROM users' WHERE id = '$user_id';
  $r = mysqli_query( $dbc , $q ) ;
   $num = mysqli_num_rows( $r ) ;
   if ( $num > 0 )
}

Share this post


Link to post
Share on other sites

What does this line do?

$q = 'DELETE * FROM users' WHERE id = '$user_id';

And this one?

$r = mysqli_query( $dbc , $q ) ;

And these two?

$num = mysqli_num_rows( $r ) ;
if ( $num > 0 )

Can you describe to me how functions work?

Share this post


Link to post
Share on other sites

The last two no.

The first and second,

The first is the sql line the variable is instructed to remove the logged in user from the id column. And the second line is the database connection and the variable part of the mysqli function. Not sure what else.

Share this post


Link to post
Share on other sites

The first line is just the assignment of a string to a variable. It actually does not do anything at all on its own.

As for the other two lines, at least one of them should make the least bit of sense to you. You should know by now about variables and if statements and understand what this line is doing:

if ( $num > 0 )

 

As for this part of the code:

function deleteRecord( $dbc ) 

It is declaring a function, but the function never runs, so none of the code inside the function is doing anything. You need to add some code that explicitly runs this function.

Share this post


Link to post
Share on other sites

Well, I got my source from the youtube site, no where else. One video shows one example, and then another.

So I just tried what there was. So what is the step to get the function going then?

Share this post


Link to post
Share on other sites

There is a lot there on the functions. The first one seems pretty basic, and useless the rest go onto something else.

I thought the SQL line was correct? So now it isn't. 🙄

So what, there is no longer the use for WHERE?

Share this post


Link to post
Share on other sites

The WHERE clause isn't the problem.  You have both PHP syntax errors, and also SQL syntax errors, all in a single line of code.  And those errors are there, and you don't see them, because you never bothered to understand the basics of what you're trying to use.

I don't know if you've done much with electrical engineering, but I'll try to use an analogy.  Imagine you're an electrical engineer, you've been designing circuits all of your life.  You know how resistors, capacitors, etc work.  You know the fundamentals, like the fact that a circuit has to be complete for any electricity to flow at all.  Now, someone comes to you and they're trying to design a circuit, and they show you a circuit diagram.  They have a 5v power source, and a switch, and a 5v light, and they're trying to make a circuit where you flick the switch, and the light turns on.  For anyone calling themselves an electrical engineer, this is just about the most basic circuit imaginable.  But this person shows their circuit diagram to you, and they have a bunch of resistors and capacitors in there for some reason, so you ask them why.

"Well, because every other circuit diagram I looked at had those, so I figured I needed them, so I put them in mine."

OK, but where you put them doesn't make sense.  In fact, they don't need to be there at all.  Why do you think they need to be there?

"I don't know, the ones I looked at had them."

OK, but what does a resistor actually do?

"I don't know."

OK, well just remove the resistors, remove the capacitors, and hook the wires up.

"OK, I hooked up the wires, what about now?"

Well, now the diagram only shows one wire from each part connected.  The circuit isn't complete.  How is  electricity going to flow through an incomplete circuit?

"I don't know."

And for some reason you just moved the resistor to the negative terminal of the light bulb, why did you do that?

"Other circuits had resistors."

But what does a resistor actually do, do you need it here?

"I don't know."

Now a year passes, and this person comes back with the same diagram, with the same problems.  Aren't you going to get just a little bit frustrated that the person is still trying to design the most basic circuit imaginable and has STILL never bothered to learn the basics about what they're using?

That's what's going on here.  You've been at this for well over a year, and for some reason you absolutely refuse to learn the basics.  You pick up a book, skip all of the opening chapters, flip through the one chapter that's remotely similar to the thing you're trying to make, don't understand it, and conclude the book is useless.  It would make perfect sense if you bothered to read the basics, but for some reason you think you don't need to.  Every person trying to help you knows the basics.  Every person who has ever written any non-trivial functional program understands the basics.  You don't think you need to understand the basics, for some reason that I cannot comprehend.

It doesn't matter what trade or skill you're trying to learn, if you don't learn the basics you're not going to understand anything.  You can't expect to pick up a book about how engines work, skip to the chapter about spark plugs without reading anything else, and expect to be able to diagnose and fix any problem with spark plugs.  You don't know how the entire system works together.

This is no different.  This entire thread has been trying to call your attention to the smallest, most basic of errors, and you remain completely incapable of detecting them because you never learned the basics.  Nothing else matters if you're not going to learn the basics.  Fixing this code for you will not teach you a single thing, because you will not even be able to detect all of the changes in the first place, and you will have absolutely zero idea why those changes were necessary to fix the code.

Share this post


Link to post
Share on other sites

Unfortunately, I was like with this python and visual basic. Your post reminds me of when I had tried to this, I seem to switch off on the learning curve.

I did try the php 7 book by ineasysteps, it wasn't all that, just code really that works, very basic explanation. The book you suggested is much more thick with explanation, but I couldn't quite get it.

Quote

You have both PHP syntax errors, and also SQL syntax errors, all in a single line of code.

 

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...