Jump to content
Html

Does a remove record link require a particular link?

Recommended Posts

But this is very basic problems, if you can't get how to do these, you really should give these books away, and hire someone who CAN do this all for you, instead of us all dying of old age trying to get you past the basics it seems.

Share this post


Link to post
Share on other sites

Yeah, i guess, and sure I thought about that in the past, but I just felt I had to keep going with this example code to try and get what I wanted working.

Like I stated, I emailed the company who published the book, and they didn't respond to trying to make sense of my example code. Not sure what is going on there, could be that the book is too old for anyone there to want to add a tutorial on removing data. Seems obvious to include that with a user site login, I am sure everybody here thinks that is a must.

 <?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 )
}
deleterecord(); // call the function

# Close database connection.
mysqli_close( $dbc ) ;
?> 

I don't know.

Edited by Html
Added new line based of W3schools example.

Share this post


Link to post
Share on other sites

Personally, I don't think books should just be giving examples of how to do things at all. They should be focusing on teaching the material and only provide examples that help solidify understanding that material.

A good book wouldn't tell you how to make a login system, it would give you the knowledge to figure out how to make a login system on your own.

Share this post


Link to post
Share on other sites

Perhaps, the book is pretty basic, the one I have, ineasysteps is the name, it doesn't really give a solid understanding of php. Cheaper books won't be that in depth.

<?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 )'
}
deleterecord(); // call the function

# Close database connection.
mysqli_close( $dbc ) ;
?> 

if ( $num > 0 )'

 

<?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 )

deleterecord(); // call the function
}
# Close database connection.
mysqli_close( $dbc ) ;
?> 

No problem with this in php checker.

 

<?php
# Access session.
session_start() ;

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

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

deleterecord(); // call the function
}
# Close database connection.
mysqli_close( $dbc ) ;
?>

Edited by Html
Some more tickering

Share this post


Link to post
Share on other sites

Yeah, i guess, and sure I thought about that in the past, but I just felt I had to keep going with this example code to try and get what I wanted working.

The example code isn't what you need, you need to start at the beginning.  You need to understand variables, functions, constants, objects with methods and properties.  You need to understand data types, string syntax, control structures, scope, and how to look up information in the manual.  Any amount of looking at example code or Youtube videos will fail to give you that knowledge.  That's just a fact.

Like I stated, I emailed the company who published the book, and they didn't respond to trying to make sense of my example code.

I would never expect a publishing company to provide technical support for their books.  The people you are emailing are in the book publishing business, they don't have technical support staff on hand for any subject that they might publish a book for.  That's not what they do, they publish books.

Seems obvious to include that with a user site login, I am sure everybody here thinks that is a must.

100% of lessons involving databases will cover delete operations.  They are one of the 4 fundamental data manipulation operations along with select, insert, and update.  Your book covers using delete queries, what it probably doesn't do is have an example that you can immediately apply to your specific situation since you don't understand the basics.  Deleting is every bit as simple as selecting data, it's actually more simple.  With selecting data, you have to tell it which table or tables, which fields, and which records.  With deleting you only tell it which single table and which record or records.  Deleting is easier and more basic than selecting data.  It's also more basic than inserting or updating with all of the possible functions and default values and triggers and other things that you don't worry about with deleting.  So I really doubt that the problem is that your book didn't include a section on using delete queries, I bet the problem is that you didn't understand it because you skipped everything before it, and you couldn't find an example that directly applied to your specific situation.

Perhaps, the book is pretty basic, the one I have, ineasysteps is the name, it doesn't really give a solid understanding of php. Cheaper books won't be that in depth.

You don't think a book like this is going to have anything useful to you?  Really?  There's over 800 pages there, you think you would read through 800+ pages and still have no idea?  Man, I read a book about PHP 15 years ago that only had 500 pages, and it gave me everything I needed.  Surely those 800+ pages have some basic information about PHP, right?  I mean, considering that your existing knowledge of PHP is essentially zero, any information at all is something you don't have, right?

No problem with this in php checker.

Well, the SQL checker in my head shows that you have a syntax error.  That query won't run, because you haven't learned how to write the most basic query in SQL.

You're not even going to execute the function to delete the record at all, because the logic in that codes makes absolutely no sense. 

If your delete query returns at least one row (why would a delete query return rows at all?  That's not what a delete does.  That doesn't make any sense) then you want to run the deleterecord function.  So you ONLY want to run the deleterecord function if your delete query that doesn't return any records actually did return a record, and you don't want to call your function in any other situation?  Why do you even have a function at all, what's the purpose of putting your code in a function here?  And if you're going to define your function to require that 3 parameters are passed to it, then why are you trying to call it with no parameters?  Isn't that a fatal runtime error?  And, speaking of parameters, why are you trying to require 2 parameters in the function that you never even use?

See the problem?  You really think your issue is that you haven't found the right example code?  That's not your problem.

Share this post


Link to post
Share on other sites

Ineasysteps has a website, but have no customer support, but a person can send recommendations, so that was what I did end of last month, I sent the code here, and hopefully the work was mostly done, even though it didn't make much sense.

I tried the book, it is too much in it. I looked back at php 7 book from ineasysteps on functions, I then looked back at the w3schools example, so I'm confused on all of it.

One example states this, and then another.

<?php
# Access session.
session_start() ;

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

{

deleterecord(); // call the function

function deleteRecord( $dbc, $q, $r )

  $q = 'DELETE * FROM users  WHERE id = $user_id';
 $r = mysqli_query( $dbc , $q ) ;

}
# Close database connection.
mysqli_close( $dbc ) ;
?>

Updated this, no rows.

W3 schools first example write message, so I wasn't sure what is where.

Edited by Html

Share this post


Link to post
Share on other sites

I tried the book, it is too much in it.

Well, I don't know what to tell you.  Some books don't have enough information, other books have too much.  I don't think the problem is the books.

Updated this, no rows.

Of course not, now in addition to your SQL syntax errors, you have PHP syntax errors also.

Share this post


Link to post
Share on other sites

Right,

This is a function from the Php & Mysql code example.

<?php

# Create a global variable.
$result = 5 + 5 ;

function square()
{
 $result = 5 * 5 ;
 echo "Local Square Result = $result<br>" ;
}

function cube()
{
 $result = 5 * 5 * 5 ;
 echo "Local Cube Result = $result<br>" ;
}

echo "Global Sum Result = $result<br>" ;
square();
cube();

?>

 

Share this post


Link to post
Share on other sites
 {

deleterecord(); // call the function

function deleteRecord( $dbc, $q, $r )

  $q = "DELETE * FROM users  WHERE id = '$user_id'" ;
 $r = mysqli_query( $dbc , $q ) ;

}

 

Edited by Html

Share this post


Link to post
Share on other sites

Why is there a "{" on the first line of your code?

Share this post


Link to post
Share on other sites

Seriously.  And why is the same character missing after the function definition line?

There is a major lack of attention to detail going on, and that is a really bad sign for anyone who wants to program.

This is the format of a function definition:

function function_name (parameter list)
{
  function body
}

You have this:

{
  function function_name (parameter list)

  function body
}

And you don't seem to notice, or have any idea.  That's worrisome.

Share this post


Link to post
Share on other sites

😑 Yeah sure is.

 <?php
# Access session.
session_start() ;

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

deleterecord(); // call the function

function deleteRecord( $dbc, $q, $r )
{
  $q = 'DELETE * FROM users  WHERE id = $user_id';
 $r = mysqli_query( $dbc , $q ) ;

}
# Close database connection.
mysqli_close( $dbc ) ;
?> 

 

Share this post


Link to post
Share on other sites

OK, a few new questions:

1. Why are you defining the function with required parameters, and then not passing any parameters when you call it?

2. Since I already asked that, why didn't you address it?  Did you not see the question?  Did you ignore it?  Do you not know what I'm talking about at all?

3. Why are those 2 lines of code in a function at all?  What is the purpose of putting those 2 lines of code into a function?

4. Are you ever going to address the SQL syntax error?  I've mentioned it multiple times and, again, it appears that you're just ignoring me when I point it out.

5. Why are you using a variable called $user_id that you haven't defined?

6. Why am I still bothering to point out errors if you just ignore my questions?

 

When I ask a question, I expect an answer, these questions are not rhetorical.  I don't expect you to just post a slightly-altered piece of code with different problems but not answer any of the questions that I'm asking you.

Share this post


Link to post
Share on other sites

I don't know why I am not using user_id, I haven't a clue.

 

 <?php
# Access session.
session_start() ;

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

{
deleterecord(); // call the function
}

function deleteRecord( $dbc, $q, $r )
{
  $q = 'DELETE * FROM users  WHERE id = $user_id';
 $r = mysqli_query( $dbc , $q ) ;

}
# Close database connection.
mysqli_close( $dbc ) ;
?> 

Share this post


Link to post
Share on other sites

Damn, did I call that, or what?

I don't expect you to just post a slightly-altered piece of code with different problems but not answer any of the questions that I'm asking you.

You have decided that the problem with your code, after all of the questions that I asked, is that your code was missing curly brackets around the function call for some reason, even though you've never seen any other code that just has an arbitrary pair of curly brackets that are not associated with any control structure or anything else.  I don't know what to say.  Well done.

The funny thing is that it's not actually a syntax error.  It serves absolutely zero purpose at all, but yeah, if you want to just group arbitrary code in a set of brackets I suppose PHP won't stop you.

Share this post


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

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

{
deleterecord(); // call the function

function deleteRecord( $dbc, $q, $r , $user_id)

  $q = 'DELETE * FROM users  WHERE id' = '$user_id';
 $r = mysqli_query( $dbc , $q, $user_id ) ;
  
}

# Close database connection.
mysqli_close( $dbc ) ;
?> 

Okay, I am probably not going anywhere with this.

Edited by Html

Share this post


Link to post
Share on other sites

Wait, no, you've added $user_id to your function definition (which actually does give the function a purpose), but for some reason you've also decided to pass $user_id to mysqli_query, even though the third parameter to mysqli_query should be the result mode:

https://www.php.net/manual/en/mysqli.query.php

Share this post


Link to post
Share on other sites

Result mode, so now there is more than one mysqli_query to be used,

3 hours ago, Html said:

$r = mysqli_free_result() ( $dbc , $q, $user_id ) ;

This is certainly new to me.🤨 Yeah. Now what.😕

Share this post


Link to post
Share on other sites

I recommend you read every post in this thread carefully and consider reading books, tutorials and other resources more carefully as well. It's as though you ignore anything longer than a sentence. You have to stop writing code and begin learning to read code instead.

Share this post


Link to post
Share on other sites

Result mode, so now there is more than one mysqli_query to be used,

There has always ever only been one mysqli_query function, you've just never understood how to use it.  That's because your knowledge comes from looking at existing code instead of learning the way that everyone else has.

Yeah. Now what.

Now, this is the end of this for me.  There's nothing I can do for you, and I don't think there's anything that anyone else can do for you either.  You need to start at the beginning, and I don't think anyone here wants to teach someone how to program from scratch.  That's what the online resources are for.  You need to either get a new book, re-read the books you have, or enroll in a class to learn what you're missing (assuming that the online written tutorials just don't work for you for whatever reason).  Like I've said repeatedly, tutorial videos will not teach you how to program, and skipping the important parts of books ins't doing it either.  Your methods of learning this are not working, at all, period.

If you want to see what a functional page to delete a user might look like to see how far you are, this is what it looks like.  This does not just plug into your existing files, but if you want to see how far you are from where you should be, here you go:

<?php

// start the session, define $config, etc
require_once 'global.init.php';

if (empty($_SESSION['userid'])) {
  header('Location: login.php?error=' . rawurlencode('You are not logged in'));
  exit();
}

if (empty($_GET['id'])) {
  header('Location: users.php?error=' . rawurlencode('The ID was not found'));
  exit();
}

// there should be additional validation to verify that the logged-in user has authorization to delete the specified user

$pdo = new PDO($config['dsn'], $config['db_user'], $config['db_pass'], [\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION]);

try {
  $stmt = $pdo->prepare('DELETE FROM users WHERE id = ?');
  $stmt->execute([$_GET['id']]);
  
  header('Location: users.php?msg=' . rawurlencode('The user was deleted'));
  exit();
}
catch (PDOException $e) {
  header('Location: users.php?error=' . rawurlencode($e->getMessage()));
  exit();
}

 

Share this post


Link to post
Share on other sites

I'm no where near there then, besides that is PDO or what ever, I am using php 5. Considering php & Mysql was published in 2012.

That code is more useful for if I attempt to learn php 7 from scratch.

Share this post


Link to post
Share on other sites

PDO is from PHP 5.1.0, released in 2005.  You're just learning the older way, from 2004 (5.0.0).  But if 2005 code is too recent and you'd rather learn the 2004 way, whatever.  You should still use prepared statements, though.  Mysqli supports them.

Something to keep in mind when you decide to start over.

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