cant add things to database...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gaogier
Forum Contributor
Posts: 391
Joined: Wed Mar 02, 2005 1:02 pm
Location: Portsmouth, UK
Contact:

cant add things to database...

Post by gaogier »

Code: Select all

//------------------------Begin Monster-------------------------------------------//
function monster(){
$page = $_GET['page'];
    if(empty($page)){
        $page = 1;
    }     
    $limit          = 25;               
    $query_count    = "SELECT id, name, lvl, members FROM monsters ORDER BY name ASC";    
    $result_count   = mysql_query($query_count);    
    $totalrows      = mysql_num_rows($result_count);
    
    $limitvalue = $page * $limit - ($limit);
echo '<p><font class="adminheader">Monster Admin</font></p>';
echo "<TABLE border=\"0\" width=\"89%\" class=monster>\n";
 echo "<TR><TD class=title><center>Monster Name</center></td><td class=title><center>Edit</center></TD</TR>\n";
   /* query for monsters */
        $query = "SELECT id, name, lvl, members FROM monsters ORDER BY name ASC LIMIT $limitvalue, $limit";    
        $result = mysql_query ($query);
        while ($row = mysql_fetch_assoc ($result)) {
            /* display monsters in a table */
            /* place table row data in 
             * easier to use variables.
             */
             $count = $count + 1; 
    
            $name = $row['name'];        
            $level = $row['lvl'];
    
      
            /* display the data */
            echo '<TR bgcolor="'.processRow($count).'"><TD class="calc"><b>'.$name.'</b></td><td class="calc"><a href="'.$_SERVER['PHP_SELF'] . '?flibble=m_update&id='.$row['id'].'"><img src="images/admin/view.gif" alt="Edit" border="0"></a>&nbsp;<a href="'.$_SERVER['PHP_SELF'] . '?flibble=m_delete&id='.$row['id'].'"><img src="images/admin/delete.gif" alt="Delete" border="0"></a></TD></TR>';
            if($count == 2){
                $count = 0;
            }    
        }
    
        /* finish up table*/
        echo "</TABLE><center>\n";
        
        if($page != 1){
        $pageprev = $page-1;        
        echo '<a href="admin.php?flibble=monster&page='.$pageprev.'">Previous</a>&nbsp;&nbsp;';
    }else{
        echo 'Previous&nbsp;&nbsp;';
    }
 
    $numofpages = $totalrows / $limit;
    
    for($i = 1; $i <= $numofpages; $i++){
        if($i == $page){
            echo $i.' ';
        }else{
            echo '&nbsp;<a href="admin.php?flibble=monster&page='.$i.'">'.$i.'</a>&nbsp;';
        }
    }
    if(($totalrows % $limit) != 0){
        if($i == $page){
            echo $i.' ';
        }else{
            echo '&nbsp;<a href="admin.php?flibble=monster&page='.$i.'">'.$i.'</a>&nbsp;';
        }
    }
    if(($totalrows - ($limit * $page)) > 0){
        $pagenext = $page+1;
         
        echo '&nbsp;&nbsp;<a href="admin.php?flibble=monster&page='.$pagenext.'">Next</a>';
    }else{
        echo '&nbsp;&nbsp;Next';
    }
        
        
        echo '<p><b><a href="http://runehints.com/admin.php?flibble=add_monster"><img src="images/plus.gif" alt="Add" border="0"> Add monster</a></b></p>';
 
}
 
function m_update($id) {
   /* query for item */
    $query = "SELECT * FROM monsters WHERE id=$id";
    $result = mysql_query ($query);
    
    /* if we get no results back, error out */
$numrtn = mysql_num_rows($result);
 
    if ($numrtn == 0) {
        echo "The monster requested cannot be found\n";
        return;
    }
    $row = mysql_fetch_assoc($result);
    /* easier to read variables and 
     * striping out tags */
    $id = $row['id'];
    $name = $row['name'];
    $lvl = $row['lvl'];
    $hp = $row['hp'];
    $race = $row['race'];
    $location = $row['location'];
    $members = $row['members'];
    $examine = $row['examine'];
    $contrib = $row['contributedby'];
    $drops = $row['drops'];
    $picture = $row['picture'];
    $sxp = $row['slayerxp'];
 
if ($members = "Y") {
$mem = "Yes";
}else{
$mem = "No";
}
$xphp1 = $hp * 1.3333333333333333333;
$xpo = $hp * 4; 
$xp1 = $hp * 5.3333333333333333333;
 
$xphp = sprintf('%.0f', $xphp1);
$xp = sprintf('%.0f', $xp1);
 
if (isset($_POST['submit'])){  //handle form           
    
    require_once ('../mysql_connect.php');//connect to db
$name = escape_data($_POST['name']);
$picture = escape_data($_POST['picture']);
$level = escape_data($_POST['level']);
$hp = escape_data($_POST['hp']);
$members = escape_data($_POST['members']);
$examine = escape_data($_POST['examine']);
$location = escape_data($_POST['location']);
$loot = escape_data($_POST['drops']);
$contrib = escape_data($_POST['contrib']);
$race = escape_data($_POST['race']);
$sxp = escape_data($_POST['sxp']);
 
    if ($name && $picture && $level && $hp && $members && $examine && $location && $loot && $contrib){//if evrything is ok   
 
        $query = "UPDATE monsters SET name = '$name' , lvl = '$level', hp = '$hp', members = '$members', location = '$location', examine = '$examine', drops = '$loot', contributedby = $contrib', picture = '$picture', slayerxp = '$sxp' WHERE id ='$id'";
        $result = @mysql_query ($query); //Run the query.
 
if ($result){ //if it entered correctly   
            echo '<br /><table width=98% bgcolor=#C6F4DF class=pass align=center><tr>
                            <td width=40><img src=images/tick.gif></td>
                            <td align=left>
                            <B>Success</B> <BR>
                            The '.$name.'\'s data was successfully updated<br />                            
                            </table><br /><br />';
            include ('difffooter.inc');//footer
            exit();
}else{ // didn't work         
                echo '<table width=98% bgcolor=#faf5f4 class=logfail align=center><tr>
                            <td width=40><img src=images/exclamation.gif></td>
                            <td align=left>
                            <B>Update Failed!</B> <BR>
                            The '.$name.'\'s data was could not be updated
                            <BR>Please Try again later<br />
                            </table><br /><br />';
 
}       
 
}else{          
echo '<table width=98% bgcolor=#faf5f4 class=logfail align=center><tr>
                            <td width=40><img src=images/exclamation.gif></td>
                            <td align=left>
                            <B>Update Failed!</B> <BR>
                            Data missing.
                            <BR>Please enter all information needed and try again<br />
                            </table><br /><br />';
}   
}
    /* display the items */    
 
echo '<br /><center><font class="adminheader">Update Monster Information</font></center> <br />';
echo '<form action="admin.php?flibble=m_update&id='.$id.'" method="post">';
?>
<table width="89%" height="334" border="0" class="monster">
  <tr>
    <td><center><input type="text" class="text" name="name" size="15" maxlength="30" value="<?php echo "$name"; ?>" /></center></td>
  </tr>
  <tr>
    <td><table width="101%" height="297" border="1" class="monster">
        <tr>
          <td height="115" width="17%">Picture:</td>
          <td><img src="monster/<?php echo $picture; ?>"><br /><br /><input type="text" class="text" name="picture" size="15" maxlength="30" value="<?php echo "$picture"; ?>" /></td>
          <td width="34%">Level:</td>
          <td><input type="text" class="text" name="level" size="3" maxlength="3" value="<?php echo "$lvl"; ?>" /></td>
        </tr>
        <tr>
          <td height="23" width="17%">Race:</td>
          <td><input type="text" class="text" name="race" size="15" maxlength="30" value="<?php echo "$race"; ?>" /></td>
          <td width="34%">Members?</td>
          <td><input type="text" class="text" name="members" size="1" maxlength="1" value="<?php echo "$members"; ?>" /></td>
        </tr>
        <tr>
          <td height="23" width="17%">Hitpoints:</td>
          <td><input type="text" class="text" name="hp" size="3" maxlength="4" value="<?php echo "$hp"; ?>" /></td>
          <td>Xp gained total:</td>
          <td><?php echo "$xp"; ?></td>
        </tr>
        <tr>
          <td height="23">Xp gained to hits:</td>
          <td colspan="1"><?php echo "$xphp"; ?></td>
          <td>Xp that goes to other combat skill:</td>
          <td colspan="1"><?php echo "$xpo"; ?></td>
        <tr>
          <td height="23" width="17%">Location:</td>
          <td><input type="text" class="text" name="location"  value="<?php echo "$location"; ?>" /></td>
          <td width="34%">Examine:</td>
          <td><input type="text" class="text" name="examine"  value="<?php echo "$examine"; ?>" /></td>
        </tr>
        <tr>
          <td height="74" width="17%">Contributed by:</td>
          <td><input type="text" class="text" name="contrib" value="<?php echo $contrib; ?>" /></td>
          <td width="34%">Loot:</td>
          <td><textarea name="loot" rows="18" cols="50"><?php echo $drops; ?></textarea></td>
        </tr>
        <tr>
          <td>Slayer XP:</td>
          <td><input type="text" class="text" name="sxp" size="3" maxlength="4" value="<?php echo "$sxp"; ?>" /></td>
        </tr>
      </table></td>
  </tr>
</table>
<div align="center"><input type="submit" name="submit" value="Update DB" class="liteoption" />&nbsp;<input type="reset" name="reset" value="reset" class="liteoption" /></div></form>
<?php
 
}
 
