SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Aug 2011
    Location
    Manchester, England
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Edit and Delete from databases

    Hi there

    Iím trying to create a system in php where people can input times on F1 2011
    And then compare them - Saves doing it over twitter!

    Working through a book on building a database driven site by Kevin Yank


    Iím working on the admin section where I can add/edit/delete players
    The issue Iím having is I canít seem to edit/delete players
    I can add, no problem
    I've followed the code from the book exactly (with my own content) but it just will not work!

    Iíve attached screen shops of phpmyadmin as well

    Live link is Manage Players

    index.php
    PHP Code:
    <?php
    //ADD - WORKING
    include '../../magic-quotes.php';

    if (isset(
    $_GET['add']))
    {
        
    $pagetitle 'New player';
        
    $action 'addform';
        
    $name ='';
        
    $email ='';
        
    $gamertag ='';
        
    $console_format ='';    
        
    $id ='';
        
    $button ='Add player';    
        
        include 
    'form.php';
        exit();
    }

    if (isset(
    $_GET['addform']))
    {
        include 
    '../../connection.php';
        
        
    $name mysqli_real_escape_string ($link$_POST['name']);
        
    $email mysqli_real_escape_string ($link$_POST['email']);    
        
    $gamertag mysqli_real_escape_string ($link$_POST['gamertag']);
        
    $console_format mysqli_real_escape_string ($link$_POST['console_format']);
        
        
    $sql "INSERT INTO player SET
                name = '
    $name',
                email = '
    $email',
                gamertag = '
    $gamertag',
                console_format = '
    $console_format'";
                
                if (!
    mysqli_query($link$sql))
                {
                    
    $error 'Error adding player';
                    include 
    'output.php';
                    exit();
                }
                
            
    header ('Location: .');
            exit();
    }

    //EDIT

    if (isset($_POST['action']) and $_POST['action'] == 'Edit')
    {
        include 
    '../../connection.php';
        
        
    $id $mysqli_real_escape_string($link$_POST['id']);
        
    $sql "SELECT id, name, email, gamertag, console_format FROM player WHERE id='$id'";
        
    $result mysqli_query($link$sql);
        if (!
    $result)
        {
            
    $error 'Error fetching player details.';
            include 
    'output.php';
            exit();
        }
        
        
    $row mysqli_fetch_array($result);
        
        
    $pagetitle 'Edit player';
        
    $action 'editform';
        
    $name $row['name'];
        
    $email $row['email'];
        
    $gamertag $row['gamertag'];
        
    $console_format $row['console_format'];
        
    $id $row['id'];
        
    $button 'Edit player';    
        
        include 
    'form.php';
        exit();
    }

    if (isset(
    $_GET['editform']))
    {
        include 
    '../../connection.php';
        
        
    $id mysqli_real_escape_string ($link$_POST['id']);
        
    $name mysqli_real_escape_string ($link$_POST['name']);
        
    $email mysqli_real_escape_string ($link$_POST['email']);    
        
    $gamertag mysqli_real_escape_string ($link$_POST['gamertag']);
        
    $console_format mysqli_real_escape_string ($link$_POST['console_format']);
        
        
    $sql "UPDATE player SET
                name = '
    $name',
                email = '
    $email',
                gamertag = '
    $gamertag',
                console_format = '
    $console_format'
                WHERE id='
    $id'";
                
                if (!
    mysqli_query($link$sql))
                {
                    
    $error 'Error editing player';
                    include 
    'output.php';
                    exit();
                }
                
            
    header ('Location: .');
            exit();
    }

    //DELETE

    if (isset($_POST['action']) and $_POST['action'] == 'Delete')
    {
        include 
    '../../connection.php';
        
    $id mysqli_real_escape_string ($link$_POST['id']);
        
        
    //get times belonging to player
        
    $sql "SELECT id FROM times WHERE playerid='$id'";
        
    $result mysqli_query($link$sql);
        if (!
    $result)
        {
            
    $error 'Error getting list of times to delete';
            include 
    'output.php';
            exit();
        }
        
        
    //for each time
        
    while ($row mysqli_fetch_array($result))
        {
            
    $timesId $row[0];
            
            
    //Delete times from tracks
            
    $sql "DELETE FROM tracktimes WHERE timesId='$timesid'";
            if (!
    mysqli_query($link$sql))
            {
                
    $error 'Error deleting track entries for times.';
                include 
    'output.php';
                exit();
            }
        }
        
        
    //Delete times belonging to player
        
    $sql "DELETE FROM times WHERE playerid ='$id'";
        if (!
    mysqli_query($link$sql))
        {
            
    $error 'Error delete times for player';
            include 
    'output.php';
            exit();
        }
        
        
    //delete the player
        
    $sql "DELETE FROM player WHERE id ='$id'";
        if (!
    mysqli_query($link$sql))
        {
            
    $error 'Error deleting player';
            include 
    'output.php';
            exit();
        }
        
        
    header('Location: . ');
        exit();
    }

    //Display players list - WORKING
    include '../../connection.php';

    $result mysqli_query($link'SELECT id, name, email, gamertag, console_format FROM player');

    if(!
    result)
    {
        
    $error 'Error fetching players from database.';
        include 
    'output.php';
        exit();
    }

    while (
    $row mysqli_fetch_array($result))
    {
            
    $players[] = array ('name' => $row['name'], 'email' => $row['email'], 'gamertag' => $row['gamertag'], 'console_format' => $row['console_format']);
    }

    include 
    'players.php';

    ?>
    form.php

    HTML Code:
    <?php include '../../helpers.php'; ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    		"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    	<head>
    		<title><?php htmlout($pagetitle); ?></title>
    		<meta http-equiv="content-type"
    				content="text/html; charset=utf-8"/>
    	</head>
    	<body>
    		<h1><?php htmlout($pagetitle); ?></h1>
    		<form action="?<?php htmlout($action); ?>" method="post">
    			<div>
    				<label for="name">Name: <input type="text" name="name"
    						id="name" value="<?php htmlout($name); ?>"/></label>
    			</div>
    			<div>
    				<label for="email">Email: <input type="text" name="email"
    						id="email" value="<?php htmlout($email); ?>"/></label>
    			</div>
    			<div>
    				<label for="email">Gamertag: <input type="text" name="gamertag"
    						id="gamertag" value="<?php htmlout($gamertag); ?>"/></label>
    			</div>  
    			<div>
    				<label for="email">console_format: <input type="text" name="console_format"
    						id="console_format" value="<?php htmlout($console_format); ?>"/></label>
    			</div>                       
    			<div>
    				<input type="hidden" name="id" value="<?php htmlout($id); ?>"/>
    				<input type="submit" value="<?php htmlout($button); ?>"/>
    			</div>
    		</form>
    	</body>
    </html>
    players.php
    HTML Code:
    <?php include '../../helpers.php'; ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    		"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    	<head>
    		<title>Manage Players</title>
    		<meta http-equiv="content-type"
    				content="text/html; charset=utf-8"/>
        <link href="../../css/reset.css" rel="stylesheet" type="text/css" />
    	</head>
    	<body>
    		<h1>Manage Players</h1>
    		<p><a href="?add">Add new player</a></p>
            <div class="header">Name</div><div class="header">Gamertag</div><div class="header">e-mail</div><div class="header">Console Format</div><div class="header">Action</div>
            <br />
    		<br />
    		<ul>
    			<?php foreach ($players as $player): ?>
    				<li>
    					<form action="" method="post">
    						<div class="content">
    							<?php echo htmlspecialchars ($player['name'], ENT_QUOTES, 'UTF-8'); ?>
                            </div>
                            <div class="content">
                                <?php echo htmlspecialchars ($player['gamertag'], ENT_QUOTES, 'UTF-8'); ?>
                            </div>
                            <div class="content">
                                <?php echo htmlspecialchars ($player['email'], ENT_QUOTES, 'UTF-8'); ?>
                            </div>  
                            <div class="content">
                                <?php echo htmlspecialchars ($player['console_format'], ENT_QUOTES, 'UTF-8'); ?>
                            </div>                                              
                            <div class="content">
    							<input type="hidden" name="id" value="<?php	echo htmlspecialchars ($player['id'], ENT_QUOTES, 'UTF-8'); ?>"/>
    							<input type="submit" name="action" value="Edit"/>
    							<input type="submit" name="action" value="Delete"/>
    						</div>
                            <br />
                            <br />
    					</form>
    				</li>
    			<?php endforeach; ?>
    		</ul>        		
    	</body>
    </html>
    Please help!

    Thanks for reading
    Any questions give me a shout
    Attached Images Attached Images
    Last edited by TheAnishMistry; Aug 12, 2011 at 07:18. Reason: forgot to add code!

  2. #2
    #titanic {float:none} silver trophy
    molona's Avatar
    Join Date
    Feb 2005
    Location
    from Madrid to Heaven
    Posts
    8,221
    Mentioned
    237 Post(s)
    Tagged
    1 Thread(s)
    You execute an SQL INPUT statement to add a new record into the database. For deleting a record, you need a SQL instruction... DELETE

    Code:
    DELETE from TABLE_NAME where conditions_here

  3. #3
    SitePoint Member
    Join Date
    Aug 2011
    Location
    Manchester, England
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But I've done that in the index.php page though!
    It's about a third of the way down

    I think it's to do with the button...

  4. #4
    #titanic {float:none} silver trophy
    molona's Avatar
    Join Date
    Feb 2005
    Location
    from Madrid to Heaven
    Posts
    8,221
    Mentioned
    237 Post(s)
    Tagged
    1 Thread(s)
    What error message do you get?

  5. #5
    SitePoint Member
    Join Date
    Aug 2011
    Location
    Manchester, England
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't get one.
    When I click edit or delete
    Nothing happens

    Delete acts like a refresh, and edit just goes to a white screen

    Have a gander here - Manage Players

  6. #6
    #titanic {float:none} silver trophy
    molona's Avatar
    Join Date
    Feb 2005
    Location
    from Madrid to Heaven
    Posts
    8,221
    Mentioned
    237 Post(s)
    Tagged
    1 Thread(s)
    If you can't se the error it may be because you've configured php to not display them but. The error will be listed in the web server log nevertheless. If you use Apache, you shuold have a logs folder where your error.log file can be found

  7. #7
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    If I click on Edit I get a HTTP 500 error.
    Also, if I click on Delete a couple of times, I get a HTTP 403 (Forbidden) error.

    I think there might be something wrong with your server config?

  8. #8
    SitePoint Member
    Join Date
    Aug 2011
    Location
    Manchester, England
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I believe it has flood control on it?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •