Reply to Re: PHP page get executed twice

Your name:

Reply:


Posted by Marcel on 06/01/06 14:34

### mainpage

<?php
#error_reporting(E_ALL); // alle fouten weergeven
#error_reporting(0); // geen fouten weergeven
error_reporting(E_ALL ^ E_NOTICE); // alle fouten behalve notices weergeven

header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past

# database connection
require_once("../Connections/connect_communication.php");

# mssql_escape function
require_once("../php_functions/mssql_escape.php");

#
require_once("../php_functions/validator.php");

#
require_once("../php_functions/sql.php");

$uploaddir = 'C:/Inetpub/domain.com/newsletters/uploaded_images/';


mail("info@mediaquest.nl","Test Newsletters",$_SERVER['HTTP_REFERER'].' -
'.$_SERVER['REMOTE_HOST'].' - '.$_SERVER['REQUEST_URI']);


function img_exists($filename_to_check) {

$SQLcheck = "SELECT id FROM com_images WHERE img_original_name =
'".trim($filename_to_check)."'";

#echo '<hr />'.$SQLcheck.'<hr />';

$RESULTcheck = mssql_query($SQLcheck);

if(mssql_num_rows($RESULTcheck) > 0) {
return true;
} else {
return false;
}

}

if($_POST['Submit']) {

rule(!$_POST['nwi_name'],
"You did not fill in a name for this newsitem");

/*
rule(isset($_FILES['nwi_image1']['name']) &&
img_exists($_FILES['nwi_image1']['name']),
"The filename of Image 1 that you are trying to upload allready exists");

rule(isset($_FILES['nwi_image2']['name']) &&
img_exists($_FILES['nwi_image2']['name']),
"The filename of Image 2 that you are trying to upload allready exists");

rule(isset($_FILES['nwi_image3']['name']) &&
img_exists($_FILES['nwi_image3']['name']),
"The filename of Image 3 that you are trying to upload allready exists");

rule(isset($_FILES['nwi_image4']['name']) &&
img_exists($_FILES['nwi_image4']['name']),
"The filename of Image 4 that you are trying to upload allready exists");
*/

# validation of userinput
if(getError()) {

# zo ja error message toekennen aan $err_msg
$err_msg = getError();

# leeg maken array validations
clearValidations();

$tmp_nwi_name = $_POST['nwi_name'];
$tmp_nwi_description = $_POST['nwi_description'];

$tmp_nwi_heading = $_POST['nwi_heading'];

$tmp_nwi_subheading1 = $_POST['nwi_subheading1'];
$tmp_nwi_image1 = $_POST['nwi_image1'];
$tmp_nwi_alignment1 = $_POST['nwi_alignment1'];
$tmp_nwi_text1 = $_POST['nwi_text1'];

$tmp_nwi_subheading2 = $_POST['nwi_subheading2'];
$tmp_nwi_image2 = $_POST['nwi_image2'];
$tmp_nwi_alignment2 = $_POST['nwi_alignment2'];
$tmp_nwi_text2 = $_POST['nwi_text2'];

$tmp_nwi_subheading3 = $_POST['nwi_subheading3'];
$tmp_nwi_image3 = $_POST['nwi_image3'];
$tmp_nwi_alignment3 = $_POST['nwi_alignment3'];
$tmp_nwi_text3 = $_POST['nwi_text3'];

$tmp_nwi_subheading4 = $_POST['nwi_subheading4'];
$tmp_nwi_image4 = $_POST['nwi_image4'];
$tmp_nwi_alignment4 = $_POST['nwi_alignment4'];
$tmp_nwi_text4 = $_POST['nwi_text4'];


} else {
# all userinput proofed ok, let's write data to database

#echo '<hr />userinput ok!!!<hr />';

clearValidations();

# indien er images geupload worden
# img 1
if($_FILES['nwi_image1']['name']) {

$tsql_img1 = new sql('com_images');

$tsql_img1->add("img_original_name",$_FILES['nwi_image1']['name']);
$tsql_img1->add("img_tmp_name",$_FILES['nwi_image1']['tmp_name']);
$tsql_img1->add("img_type",$_FILES['nwi_image1']['type']);
if(GetImageSize($_FILES['nwi_image1']['tmp_name'])) {

$img_dimensions = GetImageSize($_FILES['nwi_image1']['tmp_name']);

$tsql_img1->add("img_width",$img_dimensions[0]);
$tsql_img1->add("img_height",$img_dimensions[1]);

}
$tsql_img1->add("img_bytes",$_FILES['nwi_image1']['size']);

# C:\Inetpub\domain.com\newsletters\uploaded_images

if(move_uploaded_file($_FILES['nwi_image1']['tmp_name'],
$uploaddir.$_FILES["nwi_image1"]["name"])) {

$tsql_img1->add("img_system_name","http://www.domain.com/newsletters/uploaded_images/".$_FILES["nwi_image1"]["name"]);

}

$tsql_img1->insert();

$nwi_image1_id = $tsql_img1->getLastID();

unset($tsql_img1);

}

# indien er images geupload worden
# img 2
if($_FILES['nwi_image2']['name']) {

$tsql_img2 = new sql('com_images');

$tsql_img2->add("img_original_name",$_FILES['nwi_image2']['name']);
$tsql_img2->add("img_tmp_name",$_FILES['nwi_image2']['tmp_name']);
$tsql_img2->add("img_type",$_FILES['nwi_image2']['type']);
if(GetImageSize($_FILES['nwi_image2']['tmp_name'])) {

$img_dimensions = GetImageSize($_FILES['nwi_image2']['tmp_name']);

$tsql_img2->add("img_width",$img_dimensions[0]);
$tsql_img2->add("img_height",$img_dimensions[1]);

}
$tsql_img2->add("img_bytes",$_FILES['nwi_image2']['size']);

# C:\Inetpub\domain.com\newsletters\uploaded_images

if(move_uploaded_file($_FILES['nwi_image2']['tmp_name'],
$uploaddir.$_FILES["nwi_image2"]["name"])) {

$tsql_img2->add("img_system_name","http://www.domain.com/newsletters/uploaded_images/".$_FILES["nwi_image2"]["name"]);

}

$tsql_img2->insert();

$nwi_image2_id = $tsql_img2->getLastID();

unset($tsql_img2);

}

# indien er images geupload worden
# img 3
if($_FILES['nwi_image3']['name']) {

$tsql_img3 = new sql('com_images');

$tsql_img3->add("img_original_name",$_FILES['nwi_image3']['name']);
$tsql_img3->add("img_tmp_name",$_FILES['nwi_image3']['tmp_name']);
$tsql_img3->add("img_type",$_FILES['nwi_image3']['type']);
if(GetImageSize($_FILES['nwi_image3']['tmp_name'])) {

$img_dimensions = GetImageSize($_FILES['nwi_image3']['tmp_name']);

$tsql_img3->add("img_width",$img_dimensions[0]);
$tsql_img3->add("img_height",$img_dimensions[1]);

}
$tsql_img3->add("img_bytes",$_FILES['nwi_image3']['size']);

# C:\Inetpub\domain.com\newsletters\uploaded_images

if(move_uploaded_file($_FILES['nwi_image3']['tmp_name'],
$uploaddir.$_FILES["nwi_image3"]["name"])) {

$tsql_img3->add("img_system_name","http://www.domain.com/newsletters/uploaded_images/".$_FILES["nwi_image3"]["name"]);

}

$tsql_img3->insert();

$nwi_image3_id = $tsql_img3->getLastID();

unset($tsql_img3);

}

# indien er images geupload worden
# img 4
if($_FILES['nwi_image4']['name']) {

$tsql_img4 = new sql('com_images');

$tsql_img4->add("img_original_name",$_FILES['nwi_image4']['name']);
$tsql_img4->add("img_tmp_name",$_FILES['nwi_image4']['tmp_name']);
$tsql_img4->add("img_type",$_FILES['nwi_image4']['type']);
if(GetImageSize($_FILES['nwi_image4']['tmp_name'])) {

$img_dimensions = GetImageSize($_FILES['nwi_image4']['tmp_name']);

$tsql_img4->add("img_width",$img_dimensions[0]);
$tsql_img4->add("img_height",$img_dimensions[1]);

}
$tsql_img4->add("img_bytes",$_FILES['nwi_image4']['size']);

# C:\Inetpub\domain.com\newsletters\uploaded_images

if(move_uploaded_file($_FILES['nwi_image4']['tmp_name'],
$uploaddir.$_FILES["nwi_image4"]["name"])) {

$tsql_img4->add("img_system_name","http://www.domain.com/newsletters/uploaded_images/".$_FILES["nwi_image4"]["name"]);

}

$tsql_img4->insert();

$nwi_image4_id = $tsql_img4->getLastID();

unset($tsql_img4);

}




$tsql = new sql('com_newsitems');


$tsql->add("nwi_name",$_POST['nwi_name']);
$tsql->add("nwi_description",$_POST['nwi_description']);

$tsql->add("nwi_heading",$_POST['nwi_heading']);

$tsql->add("nwi_subheading1",$_POST['nwi_subheading1']);
if($_FILES['nwi_image1']['name']) {
$tsql->add("nwi_image1",$nwi_image1_id);
$tsql->add("nwi_alignment1",$_POST['nwi_alignment1']);
} else {

if(!$_POST['nwi_image1_del']) {
$tsql->exclude("nwi_image1");
}

}
$tsql->add("nwi_text1",$_POST['nwi_text1']);

$tsql->add("nwi_subheading2",$_POST['nwi_subheading2']);
if($_FILES['nwi_image2']['name']) {
$tsql->add("nwi_image2",$nwi_image2_id);
$tsql->add("nwi_alignment2",$_POST['nwi_alignment2']);
} else {

if(!$_POST['nwi_image2_del']) {
$tsql->exclude("nwi_image2");
}

}
$tsql->add("nwi_text2",$_POST['nwi_text2']);

$tsql->add("nwi_subheading3",$_POST['nwi_subheading3']);
if($_FILES['nwi_image3']['name']) {
$tsql->add("nwi_image3",$nwi_image3_id);
$tsql->add("nwi_alignment3",$_POST['nwi_alignment3']);
} else {

if(!$_POST['nwi_image3_del']) {
$tsql->exclude("nwi_image3");
}

}
$tsql->add("nwi_text3",$_POST['nwi_text3']);

$tsql->add("nwi_subheading4",$_POST['nwi_subheading4']);
if($_FILES['nwi_image4']['name']) {
$tsql->add("nwi_image4",$nwi_image4_id);
$tsql->add("nwi_alignment4",$_POST['nwi_alignment4']);
} else {

if(!$_POST['nwi_image4_del']) {
$tsql->exclude("nwi_image4");
}

}
$tsql->add("nwi_text4",$_POST['nwi_text4']);


$tsql->exclude("id");
$tsql->exclude("nwi_date_created");


$tsql->update("id",$_POST['id']);

unset($tsql);

unset($_FILES);

header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Location:newsitems.php");


#echo $tsql->getSQLupdate();



}

} else {

$SQL = "SELECT * FROM com_newsitems WHERE id = ".$_GET['id'];

$RESULT = mssql_query($SQL);

$RECORD = mssql_fetch_array($RESULT);

$tmp_nwi_name = $RECORD['nwi_name'];
$tmp_nwi_description = $RECORD['nwi_description'];

$tmp_nwi_heading = $RECORD['nwi_heading'];

$tmp_nwi_subheading1 = $RECORD['nwi_subheading1'];
$tmp_nwi_image1 = $RECORD['nwi_image1'];


if($RECORD['nwi_image1']) {

$SQLimg1 = "SELECT img_original_name FROM com_images WHERE id =
".$RECORD['nwi_image1'];

$RESULTimg1 = mssql_query($SQLimg1);

if(mssql_num_rows($RESULTimg1) > 0) {

$RECORDimg1 = mssql_fetch_array($RESULTimg1);

$nwi_image1_name = $RECORDimg1['img_original_name'];

}

}

$tmp_nwi_alignment1 = $RECORD['nwi_alignment1'];
$tmp_nwi_text1 = $RECORD['nwi_text1'];

$tmp_nwi_subheading2 = $RECORD['nwi_subheading2'];
$tmp_nwi_image2 = $RECORD['nwi_image2'];

if($RECORD['nwi_image2']) {

$SQLimg2 = "SELECT img_original_name FROM com_images WHERE id =
".$RECORD['nwi_image2'];

$RESULTimg2 = mssql_query($SQLimg2);

if(mssql_num_rows($RESULTimg2) > 0) {

$RECORDimg2 = mssql_fetch_array($RESULTimg2);

$nwi_image2_name = $RECORDimg2['img_original_name'];

}

}

$tmp_nwi_alignment2 = $RECORD['nwi_alignment2'];
$tmp_nwi_text2 = $RECORD['nwi_text2'];

$tmp_nwi_subheading3 = $RECORD['nwi_subheading3'];
$tmp_nwi_image3 = $RECORD['nwi_image3'];

if($RECORD['nwi_image3']) {

$SQLimg3 = "SELECT img_original_name FROM com_images WHERE id =
".$RECORD['nwi_image3'];

$RESULTimg3 = mssql_query($SQLimg3);

if(mssql_num_rows($RESULTimg3) > 0) {

$RECORDimg3 = mssql_fetch_array($RESULTimg3);

$nwi_image3_name = $RECORDimg3['img_original_name'];

}

}

$tmp_nwi_alignment3 = $RECORD['nwi_alignment3'];
$tmp_nwi_text3 = $RECORD['nwi_text3'];

$tmp_nwi_subheading4 = $RECORD['nwi_subheading4'];
$tmp_nwi_image4 = $RECORD['nwi_image4'];

if($RECORD['nwi_image4']) {

$SQLimg4 = "SELECT img_original_name FROM com_images WHERE id =
".$RECORD['nwi_image4'];

$RESULTimg4 = mssql_query($SQLimg4);

if(mssql_num_rows($RESULTimg4) > 0) {

$RECORDimg4 = mssql_fetch_array($RESULTimg4);

$nwi_image4_name = $RECORDimg4['img_original_name'];

}

}

$tmp_nwi_alignment4 = $RECORD['nwi_alignment4'];
$tmp_nwi_text4 = $RECORD['nwi_text4'];

$tmp_id = $_GET['id'];

}

?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>domain.com CMS Newsletters</title>
<link href="styles/newslettercms.css" rel="stylesheet" type="text/css">
</head>
<body vlink="#000066" alink="#FF3300">
<a name="Top"></a>
<form action="test2.php" method="post" enctype="multipart/form-data"
name="form1">
<input type="hidden" name="id" value="<?php echo $tmp_id; ?>">
<table width="748" border="0" align="center" cellpadding="1" cellspacing="1"
bgcolor="#003875">
<tr>
<td width="744" bgcolor="#ffffff">
<table width="744" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="166" height="79">
<div align="center"><a href="http://www.domain.com"><img
src="images/mv-logo.gif" alt="domain.com" width="153" height="50"
border="0"></a></div>
</td>
<td width="578" align="right"><img src="images/header-1.jpg"
alt="domain.com" width="550" height="79"></td>
</tr>
<tr>
<td colspan="2" bgcolor="#003875">
<table width="744" border="0" cellspacing="0" cellpadding="4">
<tr>
<td width="468"><font color="#ffffff" face="Verdana, Arial, Helvetica,
sans-serif" size="2"><strong>domain.com
CMS Newsletters</strong></font></td>
<td width="260" align="right"><font color="#ffffff" face="Verdana, Arial,
Helvetica, sans-serif" size="2">Welcome
administrator</font></td>
</tr>
</table>
</td>
</tr>
</table>
<table width="744" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="748" valign="top">
<table width="744" border="0" align="center" cellpadding="4"
cellspacing="0">
<tr>
<td valign="top" align="center" bgcolor="#f1f1f1"><font color="#003875"
face="Arial, Helvetica, sans-serif" size="2"> <strong>|</strong> <a
href="emailaddresses.php">View/manage
e-mail adressess</a> <strong>|</strong> <a
href="newsletters.php">View/manage newsletters</a> <strong>|</strong> <a
href="newsitems.php">View/manage
newsitems</a> <strong>|</strong> <a href="#">View userhistory</a>
<strong>|</strong> </font></td>
</tr>
</table>
<br>
<table width="736" border="0" align="center" cellpadding="2"
cellspacing="0">
<tr>
<td width="732"><font color="#003875" face="Arial, Helvetica, sans-serif"
size="3"><strong>Edit newsitem </strong></font></td>
</tr>
<tr>
<td align="right"><font color="#000000" face="Arial, Helvetica, sans-serif"
size="2"><a href="addnewsitem.php">create new newsitem</a>
&raquo;</font></td>
</tr>
<?php
if($err_msg) {
?>
<tr>
<td><font color="#FF0000" face="Arial, Helvetica, sans-serif"
size="2"><strong><?php echo $err_msg; ?></strong></font></td>
</tr>
<?php
}
?>
</table>
<font face="Arial, Helvetica, sans-serif">
<table class="table3">
<tr>
<td>Newsitem name</td>
<td>
<input name="nwi_name" type="text" class="nwi_title" id="nwi_name"
value="<?php echo $tmp_nwi_name; ?>">
</td>
</tr>
<tr>
<td>Newsitem description</td>
<td>
<textarea name="nwi_description" id="nwi_description" cols="35"
rows="6"><?php echo $tmp_nwi_description; ?></textarea>
</td>
</tr>

<tr>
<td colspan="2"><hr /></td>
</tr>
<tr>
<td>Heading</td>
<td>
<input name="nwi_heading" type="text" class="nwl_nwi_name" id="nwi_heading"
value="<?php echo $tmp_nwi_heading; ?>">
</td>
</tr>
<tr>
<td colspan="2"><hr /></td>
</tr>

<!-- 1 -->
<tr>
<td>Subheading 1</td>
<td>
<input name="nwi_subheading1" type="text" class="nwl_nwi_name"
id="nwi_subheading1" value="<?php echo $tmp_nwi_subheading1; ?>">
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>
<?php echo $nwi_image1_name; ?><input name="nwi_image1_del" type="checkbox"
id="nwi_image1_del" value="<?php echo $nwi_image1_name; ?>">
</td>
</tr>
<tr>
<td>Image 1</td>
<td>
<input name="nwi_image1" type="file" class="nwl_nwi_name" id="nwi_image1"
value="<?php echo $tmp_nwi_image1; ?>">
</td>
</tr>
<tr>
<td>Horzontal alignment 1</td>
<td>
<input name="nwi_alignment1" type="radio" value="L" <?php
if($tmp_nwi_alignment1 == 'L') {echo 'checked';} ?>> Left
<input name="nwi_alignment1" type="radio" value="C" <?php
if($tmp_nwi_alignment1 == 'C') {echo 'checked';} ?>> Center
<input name="nwi_alignment1" type="radio" value="R" <?php
if($tmp_nwi_alignment1 == 'R') {echo 'checked';} ?>> Right
</td>
</tr>
<tr>
<td>Text 1</td>
<td>
<textarea name="nwi_text1" id="nwi_text1" cols="35" rows="6"><?php echo
$tmp_nwi_text1; ?></textarea>
</td>
</tr>
<tr>
<td colspan="2"><hr /></td>
</tr>

<!-- 2 -->
<tr>
<td>Subheading 2</td>
<td>
<input name="nwi_subheading2" type="text" class="nwl_nwi_name"
id="nwi_subheading2" value="<?php echo $tmp_nwi_subheading2; ?>">
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>
<?php echo $nwi_image2_name; ?><input name="nwi_image2_del" type="checkbox"
id="nwi_image2_del" value="<?php echo $nwi_image2_name; ?>">
</td>
</tr>
<tr>
<td>Image 2</td>
<td>
<input name="nwi_image2" type="file" class="nwl_nwi_name" id="nwi_image2"
value="<?php echo $tmp_nwi_image2; ?>">
</td>
</tr>
<tr>
<td>Horzontal alignment 2</td>
<td>
<input name="nwi_alignment2" type="radio" value="L" <?php
if($tmp_nwi_alignment2 == 'L') {echo 'checked';} ?>> Left
<input name="nwi_alignment2" type="radio" value="C" <?php
if($tmp_nwi_alignment2 == 'C') {echo 'checked';} ?>> Center
<input name="nwi_alignment2" type="radio" value="R" <?php
if($tmp_nwi_alignment2 == 'R') {echo 'checked';} ?>> Right
</td>
</tr>
<tr>
<td>Text 2</td>
<td>
<textarea name="nwi_text2" id="nwi_text2" cols="35" rows="6"><?php echo
$tmp_nwi_text2; ?></textarea>
</td>
</tr>
<tr>
<td colspan="2"><hr /></td>
</tr>


<!-- 3 -->
<tr>
<td>Subheading 3</td>
<td>
<input name="nwi_subheading3" type="text" class="nwl_nwi_name"
id="nwi_subheading3" value="<?php echo $tmp_nwi_subheading3; ?>">
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>
<?php echo $nwi_image3_name; ?><input name="nwi_image3_del" type="checkbox"
id="nwi_image3_del" value="<?php echo $nwi_image3_name; ?>">
</td>
</tr>
<tr>
<td>Image 3</td>
<td>
<input name="nwi_image3" type="file" class="nwl_nwi_name" id="nwi_image3"
value="<?php echo $tmp_nwi_image3; ?>">
</td>
</tr>
<tr>
<td>Horzontal alignment 3</td>
<td>
<input name="nwi_alignment3" type="radio" value="L" <?php
if($tmp_nwi_alignment3 == 'L') {echo 'checked';} ?>> Left
<input name="nwi_alignment3" type="radio" value="C" <?php
if($tmp_nwi_alignment3 == 'C') {echo 'checked';} ?>> Center
<input name="nwi_alignment3" type="radio" value="R" <?php
if($tmp_nwi_alignment3 == 'R') {echo 'checked';} ?>> Right
</td>
</tr>
<tr>
<td>Text 3</td>
<td>
<textarea name="nwi_text3" id="nwi_text3" cols="35" rows="6"><?php echo
$tmp_nwi_text3; ?></textarea>
</td>
</tr>
<tr>
<td colspan="2"><hr /></td>
</tr>



<!-- 4 -->
<tr>
<td>Subheading 4</td>
<td>
<input name="nwi_subheading4" type="text" class="nwl_nwi_name"
id="nwi_subheading4" value="<?php echo $tmp_nwi_subheading4; ?>">
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>
<?php echo $nwi_image4_name; ?><input name="nwi_image4_del" type="checkbox"
id="nwi_image4_del" value="<?php echo $nwi_image4_name; ?>">
</td>
</tr>
<tr>
<td>Image 4</td>
<td>
<input name="nwi_image4" type="file" class="nwl_nwi_name" id="nwi_image4"
value="<?php echo $tmp_nwi_image4; ?>">
</td>
</tr>
<tr>
<td>Horzontal alignment 4</td>
<td>
<input name="nwi_alignment4" type="radio" value="L" <?php
if($tmp_nwi_alignment4 == 'L') {echo 'checked';} ?>> Left
<input name="nwi_alignment4" type="radio" value="C" <?php
if($tmp_nwi_alignment4 == 'C') {echo 'checked';} ?>> Center
<input name="nwi_alignment4" type="radio" value="R" <?php
if($tmp_nwi_alignment4 == 'R') {echo 'checked';} ?>> Right
</td>
</tr>
<tr>
<td>Text 4</td>
<td>
<textarea name="nwi_text4" id="nwi_text4" cols="35" rows="6"><?php echo
$tmp_nwi_text4; ?></textarea>
</td>
</tr>
<tr>
<td colspan="2"><hr /></td>
</tr>

<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>
<input type="submit" name="Submit" value="gereed &raquo;">
</td>
</tr>
</table>
</font> <br>
<br>
</td>
</tr>
<tr>
<td bgcolor="#008C9A">&nbsp;</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</body>
</html>


### sql class

# class column om info over columns in op te slaan
class column {

var $name; # columnname
var $xtype; # datatype
var $colid; # positie
var $isnullable; # of er null in deze column mag staan
var $newval; # value die geinsert of updated moet worden

}

# class sql
class sql {

var $table; # de table waar dit sql-object betrekking op heeft
var $columns = array(); # de columns in deze table, in deze array worden
alle columns uit de tabel opgeslagen, per column wordt een column-object
aangemaakt die diverse info bevat over de column
var $excluded_columns = array();


# default constructor, neemt als argument de tabel waarop dit sql object
betrekking heeft
public function __construct($table) {

$this->table = $table;

# sql statement om info over columns in deze tabel te verkrijgen
$SQLsys = "SELECT syscolumns.name, syscolumns.xtype, syscolumns.colid,
syscolumns.isnullable FROM sysobjects INNER JOIN syscolumns ON sysobjects.id
= syscolumns.id WHERE (sysobjects.name = N'".$this->table."')";

$RESULTsys = mssql_query($SQLsys);

# while-loop om alle info van de diverse columns uit te lezen en toe te
kennen aan column-objecten
while($RECORDsys = mssql_fetch_array($RESULTsys)) {

# aanmaken nieuw column object
$tmp_column = new column;

# toekennen waarden uit database aan net gemaakte column-object
$tmp_column->name = $RECORDsys['name'];
$tmp_column->xtype = $RECORDsys['xtype'];
$tmp_column->colid = $RECORDsys['colid'];
$tmp_column->isnullable = $RECORDsys['isnullable'];

# net gemaakte column-object toevoegen aan columns array
$this->columns[$RECORDsys['name']] = $tmp_column;

}

}

function add($column,$value) {

$this->columns[$column]->newval = "$value";

}

# this is the static comparing function
function cmp_obj($a, $b) {

$al = $a->colid;

$bl = $b->colid;

if ($al == $bl) {

return 0;

}

return ($al > $bl) ? +1 : -1;

}

# functie om te kijken of een bepaalde column een string-column of een int
column is
function is_stringval($val_to_check) {

/*
61 = datetime
167 = varchar
56 = int
175 = char
52 = smallint
106 = decimal
35 = text
*/

if($val_to_check == 61 || $val_to_check == 167 || $val_to_check == 175 ||
$val_to_check == 35) {
return true;
} else {
return false;
}

}

function mssql_escape($data) {

if( get_magic_quotes_gpc() ) {
$data = stripslashes($data);
}

return str_replace("'", "''", $data);

}


function insert() {

# op volgorde zetten van columns
uasort($this->columns,array("sql", "cmp_obj"));

# start sql-statement
$SQLins = "";

$SQLins .= "INSERT INTO ".$this->table." (";

foreach($this->columns as $value) {
if(isset($value->newval) && $value->newval != '') {
$SQLins .= $value->name.",";
}
}

$SQLins = rtrim($SQLins,",");

$SQLins .= ") VALUES (";


foreach($this->columns as $value) {
if(isset($value->newval) && $value->newval != '') {
if($this->is_stringval($value->xtype)) {
$SQLins .= "'".$this->mssql_escape($value->newval)."',";
} else {
$SQLins .= $value->newval.",";
}

}
}

$SQLins = rtrim($SQLins,",");

$SQLins .= ")";

#echo '<hr />'.$SQLins.'<hr />';

mssql_query($SQLins);

}

function exclude($column_name) {

array_push($this->excluded_columns,$column_name);

}

function
update($keycolumn1,$keyval1,$keycolumn2='',$keyval2='',$keycolumn3='',$keyval3='')
{

# op volgorde zetten van columns
uasort($this->columns,array("sql", "cmp_obj"));

# start sql-statement
$SQLupd = "";

$SQLupd .= "UPDATE ".$this->table." SET ";



foreach($this->columns as $value) {
if(!in_array($value->name,$this->excluded_columns)) {
if(isset($value->newval) && trim($value->newval) != '') {
if($this->is_stringval($value->xtype)) {
$SQLupd .= $value->name." =
'".$this->mssql_escape($value->newval)."',";
} else {
$SQLupd .= $value->name."= ".$value->newval.",";
}

} else {
$SQLupd .= $value->name."= NULL,";
}
}
}

$SQLupd = rtrim($SQLupd,",");

$SQLupd .= " WHERE ";

if($keycolumn1) {

if($this->is_stringval($this->columns[$keycolumn1]->xtype)) {
$SQLupd .= $keycolumn1." = '".$keyval1."'";
} else {
$SQLupd .= $keycolumn1." = ".$keyval1;
}

}
if($keycolumn2) {

if($this->is_stringval($this->columns[$keycolumn2]->xtype)) {
$SQLupd .= " AND ".$keycolumn2." = '".$keyval2."'";
} else {
$SQLupd .= " AND ".$keycolumn2." = ".$keyval2;
}

}
if($keycolumn3) {

if($this->is_stringval($this->columns[$keycolumn3]->xtype)) {
$SQLupd .= " AND ".$keycolumn3." = '".$keyval3."'";
} else {
$SQLupd .= " AND ".$keycolumn3." = ".$keyval3;
}

}

#echo $SQLupd;

mssql_query($SQLupd);

}


function
getSQLupdate($keycolumn1,$keyval1,$keycolumn2='',$keyval2='',$keycolumn3='',$keyval3='')
{

# op volgorde zetten van columns
uasort($this->columns,array("sql", "cmp_obj"));

# start sql-statement
$SQLupd = "";

$SQLupd .= "UPDATE ".$this->table." SET ";



foreach($this->columns as $value) {
if(!in_array($value->name,$this->excluded_columns)) {
if(isset($value->newval) && trim($value->newval) != '') {
if($this->is_stringval($value->xtype)) {
$SQLupd .= $value->name." =
'".$this->mssql_escape($value->newval)."',";
} else {
$SQLupd .= $value->name."= ".$value->newval.",";
}

} else {
$SQLupd .= $value->name."= NULL,";
}
}
}

$SQLupd = rtrim($SQLupd,",");

$SQLupd .= " WHERE ";

if($keycolumn1) {

if($this->is_stringval($this->columns[$keycolumn1]->xtype)) {
$SQLupd .= $keycolumn1." = '".$keyval1."'";
} else {
$SQLupd .= $keycolumn1." = ".$keyval1;
}

}
if($keycolumn2) {

if($this->is_stringval($this->columns[$keycolumn2]->xtype)) {
$SQLupd .= " AND ".$keycolumn2." = '".$keyval2."'";
} else {
$SQLupd .= " AND ".$keycolumn2." = ".$keyval2;
}

}
if($keycolumn3) {

if($this->is_stringval($this->columns[$keycolumn3]->xtype)) {
$SQLupd .= " AND ".$keycolumn3." = '".$keyval3."'";
} else {
$SQLupd .= " AND ".$keycolumn3." = ".$keyval3;
}

}

return $SQLupd;

}

function getLastID() {

$SQLlastid = "SELECT lastid = @@IDENTITY";

$RESULTlastid = mssql_query($SQLlastid);

$RECORDlastid = mssql_fetch_array($RESULTlastid);

return $RECORDlastid['lastid'];

}

}

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация