Jump to content

Recommended Posts

OK, I've been messing with PHP for about three weeks now (ever since I joined PHPFreaks). I wrote a little script here, which kinda turned in to a big mess. I mean, it works fine, but I know it's resourceful, and there's probably a few things you experts could share that would make it more efficient. I'm just looking for better ways to get things done in here. Any ideas?

 

Also, when someone fills out the data and presses the submit button it completes the SQL insert, but the new data doesn't show up in the SELECT query until the user presses the REFRESH button. Is there any way to REFRESH the information shown without the user having to press refresh. Here's the link to the page so you can see what I mean:

 

http://www.disctribution.com/forums/insert.php

 

Please feel free to submit new information, I can easily delete anything you guys submit. Thanks in advance for any help.

 

<?
<?php
$host = blocked;
$user = 'blocked';
$password = 'blocked';
$dbName = 'blocked';
$dbTable = 'computeraudit';
$conn = mysql_connect($host, $user, $password) or die(mysql_error());
$db = mysql_select_db($dbName, $conn) or die(mysql_error());

$select_result = mysql_query('SELECT * FROM computeraudit ORDER BY isite', $conn);
if (($_POST['sortby'])=="Service Tag"):
$select_result = mysql_query('SELECT * FROM computeraudit ORDER BY iservice', $conn);
endif;
if (($_POST['sortby'])=="Computer"):
$select_result = mysql_query('SELECT * FROM computeraudit ORDER BY icomputer', $conn);
endif;
if (($_POST['sortby'])=="User"):
$select_result = mysql_query('SELECT * FROM computeraudit ORDER BY iuser', $conn);
endif;
if (($_POST['sortby'])=="Model"):
$select_result = mysql_query('SELECT * FROM computeraudit ORDER BY imodel', $conn);
endif;
if (($_POST['sortby'])=="Condition"):
$select_result = mysql_query('SELECT * FROM computeraudit ORDER BY icondition', $conn);
endif;
if (($_POST['sortby'])=="Status"):
$select_result = mysql_query('SELECT * FROM computeraudit ORDER BY istatus', $conn);
endif;
if (($_POST['sortby'])=="Newest"):
$select_result = mysql_query('SELECT * FROM computeraudit ORDER BY idate DESC', $conn);
endif;
if (($_POST['sortby'])=="Oldest"):
$select_result = mysql_query('SELECT * FROM computeraudit ORDER BY idate', $conn);
endif;
// The rest of this loops through outputting the information
echo date("l jS F Y, g:i A");
echo "<body bgcolor=64748B>";
$content = "";
$content .= "<br><b><br> Make a new entry: <br>";
$content .= "<form action=\"{$_SERVER['PHP_SELF']}\" method = \"post\">";
$content .= "<input type=\"text\" name=\"service\" maxlength=\"25\" size=\"20\" STYLE=background:#FFCC00;> Service Tag <font color=AA0011>(required)</font><br>";
//$content .= "<input type=\"text\" name=\"site\" maxlength=\"25\" size=\"20\" STYLE=background:#E6E6FA;> Site<br>";
$content .= "<SELECT name=\"site\" size=\"1\" STYLE=background:#E6E6FA;><OPTION SELECTED> <OPTION>Bakersfield<OPTION>Big Pine<OPTION>Big Sandy<OPTION>Bishop<OPTION>Cold Springs<OPTION>Fresno<OPTION>Handford<OPTION>HR<OPTION>Lake Isabella<OPTION>Lone Pine<OPTION>Porterville<OPTION>Ridgecrest<OPTION>Santa Rosa<OPTION>Tule River<OPTION>Visalia</SELECT> Site<br>";

$content .= "<input type=\"text\" name=\"computer\" maxlength=\"25\" size=\"20\" STYLE=background:#E6E6FA;> Computer Name<br>";
$content .= "<input type=\"text\" name=\"user\" maxlength=\"25\" size=\"20\" STYLE=background:#E6E6FA;> User<br>";
$content .= "<input type=\"text\" name=\"model\" maxlength=\"25\" size=\"20\" STYLE=background:#E6E6FA;> Model<br>";
$content .= "<input type=\"text\" name=\"lanmac\" maxlength=\"25\" size=\"20\" STYLE=background:#E6E6FA;> LAN MAC<br>";
$content .= "<input type=\"text\" name=\"wanmac\" maxlength=\"25\" size=\"20\" STYLE=background:#E6E6FA;> WWAN MAC<br>";
$content .= "<SELECT name=\"condition\" size=\"1\" STYLE=background:#E6E6FA;><OPTION SELECTED> <OPTION>Junk<OPTION>Poor<OPTION>Fair<OPTION>Good<OPTION>Excellent<OPTION>NEW</SELECT> Condition<br>";
$content .= "<SELECT name=\"status\" size=\"1\" STYLE=background:#E6E6FA;><OPTION SELECTED> <OPTION>Idle-Outdated<OPTION>Idle-Formatted<OPTION>Idle-Current<OPTION>In Use-Outdated<OPTION>In Use-Current<OPTION>New In Box</SELECT> Status<br>Please check all that apply...<br>";
$content .= "<input type=\"checkbox\" name=\"ad\" STYLE=background:#64748B;>Adobe<br>";
$content .= "<input type=\"checkbox\" name=\"av\" STYLE=background:#64748B;>Antivirus<br>";
$content .= "<input type=\"checkbox\" name=\"ja\" STYLE=background:#64748B;>Java RE<br>";
$content .= "<input type=\"checkbox\" name=\"nd\" STYLE=background:#64748B;>Network Drives<br>";
$content .= "<input type=\"checkbox\" name=\"sh\" STYLE=background:#64748B;>Shoretel<br>";
$content .= "<input type=\"checkbox\" name=\"sp\" STYLE=background:#64748B;>Anti-Spyware<br>";
$content .= "<input type=\"checkbox\" name=\"up\" STYLE=background:#64748B;>Microsoft Updates<br>";
$content .= "<input type=\"checkbox\" name=\"vp\" STYLE=background:#64748B;>VPN<br><br>";
$content .= "<input type=\"checkbox\" name=\"update\" STYLE=background:#AA0000;>  Update Record?<br>";
$content .= "<input type=\"submit\" value=\"Refresh\">";
$content .= " Sort by : <SELECT name=\"sortby\" size=\"1\" STYLE=background:#FFCC00;><OPTION SELECTED>Site<OPTION>Service Tag<OPTION>User<OPTION>Computer Name<OPTION>Model<OPTION>Condition<OPTION>Status<OPTION>Newest<OPTION>Oldest</SELECT>";
$content .= "</form></b><br>";

$content .= "<b>AD</b> = Adobe <b>AV</b> = Antivirus <b>JA</b> = Java RE <b>ND</b> = Network Drives<br><b>SH</b> = Shoretel <b>SP</b> = Anti-Spyware <b>UP</b> = MS Updates <b>VP</b> = VPN<br><br><table bgcolor=D3DCE6 cellpadding=0 cellspacing=0 width=100% border=1>";
$content .= "<th align=center>Service Tag</th>";
$content .= "<th align=center>Site</th>";
$content .= "<th align=center>Computer Name</th>";
$content .= "<th align=center>User</th>";
$content .= "<th align=center>Model</th>";
$content .= "<th align=center>LAN MAC</th>";
$content .= "<th align=center>WWAN MAC</th>";
$content .= "<th align=center>Condition</th>";
$content .= "<th align=center>Status</th>";
$content .= "<th align=center>AD</th>";
$content .= "<th align=center>AV</th>";
$content .= "<th align=center>JA</th>";
$content .= "<th align=center>ND</th>";
$content .= "<th align=center>SH</th>";
$content .= "<th align=center>SP</th>";
$content .= "<th align=center>UP</th>";
$content .= "<th align=center>VP</th>";
$content .= "<th align=center>Last Update</th>";

while($list1 = mysql_fetch_assoc($select_result)):
if (($list1['ad'])==0):
	$adimagesource = "http://www.disctribution.com/forums/images/unchecked.gif";
else:
	$adimagesource = "http://www.disctribution.com/forums/images/checked.gif";
endif;
if (($list1['av'])==0):
	$avimagesource = "http://www.disctribution.com/forums/images/unchecked.gif";
else:
	$avimagesource = "http://www.disctribution.com/forums/images/checked.gif";
endif;
if (($list1['ja'])==0):
	$jaimagesource = "http://www.disctribution.com/forums/images/unchecked.gif";
else:
	$jaimagesource = "http://www.disctribution.com/forums/images/checked.gif";
endif;
if (($list1['nd'])==0):
	$ndimagesource = "http://www.disctribution.com/forums/images/unchecked.gif";
else:
	$ndimagesource = "http://www.disctribution.com/forums/images/checked.gif";
endif;
if (($list1['sh'])==0):
	$shimagesource = "http://www.disctribution.com/forums/images/unchecked.gif";
else:
	$shimagesource = "http://www.disctribution.com/forums/images/checked.gif";
endif;
if (($list1['sp'])==0):
	$spimagesource = "http://www.disctribution.com/forums/images/unchecked.gif";
else:
	$spimagesource = "http://www.disctribution.com/forums/images/checked.gif";
endif;
if (($list1['up'])==0):
	$upimagesource = "http://www.disctribution.com/forums/images/unchecked.gif";
else:
	$upimagesource = "http://www.disctribution.com/forums/images/checked.gif";
endif;
if (($list1['vp'])==0):
	$vpimagesource = "http://www.disctribution.com/forums/images/unchecked.gif";
else:
	$vpimagesource = "http://www.disctribution.com/forums/images/checked.gif";
endif;

$content .= "<tr border=2 bgcolor=FFCC00>";
$content .= "<td align=center>".$list1['iservice']."</td>";
$content .= "<td align=center>".$list1['isite']."</td>";
$content .= "<td align=center>".$list1['icomputer']."</td>";
$content .= "<td align=center>".$list1['iuser']."</td>";
$content .= "<td align=center>".$list1['imodel']."</td>";
$content .= "<td align=center>".$list1['ilanmac']."</td>";
$content .= "<td align=center>".$list1['iwanmac']."</td>";
$content .= "<td align=center>".$list1['icondition']."</td>";
$content .= "<td align=center>".$list1['istatus']."</td>";
$content .= "<td align=center><img src=\"".$adimagesource."\"></img></td>";
$content .= "<td align=center><img src=\"".$avimagesource."\"></img></td>";
$content .= "<td align=center><img src=\"".$jaimagesource."\"></img></td>";
$content .= "<td align=center><img src=\"".$ndimagesource."\"></img></td>";
$content .= "<td align=center><img src=\"".$shimagesource."\"></img></td>";
$content .= "<td align=center><img src=\"".$spimagesource."\"></img></td>";
$content .= "<td align=center><img src=\"".$upimagesource."\"></img></td>";
$content .= "<td align=center><img src=\"".$vpimagesource."\"></img></td>";
$timeago = time() - $list1['idate'];
$daysago = number_format(($timeago / 86400),1);
$content .= "<td align=center>".$daysago." days ago</td>";
//$content .= "<td align=center>".$list1['idate']."</td>";
$content .= "</tr>";
endwhile;
$content .= "</table>";
// Make sure the user entered values for all three fields
if (strlen($_POST['service'])>0):// IF TRUE

// Add the values to the $data array
if (($_POST['update'])=="on")
{
if (($_POST['ad'])=="on"):
	$Pad = 1;
echo "AD is".$Pad;
else:
	$Pad = 0;
endif;
if (($_POST['av'])=="on"):
	$Pav = 1;
else:
	$Pav = 0;
endif;
if (($_POST['ja'])=="on"):
	$Pja = 1;
else:
	$Pja = 0;
endif;
if (($_POST['nd'])=="on"):
	$Pnd = 1;
else:
	$Pnd = 0;
endif;
if (($_POST['sh'])=="on"):
	$Psh = 1;
else:
	$Psh = 0;
endif;
if (($_POST['sp'])=="on"):
	$Psp = 1;
else:
	$Psp = 0;
endif;
if (($_POST['up'])=="on"):
	$Pup = 1;
else:
	$Pup = 0;
endif;
if (($_POST['vp'])=="on"):
	$Pvp = 1;
else:
	$Pvp = 0;
endif;

if (($_POST['service'])!=="")
{
	$uservice = $_POST['service'];
	$result2 = mysql_query('SELECT * FROM computeraudit WHERE iservice = \'$uservice\'', $conn);
	$list2 = mysql_fetch_assoc($result2);
	if ($Pad !== ($list2['ad'])):
		$uad = $Pad;
		$update_result = mysql_query("UPDATE computeraudit SET ad = '$uad' WHERE iservice = '$uservice' LIMIT 1;", $conn);
	else:
	echo $Pad." equals ".$list2['ad']."<br><br><br>";
	endif;
	if ($Pav !== ($list2['av'])):
		$uav = $Pav;
		$update_result = mysql_query("UPDATE computeraudit SET av = '$uav' WHERE iservice = '$uservice' LIMIT 1;", $conn);
	endif;
	if ($Pja !== ($list2['ja'])):
		$uja = $Pja;
		$update_result = mysql_query("UPDATE computeraudit SET ja = '$uja' WHERE iservice = '$uservice' LIMIT 1;", $conn);
	endif;
	if ($Pnd !== ($list2['nd'])):
		$und = $Pnd;
		$update_result = mysql_query("UPDATE computeraudit SET nd = '$und' WHERE iservice = '$uservice' LIMIT 1;", $conn);
	endif;
	if ($Psh !== ($list2['sh'])):
		$ush = $Psh;
		$update_result = mysql_query("UPDATE computeraudit SET sh = '$ush' WHERE iservice = '$uservice' LIMIT 1;", $conn);
	endif;
	if ($Psp !== ($list2['sp'])):
		$usp = $Psp;
		$update_result = mysql_query("UPDATE computeraudit SET sp = '$usp' WHERE iservice = '$uservice' LIMIT 1;", $conn);
	endif;
	if ($Pup !== ($list2['up'])):
		$uup = $Pup;
		$update_result = mysql_query("UPDATE computeraudit SET up = '$uup' WHERE iservice = '$uservice' LIMIT 1;", $conn);
	endif;
	if ($Pvp !== ($list2['vp'])):
		$uvp = $Pvp;
		$update_result = mysql_query("UPDATE computeraudit SET vp = '$uvp' WHERE iservice = '$uservice' LIMIT 1;", $conn);
	endif;

	$utime = time();
	$update_time = mysql_query("UPDATE computeraudit SET idate = '$utime' WHERE iservice = '$uservice' LIMIT 1;", $conn);
	if (($_POST['site'])!==""):
		$usite = $_POST['site'];
		$update_result = mysql_query("UPDATE computeraudit SET isite = '$usite' WHERE iservice = '$uservice' LIMIT 1;", $conn);
	endif;
	if (($_POST['computer'])!==""):
		$ucomputer = $_POST['computer'];
		$update_result = mysql_query("UPDATE computeraudit SET icomputer = '$ucomputer' WHERE iservice = '$uservice' LIMIT 1;", $conn);
	endif;
	if (($_POST['user'])!==""):
		$uuser = $_POST['user'];
		$update_result = mysql_query("UPDATE computeraudit SET iuser = '$uuser' WHERE iservice = '$uservice' LIMIT 1;", $conn);
	endif;
	if (($_POST['model'])!==""):
		$umodel = $_POST['model'];
		$update_result = mysql_query("UPDATE computeraudit SET imodel = '$umodel' WHERE iservice = '$uservice' LIMIT 1;", $conn);
	endif;
	if (($_POST['lanmac'])!==""):
		$ulanmac = $_POST['lanmac'];
		$update_result = mysql_query("UPDATE computeraudit SET ilanmac = '$ulanmac' WHERE iservice = '$uservice' LIMIT 1;", $conn);
	endif;
	if (($_POST['wanmac'])!==""):
		$uwanmac = $_POST['wanmac'];
		$update_result = mysql_query("UPDATE computeraudit SET iwanmac = '$uwanmac' WHERE iservice = '$uservice' LIMIT 1;", $conn);
	endif;
	if (($_POST['condition'])!==""):
		$ucondition = $_POST['condition'];
		$update_result = mysql_query("UPDATE computeraudit SET icondition = '$ucondition' WHERE iservice = '$uservice' LIMIT 1;", $conn);
	endif;
	if (($_POST['status'])!==""):
		$ustatus = $_POST['status'];
		$update_result = mysql_query("UPDATE computeraudit SET istatus = '$ustatus' WHERE iservice = '$uservice' LIMIT 1;", $conn);
	endif;
echo "<br><font color=FF22CC>UPDATED!</font><br>";
}
else
{
echo "<br><font color=\"FF0000\"><b>Service Tag MUST be entered to update!</b></font>";
}

}
else
{
if (($_POST['ad'])=="on"):
$iad = 1;
else:
$iad = 0;
endif;
if (($_POST['av'])=="on"):
$iav = 1;
else:
$iav = 0;
endif;
if (($_POST['ja'])=="on"):
$ija = 1;
else:
$ija = 0;
endif;
if (($_POST['nd'])=="on"):
$ind = 1;
else:
$ind = 0;
endif;
if (($_POST['sh'])=="on"):
$ish = 1;
else:
$ish = 0;
endif;
if (($_POST['sp'])=="on"):
$isp = 1;
else:
$isp = 0;
endif;
if (($_POST['up'])=="on"):
$iup = 1;
else:
$iup = 0;
endif;
if (($_POST['vp'])=="on"):
$ivp = 1;
else:
$ivp = 0;
endif;
$data = array('isite' => $_POST['site'],'icomputer' => $_POST['computer'],'iuser' => $_POST['user'],'imodel' => $_POST['model'], 'iservice' => $_POST['service'], 'ilanmac' => $_POST['lanmac'], 'iwanmac' => $_POST['wanmac'], 'icondition' => $_POST['condition'], 'istatus' => $_POST['status'], 'idate' => time(), 'ad' => $iad, 'av' => $iav, 'ja' => $ija, 'nd' => $ind, 'sh' => $ish, 'sp' => $isp, 'up' => $iup, 'vp' => $ivp);
$field_names = array();
$values = array();
// Loop through the array
foreach($data as $field_name => $value):
if(empty($value)) continue;
$field_names[] = $field_name;
// Make sure no invalid information was put into the form
$values[] = "'".mysql_real_escape_string($value, $conn)."'";
endforeach;
// Add the results into the table
$insert_result = mysql_query("INSERT INTO $dbTable (".join(',',$field_names).") VALUES(".join(',',$values).")", $conn);
$content .= "<Font color=FF0000><b>Added data, please <i>REFRESH</i></b><br></font>";
}

endif;

echo $content;


?>

Link to comment
https://forums.phpfreaks.com/topic/65440-check-this-code-out/
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.