Jump to content

Old bad code to Good new code. HELP!!.


gaogier

Recommended Posts

Hello

 

I am very new to php, in fact I have just really started learning 5 days ago. I have a book, (php3-4) that has got me through the years and if you go through the old posts of mine, you see how bad I really am with php.

 

If you don't mind me having your MSN address so I can randomly ask questions about php, Please add me, gaogier@runehints.com

 

So, this is what the code MUST do, add data to each calculator.  The new code is for another section of our CMS.

 

Here is the old code. From our old CMS.

 

//------------------------Begin Calc-------------------------------------------//
function calc(){

echo '<p><font class="adminheader">Caclulator Admin</font></p>';
echo "<TABLE border=\"0\" width=\"89%\" class=monster>\n";
echo "<TR><TD class=title><center>Calculator Name</center></td><td class=title><center>Insert - Edit - Delete</center></TD></TR>\n";
   /* query for monsters */
        $query = "SELECT id, name, members, tablename FROM calc ORDER BY name ASC";    
    $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'];   
		$mem = $row['members'];
		if ($mem == "Y"){
		$ignore = 1;
		}else{
		$ignore = 0;
		}
		$tablename = $row['tablename'];     	
    
  
		/* display the data */
		echo '<TR bgcolor="'.processRow($count).'"><TD class="calc"><b>'.$name.'</b></td><td class="calc"><a href="'.$_SERVER['PHP_SELF'] . '?flibble=calcitem&calc='.$tablename.'&ignore='.$ignore.'"><img src="images/admin/insert.png" alt="Insert data items" border="0"></a> <a href="'.$_SERVER['PHP_SELF'] . '?flibble=c_update&id='.$row['id'].'&ignore='.$ignore.'"><img src="images/admin/view.gif" alt="Edit" border="0"></a> <a href="'.$_SERVER['PHP_SELF'] . '?flibble=c_delete&id='.$row['id'].'&ignore='.$ignore.'"><img src="images/admin/delete.gif" alt="Delete" border="0"></a></TD></TR>';
		if($count == 2){
			$count = 0; 
		}    
	}
    
        /* finish up table*/
        echo "</TABLE>\n";
        echo '<p><b><a href="http://runehints.com/admin2.php?flibble=add_c"><img src="images/plus.gif" alt="Add" border="0"> Add calculator</a></b></p>';

}

function calcitem($calc){
$calc1 = str_replace("calc", "", $calc);
echo '<p><font class="adminheader">Caclulator Item Admin for '.$calc1.'</font></p>';
echo "<TABLE border=\"0\" width=\"89%\" class=monster>\n";
echo "<TR><TD class=title><center>Item Name</center></td><td class=title><center>Level - XP</center></TD><td class=title><center>Edit</center></TD</TR>\n";
   /* query for monsters */
        $query = "SELECT id, item, members, level, xp FROM ".$calc." ORDER BY level, item ASC";    
    if(!$result = mysql_query ($query)) die(mysql_error());
    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['item'];   
		$mem = $row['members'];
		if ($mem == "1"){
		$ignore = 1;
		}else{
		$ignore = 0;
		}
		$tablename = $row['tablename'];     
		$level = $row['level'];
		$xp = $row['xp'];	
    
  
		/* display the data */
		echo '<TR bgcolor="'.processRow($count).'"><TD class="calc"><b>'.$name.'</b></td><TD class="calc"><b>'.$level.' - '.$xp.'</b></td><td class="calc"><a href="'.$_SERVER['PHP_SELF'] . '?flibble=edit_citem&calc='.$calc.'&id='.$row['id'].'&ignore='.$ignore.'"><img src="images/admin/view.gif" alt="Edit" border="0"></a> <a href="'.$_SERVER['PHP_SELF'] . '?flibble=c_delete&id='.$row['id'].'"><img src="images/admin/delete.gif" alt="Delete" border="0"></a></TD></TR>';
		if($count == 2){
			$count = 0; 
		}    
	}
    $ignores = $_GET['ignore'];
        /* finish up table*/
        echo "</TABLE>\n";
        echo '<p><b><a href="http://runehints.com/admin2.php?flibble=add_citem&calc='.$calc.'&ignore='.$ignores.'"><img src="images/plus.gif" alt="Add" border="0"> Add a calculator item</a></b></p>';

}

function c_update($id) {
   /* query for item */
    $query = "SELECT * FROM calc 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 Skill guide 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'];
    $members = $row['members'];
    $guideby = $row['calcby'];


if (isset($_POST['submit'])){  //handle form           

require_once ('../mysql_connect.php');//connect to db
$name = escape_data($_POST['name']);
$members = escape_data($_POST['members']);
$calcby = escape_data($_POST['guideby']);


if ($name && $members && $calcby){//if evrything is ok   

	$query = "UPDATE calc SET name = '$name' , members = '$members', calcby = '$calcby' WHERE id ='$id'";
	$result = @mysql_query ($query); //Run the query.

if ($result){ //if it entered correctly   
		echo '<br /><table width=98% bgcolor=#565866 class=pass align=center><tr>
						<td width=40><img src=images/tick.gif></td>
						<td align=left>
						<B>Success</B> <BR>
						The '.$name.' calculator was successfully updated<br />							
						</table><br /><br />';
		include ('difffooter.inc');//footer
		exit();
}else{ // didn't work         
			echo '<table width=98% bgcolor=#565866 class=logfail align=center><tr>
						<td width=40><img src=images/exclamation.gif></td>
						<td align=left>
						<B>Update Failed!</B> <BR>
						The '.$name.' calculator could not be updated
						<BR>Please Try again later<br />
						</table><br /><br />';

}       

}else{          
echo '<table width=98% bgcolor=#565866 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 Calculator Information</font></center> <br />';
echo '<form action="admin2.php?flibble=c_update&id='.$id.'" method="post">';
echo '
<center>
<table class=calc>
<tr><td>
<table>

<tr><td align=right><font class="text2">Calculator:</font></td><td align=left><input type="text" class="text" name="name" size="15" maxlength="30" value="'.$name.'" /></td></tr>	

<tr><td align=right><font class="text2">By:</font></td><td align=left><input type="text" class="text" name="guideby" value="'.$guideby.'" /></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="'.$members.'" /><font class="small2">Use Y or N only</font></td></tr>	
</table>
</td>
</tr>
</table>
</CENTER>
</form>
';
?>
<div align="center"><input type="submit" name="submit" value="Update DB" class="liteoption" /> <input type="reset" name="reset" value="reset" class="liteoption" /></div></form>
<?php

}

function c_delete($id){

$query = "SELECT `name` FROM calc 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 calculator 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 `calc` WHERE `id` = ".$id." LIMIT 1";
$result = mysql_query($query);

    if ($result) {
        ob_end_clean();
	header("http://runehints.com/admin2.php?flibble=calc");        
    }
}

echo '<table width=98% bgcolor=#565866 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="admin2.php?flibble=c_delete&id='.$id.'" method="post"><input type="submit" name="yes" value="Yes" class="delete" /> <input name="no" type=button onClick="javascript:history.go(-1)" value="No" class="delete" /></form>
								</table><br /><br />';


}

function add_calc() {

if (isset($_POST['submit'])){  //handle form 
require_once ('../mysql_connect.php');//connect to db
$name = escape_data($_POST['name']);
$calcby = escape_data($_POST['calcby']);
$members = escape_data($_POST['members']);


if ($name && $calcby && $members){//if evrything is ok   


						$query = "INSERT INTO calc (name, calcby, members) VALUES ('$name', '$calcby','$members')";
						$result = @mysql_query ($query); //Run the query.

				if ($result){ //if it entered correctly   
					echo '<br /><table width=98% bgcolor=#565866 class=pass align=center><tr>
									<td width=40><img src=images/tick.gif></td>
									<td align=left>
									<B>Success</B> <BR>
									The  '.$name.'\' calculator was successfully added<br />							
									</table><br /><br />';
					include ('difffooter.inc');//footer
					exit();
				}else{ // didn't work         
					echo '<table width=98% bgcolor=#565866 class=logfail align=center><tr>
								<td width=40><img src=images/exclamation.gif></td>
								<td align=left>
								<B>Update Failed!</B> <BR>
								The '.$name.'\ calculator was could not be added
								<BR>Please Try again later<br />
								</table><br /><br />';

				}       

}
}

?>

<br /><font class="adminheader"><center>Add Calculator</center></font><br />
Here you can add skill guide to the database. Be sure to give credit properly where it is due!<br />

<form action="admin2.php?flibble=add_c" method="post"><center>
<table class=calc>
<tr><td>
<table>

<tr><td align=right><font class="text2">Calculator 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">Calculator by:</font></td><td align=left><input type="text" class="text" name="calcby" value="<?php if (isset($_POST['calcby'])) echo $_POST['calcby']; ?>" /></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>	
</table>
</td>
</tr>
<tr>
<td>
<div align="center"><input type="submit" name="submit" value="Add Calculator" class="liteoption" /> <input type="reset" name="reset" value="reset" class="liteoption" /></div>
</td>
</tr>
</table>
</CENTER>
</form>
<?php
}


function addcalcitem($calc){

$ignore = $_GET['ignore'];

if (isset($_POST['submit'])){  //handle form 
require_once ('../mysql_connect.php');//connect to db
$name = escape_data($_POST['name']);
$level = escape_data($_POST['level']);
if ($ignore != 1){
$members = strtoupper(escape_data($_POST['members']));
if ($members == "Y"){
$members =1;
}else{
$members =0;
}
}
$xp = escape_data($_POST['xp']);

if ($name && $level && $xp){//if evrything is ok   


						$query = "INSERT INTO ".$calc." (item, level, xp, members) VALUES ('$name', '$level', '$xp', '$members')";
						$result = mysql_query ($query); //Run the query.

				if ($result){ //if it entered correctly   
					echo '<br /><table width=98% bgcolor=#565866 class=pass align=center><tr>
									<td width=40><img src=images/tick.gif></td>
									<td align=left>
									<B>Success</B> <BR>
									The item '.$name.' was successfully added<br />							
									</table><br /><br />';
					include ('difffooter.inc');//footer
					exit();
				}else{ // didn't work         
					echo '<table width=98% bgcolor=#565866 class=logfail align=center><tr>
								<td width=40><img src=images/exclamation.gif></td>
								<td align=left>
								<B>Update Failed!</B> <BR>
								The item '.$name.' could not be added
								<BR>Please Try again later<br />
								</table><br /><br />';

				}       

}
}

?>

<br /><font class="adminheader"><center>Add Calculator item</center></font><br />
Here you can add items to calculators. Be sure to give credit properly where it is due!<br />

<?php
echo '<form action="'.$_SERVER['PHP_SELF'].'?flibble=add_citem&ignore='.$ignore.'&calc='.$calc.'" method="post"><center>';
?>
<table class=calc>
<tr><td>
<table>

<tr><td align=right><font class="text2">Item 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">Level:</font></td><td align=left><input type="text" class="text" name="level" value="<?php if (isset($_POST['level'])) echo $_POST['level']; ?>" /></td></tr>	

<tr><td align=right><font class="text2">XP(to decimal if possible):</font></td><td align=left><input type="text" class="text" name="xp" value="<?php if (isset($_POST['xp'])) echo $_POST['xp']; ?>" /></td></tr>	
<?php
if ($ignore !=1){
echo '<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="'.$_POST['members'].'" /><font class="small2">Use Y or N only</font></td></tr>	';
}
?>
</table>
</td>
</tr>
<tr>
<td>
<div align="center"><input type="submit" name="submit" value="Add Calculator item" class="liteoption" /> <input type="reset" name="reset" value="reset" class="liteoption" /></div>
</td>
</tr>
</table>
</CENTER>
</form>
<?php
}

function editcalcitem($calc){
$ignore = $_GET['ignore'];
$id = $_GET['id'];
   /* query for item */
    $query = "SELECT * FROM ".$calc." 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 calculator item requested cannot be found\n";
        return;
    }
    $row = mysql_fetch_assoc($result);
    /* easier to read variables and 
     * striping out tags */
    $name = $row['item'];
    $members = $row['members'];
    if ($members == "1"){
$members ="Y";
}else{
$members ="N";
}
    $xp = $row['xp'];
    $level = $row['level'];


$ignore = $_GET['ignore'];

if (isset($_POST['submit'])){  //handle form 
require_once ('../mysql_connect.php');//connect to db
$name = escape_data($_POST['name']);
$level = escape_data($_POST['level']);
if ($ignore != 1){
$members = strtoupper(escape_data($_POST['members']));
if ($members == "Y"){
$members =1;
}else{
$members =0;
}
}

$xp = escape_data($_POST['xp']);


if ($name && $level && $xp){//if evrything is ok   


						$query = "UPDATE ".$calc." SET item = '$name', level = '$level', xp = '$xp' , members='$members' WHERE id='$id'";
						$result = @mysql_query ($query); //Run the query.

				if ($result){ //if it entered correctly   
					echo '<br /><table width=98% bgcolor=#565866 class=pass align=center><tr>
									<td width=40><img src=images/tick.gif></td>
									<td align=left>
									<B>Success</B> <BR>
									The item '.$name.'\' was successfully added<br />							
									</table><br /><br />';
					include ('difffooter.inc');//footer
					exit();
				}else{ // didn't work         
					echo '<table width=98% bgcolor=#565866 class=logfail align=center><tr>
								<td width=40><img src=images/exclamation.gif></td>
								<td align=left>
								<B>Update Failed!</B> <BR>
								The item '.$name.'\ could not be added
								<BR>Please Try again later<br />
								</table><br /><br />';

				}       

}
}

?>

<br /><font class="adminheader"><center>Edit Calculator item</center></font><br />
Here you can add items to calculators. Be sure to give credit properly where it is due!<br />

<form action="admin2.php?flibble=add_citem" method="post"><center>
<table class=calc>
<tr><td>
<table>

<tr><td align=right><font class="text2">Item name:</font></td><td align=left><input type="text" class="text" name="name" size="15" maxlength="30" value="<?php echo $name; ?>" /></td></tr>	

<tr><td align=right><font class="text2">Level:</font></td><td align=left><input type="text" class="text" name="level" value="<?php echo $level; ?>" /></td></tr>	

<tr><td align=right><font class="text2">XP(to decimal if possible):</font></td><td align=left><input type="text" class="text" name="xp" value="<?php echo $xp; ?>" /></td></tr>	

<?php
if ($ignore !=1){
echo '<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="'.$members.'" /><font class="small2">Use Y or N only</font></td></tr>	';
}
?>
</table>
</td>
</tr>
<tr>
<td>
<div align="center"><input type="submit" name="submit" value="Add Calculator item" class="liteoption" /> <input type="reset" name="reset" value="reset" class="liteoption" /></div>
</td>
</tr>
</table>
</CENTER>
</form>
<?php
}


//------------------------End Calc-------------------------------------------//

 

New code - Add section.

 

    /* RuneHints CMS */
    /* Achievements Add Submit */
    /* Achievements Add Form */ 
    if (($admin == 'achievements') && ($_POST['add'])) {
        $added = time();
        $sql = "INSERT INTO `achi` (id, name, added, guideby, contributors, members, mainbody, updated, sorter) VALUES
        ('',
        '{$_POST['name']}',
        '$added',
        '{$_POST['guideby']}',
        '{$_POST['contributors']}',
        '{$_POST['members']}',
        '{$_POST['mainbody']}',
        '',
        '{$_POST['sorter']}'
        )";
            $result = $db->query($sql);
        echo '<div class="msgOk"><span>Success</span>Record was added successfully.</div>';
    }
    if ($admin == 'achievements') {
            echo '<p>
                    <form action="" method="post">
                            <table>
                                <tr>
                                    <td>Achievement Name:</td>
                                    <td><input type="text" name="name"/></td>
                                </tr>
                                <tr>
                                    <td>Guide By:</td>
                                    <td><input type="text" name="guideby"/></td>
                                </tr>
                                <tr>
                                    <td>Contributors:</td>
                                    <td><input type="text" name="contributors"/></td>
                                </tr>
                                <tr>
                                    <td>Members:</td>
                                    <td><input type="text" name="members"/></td>
                                </tr>
                                <tr>
                                    <td style="vertical-align:top;">Main Body:</td>
                                    <td>
                                    <textarea id="elm1" name="mainbody" rows="30" cols="80" width="60%">
                                                
                                    </textarea>
                                    </td>
                                </tr>
                                <tr>
                                    <td>Sorter:</td>
                                    <td><input type="text" name="sorter"/></td>
                                </tr>
                                <tr>
                                    <td></td>
                                    <td><input type="submit" name="add" value="Add"/></td>
                                </tr>
                            </table>
                    </form>
          </p>';
    }

 

New code edit section.

 

    /* RuneHints CMS */
    /* Achievement Edit Submit */
    /* Achievement Edit Form */
    /* Achievement Add Submit */
    /* Achievement Add Form */
    /* Achievement Echo Records */   
    if (($admin == 'achievements') && ($_POST['edit'])) {
        $updated = time();
            $sql = "UPDATE `achi` SET
            `name` = '{$_POST['name']}',
            `guideby` = '{$_POST['guideby']}',
            `contributors` = '{$_POST['contributors']}',
            `members` = '{$_POST['members']}',
            `mainbody` = '{$_POST['mainbody']}',
            `updated` = '$updated',
            `sorter` = '{$_POST['sorter']}'
            WHERE `id` = '{$id}'";
            $result = $db->query($sql);
            
            echo '<div class="msgOk"><span>Updated</span>Record was updated successfully.</div>';
    }
     
    if (($admin == 'achievements') && (!empty($id))) {
            $sql = "SELECT * FROM achi WHERE `id` = '{$id}'";
            $result = $db->query($sql);
            while ($row = $db->fetch($result)) {
            echo '<p>
                    <form action="" method="post">
                            <table>
                                <tr>
                                    <td>Achievement Name:</td>
                                    <td><input type="text" name="name" value="'.$row['name'].'"/></td>
                                </tr>
                                <tr>
                                    <td>Guide By:</td>
                                    <td><input type="text" name="guideby" value="'.$row['guideby'].'"/></td>
                                </tr>
                                <tr>
                                    <td>Contributors:</td>
                                    <td><input type="text" name="contributors" value="'.$row['contributors'].'"/></td>
                                </tr>
                                <tr>
                                    <td>Members:</td>
                                    <td><input type="text" name="members" value="'.$row['members'].'"/></td>
                                </tr>
                                <tr>
                                    <td style="vertical-align:top;">Main Body:</td>
                                    <td>
                                    <textarea name="mainbody" rows="30">
                                                '.$row['mainbody'].'
                                    </textarea>
                                    </td>
                                </tr>
                                <tr>
                                    <td>Sorter:</td>
                                    <td><input type="text" name="sorter" value="'.$row['sorter'].'"/></td>
                                </tr>
                                <tr>
                                    <td></td>
                                    <td><input type="submit" name="edit" value="Submit"/></td>
                                </tr>
                            </table>
                    </form>
          </p>';
          }
    } elseif (($admin == 'achievements') && (empty($id))) {
            $sql = "SELECT * FROM achi";
            $result = $db->query($sql);
            echo '<table class="guide">
                        <tr class="trtitle">
                            <th>Achievement Name</td>
                            <th>Delete</td>
                        </tr>';
            while ($row = $db->fetch($result)) {
                    echo '<tr style="border-bottom: 1px solid #a07c3c;">
                            <td><a href="edit/'.$row[id].'">'.$row[name].'</a></td>
                            <td><a href="delete/'.$row[id].'"><img src="/images/error.png" border="0" alt="Delete"></a></td>
                        </tr>';
            }
            echo '</table>';
    }

 

