Click here to Skip to main content
15,506,981 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Im following Kristian Guevara great PHP Tutorial (Step-by-Step PHP Tutorials for Beginners - Creating your PHP Program FROM SCRATCH: Basic Authentication, Membership and CRUD Functionalities[^])

I have managed to solve several issues on the code related to deprecated functions and all that by myself (ie mysql vs mysqli). I have also been able to add includes and functions.
Yet im stucked at edit.php on this line:
if(!empty($def['id']))

Im not being able to get pass through that "if" and im not sure what $def means asi it has not been declared previously and it doesnt seem to be taking the value from the url as that should and is done later with $_GET

Can someone enlighten me?

PHP
<pre><html>
    <head>
        <title>My first PHP Website</title>
    </head>
    <?php
	include('functions.php');
    session_start();       //starts the session
    if($_SESSION['user']){ // checks if the user is logged in  
    }
    else{
        header('location: index.php'); // redirects if user is not logged in
    }
    $user = $_SESSION['user']; //assigns user value
    ?>
    <body>
        <h2>Home Page</h2>
        <p>Hello <?php Print "$user"?>!</p> 
        <a href="logout.php">Click here to go logout</a><br/><br/>
        <a href="home.php">Return to home page</a>
        <h2 align="center">Currently Selected</h2>
        <table border="1px" width="100%">
	    	<tr>
		    	<th>Id</th>
			    <th>Details</th>
			    <th>Post Time</th>
			    <th>Edit Time</th>
			    <th>Public Post</th>
		    </tr>
            <?php
				$id_exists = false;
				
                if(!empty($def['id']))
				{
					$id_exists = true;
					$con = getDBConnection();
                    $id = $_GET['id'];
                    $_SESSION['id'] = $id;
					$query = mysqli_query($con,"Select * from list");
                    $count = mysqli_num_rows($query);
					Print '<h2 align="center">There are ' . $count . 'rows';
					if($count > 0)
					{
                       while($row = mysqli_fetch_array($query))
						{
							
							Print "<tr>";
			                Print '<td align="center">' . $row['id'] . "</td>";
                            Print '<td align="center">' . $row['details'] . "</td>";
			                Print '<td align="center">' . $row['date_posted'] . " - " . $row['time_posted']."</td>";
			                Print '<td align="center">' . $row['date_edited'] . " - " . $row['time_edited']."</td>";
			                Print '<td align="center">' . $row['public'] . "</td>";
		                    Print "</tr>";
							
						}
					}
					else
					{
						$id_exists = false;
					}
				}
            ?>
        </table>      
        <br/>
        <?php
        if($id_exists)
        {
        Print '
        <form action="edit.php" method="post">
            Enter new detail: <input type="text" name="details"/><br/> 
            public post? <input type="checkbox name="public[]" value="yes"/><br/> 
            <input type="submit" value="Update List"/> 
        </form> 
        '; 
        }
        else
        {
            Print '<h2 align="center">There is not data to be edited.</h2>';
        } 
        ?>
   </body>
 </html>



Thank you.
Cristian

What I have tried:

If I remove
if(!empty($def['id']))
this code, it seems to work perfectly fine.
But im not one of those who cuts code without understanding its purpose
Posted
Updated 4-Mar-21 10:46am
Comments
Richard MacCutchan 3-Mar-21 12:06pm    
I assume that $def is an array that is being tested to see if it contains an item named 'id'. However, you would get the actual answer by asking the writer of the tutorial.
W Balboos, GHB 3-Mar-21 13:12pm    
When you say it 'Im not being able to get pass through that "if" ', do you get an error message? I'd imagine it could say something like 'undefined index' or undefined for $def, itself? You've got to describe the error.

You could try print_r($def) and, if it's defined, it'll show you all of the members of $def array. If it's not defined then it will give an error of its own.
Cristian Angel Bruner 3-Mar-21 13:37pm    
No, i mean that it never gets TRUE and the code is the one above, so there is no other place where it can be defined. So maybe im right when im saying that there is an error there and that $def is null, therefore its always false condition
W Balboos, GHB 3-Mar-21 14:29pm    
Well - it certainly wouldn't be the first posted code to have an error. Or maybe you're missing something?

OK - I don't know what the page is all about but suppose that $def['id'] is supposed to be $_SESSION['id']? Where would it get any $def values from, anyway?
Cristian Angel Bruner 3-Mar-21 15:16pm    
I totally agree. It just seems an error on the code. Im trying to contact the author but as mentioned... i dont like modifying things without understanding. Just wanted to know if I was missing anything
Thanks!

1 solution

You need to download the author's code; there are some discrepancies in the article.

The line in question is actually:
if(!empty($_GET['id']))

GitHub - kristianguevara/MyFirstWebsite: The finished product for the PHP tutorials for Beginners[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900