function m_delete($id){
 
$query = "SELECT `name` FROM monsters WHERE id=$id";
    $result = mysql_query ($query);
    
    /* if we get no results back, error out */
$numrtn = mysql_num_rows($result);
 
    if ($numrtn == 0) {
        echo "The monster requested cannot be found\n";
        return;
    }
    $row = mysql_fetch_assoc($result);
    /* easier to read variables and 
     * striping out tags */
    $name = $row['name'];
if (isset($_POST['yes'])){  //handle form           
$query = "DELETE FROM `monsters` WHERE `id` = ".$id." LIMIT 1";
$result = mysql_query($query);
 
    if ($result) {
        ob_end_clean();
        header("http://runehints.com/admin.php?flibble=monster");        
    }
}
 
echo '<table width=98% bgcolor=#faf5f4 class=logfail align=center><tr>
                                    <td width=40><img src=images/exclamation.gif></td>
                                    <td align=left>
                                    <B>Delete?</B> <BR>
                                    Are you sure you want to delete '.$name.' from the database?
                                    <BR><br /><form action="admin.php?flibble=m_delete&id='.$id.'" method="post"><input type="submit" name="yes" value="Yes" class="delete" />&nbsp;<input name="no" type=button onClick="javascript&#058;history.go(-1)" value="No" class="delete" /></form>
                                    </table><br /><br />';
 
 
}
 