Our delete code.

<?php

if(isset($_GET['id']) && !isset($_POST['yes']) && !isset($_POST['no'])) {
    $id = (int) $_GET['id'];

    echo "Are you sure you want to delete this record.";
    echo "<form method='post' action=''>";
    echo "<input type='hidden' name='id' value='{$_POST['id']}'>";
    echo "<input type='submit' value='Yes' name='yes'>";
    echo "<input type='submit' value='No' name='no'>";
    echo "</form>";

}


if (isset($_POST['no'])) {
    echo "<meta http-equiv='refresh' content='0;url=/admincp/' />";
}

if (isset($_POST['yes'])) {
    if ($admin == 'skills') { $sql = "DELETE FROM `skill` WHERE `id` = '$_GET[id]'"; }
    if ($admin == 'quests') { $sql = "DELETE FROM `quest` WHERE `id` = '$_GET[id]'"; }
    if ($admin == 'achievements') { $sql = "DELETE FROM `achi` WHERE `id` = '$_GET[id]'"; }
    if ($admin == 'special') { $sql = "DELETE FROM `sp` WHERE `id` = '$_GET[id]'"; }
    if ($admin == 'location') { $sql = "DELETE FROM `location` WHERE `id` = '$_GET[id]'"; }
    if ($admin == 'maps') { $sql = "DELETE FROM `map` WHERE `id` = '$_GET[id]'"; }
    if ($admin == 'guilds') { $sql = "DELETE FROM `guild` WHERE `id` = '$_GET[id]'"; }
    if ($admin == 'randoms') { $sql = "DELETE FROM `random` WHERE `id` = '$_GET[id]'"; }
    $result = $db->query($sql);
    echo '<div class="msgOk"><span>Success</span>Record was deleted successfully.</div>';
}

?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.