function add_monster() {
 
if (isset($_POST['submit'])){  //handle form 
    require_once ('../mysql_connect.php');//connect to db
$name = escape_data($_POST['name']);
$picture = escape_data($_POST['picture']);
$level = escape_data($_POST['level']);
$hp = escape_data($_POST['hp']);
$members = escape_data($_POST['members']);
$examine = escape_data($_POST['examine']);
$location = escape_data($_POST['location']);
$loot = escape_data($_POST['loot']);
$contrib = escape_data($_POST['contrib']);
$race = escape_data($_POST['race']);
$slayer = escape_data($_POST['sxp']);
 
    if ($name && $picture && $level && $hp && $members && $examine && $location && $loot && $contrib){//if evrything is ok   
 
                $uploaddir = 'monster/'; 
                
                $uploadfile = $uploaddir . $_FILES['image']['name'];
                
                $upload = move_uploaded_file($_FILES['image']['tmp_name'], $uploadfile);
                
                if( $upload == TRUE ) { 
                
                    echo '<font face=verdana color=green size=1>The picture was uploaded successully</font><br />';
                
 
                            $query = "INSERT INTO monsters (name, lvl, hp, members, location, examine, race, drops, contributedby, picture, slayerxp ) VALUES ('$name', '$level', '$hp', '$members', '$location', '$examine', '$race', '$loot', '$contrib', '$picture', '$slayerxp' )";
                            $result = @mysql_query ($query); //Run the query.
                    
                    if ($result){ //if it entered correctly   
                        echo '<br /><table width=98% bgcolor=#C6F4DF class=pass align=center><tr>
                                        <td width=40><img src=images/tick.gif></td>
                                        <td align=left>
                                        <B>Success</B> <BR>
                                        The '.$name.'\'s data was successfully added<br />                          
                                        </table><br /><br />';
                        include ('difffooter.inc');//footer
                        exit();
                    }else{ // didn't work         
                        echo '<table width=98% bgcolor=#faf5f4 class=logfail align=center><tr>
                                    <td width=40><img src=images/exclamation.gif></td>
                                    <td align=left>
                                    <B>Update Failed!</B> <BR>
                                    The '.$name.'\'s data was could not be added
                                    <BR>Please Try again later<br />
                                    </table><br /><br />';
                                    
                    }       
            } else {
                
                    echo 'Error while uploading file, transfer aborted<br /><pre>';
                    print_r($_FILES);
                    echo '</pre>';
                    exit;
                    
            }  
}
}
 
?>
 
<br /><font class="adminheader"><center>Add monster</center></font><br />
Here you can add monsters to the database. Be sure to give credit properly where it is due!<br />
    <form action="admin.php?flibble=add_monster" method="post" enctype="multipart/form-data"><center>
    <table class=calc>
    <tr><td>
    <table>
 
    <tr><td align=right><font class="text2">Monster Name:</font></td><td align=left><input type="text" class="text" name="name" size="15" maxlength="30" value="<?php if (isset($_POST['name'])) echo $_POST['name']; ?>" /></td></tr>  
    
    <tr><td align=right><font class="text2">Picture:</font></td><td align=left><input name="image" type="file"></td></tr>   
        
    <tr><td align=right><font class="text2">Picture Name:</font></td><td align=left><input type="text" class="text" name="picture" size="15" maxlength="30" value="<?php if (isset($_POST['picture'])) echo $_POST['picture']; ?>" /><span class="small">Simply put the name of the file you uploaded in here. eg. if in the box above you had "C:\images\image.jpg" what would go in here is "image.jpg"</span><br /></td></tr>    
 
    <tr><td align=right><font class="text2">Monster's Level:</font></td><td align=left><input type="text" class="text" name="level" size="3" maxlength="3" value="<?php if (isset($_POST['level'])) echo $_POST['level']; ?>" /></td></tr>  
 
    <tr><td align=right><font class="text2">Monster's HP:</font></td><td align=left><input type="text" class="text" name="hp" size="3" maxlength="4" value="<?php if (isset($_POST['hp'])) echo $_POST['hp']; ?>" /></td></tr>  
    
    <tr><td align=right><font class="text2">Slayer XP:</font></td><td align=left><input type="text" class="text" name="sxp" size="3" maxlength="4" value="<?php if (isset($_POST['sxp'])) echo $_POST['sxp']; ?>" /></td></tr>  
 
    <tr><td align=right><font class="text2">Monster's Race:</font></td><td align=left><input type="text" class="text" name="race" size="15" maxlength="30" value="<?php if (isset($_POST['race'])) echo $_POST['race']; ?>" /></td></tr>        
 
    <tr><td align=right><font class="text2">Members?:</font></td><td align=left><input type="text" class="text" name="members" size="1" maxlength="1" value="<?php if (isset($_POST['members'])) echo $_POST['members']; ?>" /><font class="small2">Use Y or N only</font></td></tr>    
 
    <tr><td align=right><font class="text2">Monster's Examine Description:</font></td><td align=left><input type="text" class="text" name="examine" size="25" maxlength="255" value="<?php if (isset($_POST['examine'])) echo $_POST['examine']; ?>" /><font class="small2">Please be sure to get it exact.</font></td></tr>
 
    <tr><td align=right><font class="text2">Monster's Location:</font></td><td align=left><input type="text" class="text" name="location" size="25" maxlength="255" value="<?php if (isset($_POST['location'])) echo $_POST['location']; ?>" /></td></tr>   
 
    <tr><td align=right><font class="text2">Monster's Loot:</font></td><td align=left><textarea name="loot" rows="18" cols="50" wrap="physical"><?php if (isset($_POST['loot'])) echo $_POST['loot']; ?></textarea></td></tr>   
 
    <tr><td align=right><font class="text2">Contributed by:</font></td><td align=left><input type="text" class="text" name="contrib" size="25" maxlength="100" value="<?php if (isset($_POST['contrib'])) echo $_POST['contrib']; ?>" /><font class="small2">&nbsp;The person who gave you the information not you (unless you are providing your own information)</font></td></tr> 
    </table>
    </td>
    </tr>
    <tr>
    <td>
    <div align="center"><input type="submit" name="submit" value="Add Monster" class="liteoption" />&nbsp;<input type="reset" name="reset" value="reset" class="liteoption" /></div>
    </td>
    </tr>
    </table>
</CENTER>
    </form>
    <?php
    }
 
 
 
//------------------------End Monster-------------------------------------------//
 
//--monster--//
    case 'monster':
        monster();
        break;
    case 'add_monster':
        add_monster();
        break;
    case 'm_update':
        m_update($_GET['id']);
        break;
    case 'm_delete':
        m_delete($_GET['id']);
        break;
the code just shows the page with the form where i would add the data to the mysql database, any ideas?
definewebsites
Forum Newbie
Posts: 19
Joined: Thu May 01, 2008 9:51 pm

Re: cant add things to database...

Post by definewebsites »

Hmm..

Are there certain portions of your webpage that you could display using just static HTML? That would make it a little less complicated and easier to follow.

Cheers
gaogier
Forum Contributor
Posts: 391
Joined: Wed Mar 02, 2005 1:02 pm
Location: Portsmouth, UK
Contact:

Re: cant add things to database...

Post by gaogier »

You can access where you login, just pm me if your intrested in helping out. i will reply with User/pass
gaogier
Forum Contributor
Posts: 391
Joined: Wed Mar 02, 2005 1:02 pm
Location: Portsmouth, UK
Contact:

Re: cant add things to database...

Post by gaogier »

anyone intrested in helping?
definewebsites
Forum Newbie
Posts: 19
Joined: Thu May 01, 2008 9:51 pm

Re: cant add things to database...

Post by definewebsites »

Hey there,

If its not a problem, please give me a file with the source code and I will have a look.

Cheers
Post Reply