...

View Full Version : add new table won't work. please help..



mjs
06-22-2010, 02:44 AM
Hi,
I appreciated if some one there can help me out, i want to add new table 'config' but seem not work fine.

<?php
/********************** config.PHP**************************/
include 'dbc.php';
page_protect();

$err = array();
$msg = array();

if($_POST['doSave'] == 'Save')
{

// Filter POST data for harmful code (sanitize)
foreach($_POST as $key => $value) {
$data[$key] = filter($value);
}

if(empty($err)) {

mysql_query("UPDATE config SET
`SITE_TITLE` = '$data[SITE_TITLE]',
`SITE_KEYWORDS` = '$data[SITE_KEYWORDS]',
`SITE_DESCRIPTIONS` = '$data[SITE_DESCRIPTIONS]',
`SITE_MENUS` = '$data[SITE_MENUS]',
`SITE_COPYRIGHT` = '$data[SITE_COPYRIGHT]',
`SITE_METATAGS` = '$data[SITE_METATAGS]'
WHERE ID='$data[ID]") or die(mysql_error());

$msg[] = "Profile Sucessfully saved";
}
}
//$rs_edits = mysql_query("select * from users where ID='$_SESSION[user_id]'");
$rs_edits = mysql_query("select * from users where ID='$data[ID]'") or die(mysql_error());
?>
<html>
<head>
<title>Profile Edit</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="styles.css" rel="stylesheet" type="text/css">
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="14%" valign="top">
<?php
/********************* MYACCOUNT MENU *******************/
if (isset($_SESSION['user_id'])) {?>
<div class="myaccount">
<p><strong>My Account</strong></p>
<a href="index.php">Home</a><br><br>
<a href="profile_edit.php">Edit Profile</a><br><br>
<a href="pwd_change.php">Change Password</a><br><br>
<a href="logout.php">Logout </a><br><br>
</div>
<?php }
/*****************************END************************/
if (checkAdmin()) {
/**********************ADMIN MENU************************/
?>
<div class="myaccount"><br>
<a href="admin_cp.php">Admin CP </a><br /><br>
<a href="member_create.php">Add new Member</a><br><br>
<a href="config.php">Site Configuration</a><br><br>
</div>
<?php }
/*****************************END************************/
?>
</td>
<td width="74%" valign="top" style="padding: 10px;">

<h2 class="titlehdr">Your Profile Edit Form</font></h2>
<p>
<?php
if(!empty($err)) {
echo "<div class=\"msg\">";
foreach ($err as $e) {
echo "* $e <br>";
}
echo "</div>";
}
if(!empty($msg)) {
echo "<div class=\"msg\">" . $msg[0] . "</div>";

}
?>
</p>
<p>Hdff sdgsdgs g.</p>

<?php while ($row_edits = mysql_fetch_array($rs_edits)) {?>
<form name="myform" id="myform" method="post" action="">
<table width="80%" border="0" cellpadding="5" cellspacing="2" class="myaccount">
<tr>
<td>Site Title</td>
<td><input name="SITE_TITLE" type="text" id="SITE_TITLE" value="<?php echo $row_edits['SITE_TITLE']; ?>"></td>
</tr>
<tr>
<td>Site Slogan</td>
<td><textarea name="SITE_SLOGAN" cols="30" rows="1" id="SITE_SLOGAN"><?php echo $row_edits['SITE_SLOGAN']; ?></textarea></td>
</tr>
<tr>
<td>Site Keywords</td>
<td><textarea name="SITE_KEYWORDS" cols="30" rows="1" id="SITE_KEYWORDS"><?php echo $row_edits['SITE_KEYWORDS']; ?></textarea>
</td>
</tr>
<tr>
<td>Site Descriptions</td>
<td><textarea name="SITE_DESCRIPTIONS" cols="30" rows="1" id="SITE_DESCRIPTIONS"><?php echo $row_edits['SITE_DESCRIPTIONS']; ?></textarea></td>
</tr>
<tr>
<td>Site Menus</td>
<td><textarea name="SITE_MENUS" cols="30" rows="1" id="SITE_MENUS"><?php echo $row_edits['SITE_MENUS']; ?></textarea></td>
</tr>
<tr>
<td>Site Meta Tags</td>
<td><textarea name="SITE_METATAGS" cols="30" rows="1" id="SITE_METATAGS"><?php echo $row_edits['SITE_METATAGS']; ?></textarea></td>
</tr>
<tr>
<td>Site Copyright</td>
<td><textarea name="SITE_COPYRIGHT" cols="30" rows="1" id="SITE_COPYRIGHTS"><?php echo $row_edits['SITE_COPYRIGHT']; ?></textarea></td>
</tr>
</table>
<p>
<input name="doSave" type="submit" id="doSave" value="Save">
</p>
</form>
<?php } ?>

<td width="196" valign="top">&nbsp;</td>
</tr>
<tr>
<td colspan="3">&nbsp;</td>
</tr>
</table>
</body>
</html>

and i got blank page, when i disabled
<?php while ($row_edits = mysql_fetch_array($rs_edits)) {?> the form appears but can't work fine.

That is seem cause to use GET parameter or SESSION. it just wont work with data[id]

any idea ? thanks in advance

Fou-Lu
06-22-2010, 04:37 PM
The problem is there is simply no ID field specified within your form.
You cannot fetch it from your $data['ID'] though. Heres why. This query:
$rs_edits = mysql_query("select * from users where ID='$data[ID]'") or die(mysql_error()); is asking for $data['ID'] to be specified; however it is not. Upon first load, this page will have no post data, and $data is extracted from post. Unless you have entries in users where id = '', then this loop will have no results: <?php while ($row_edits = mysql_fetch_array($rs_edits)) {?>. This also assumes that this page is not directly posted to.
So, the $rs_edits query needs to come from somewhere else. Given that its on a user table with an ID for it, and given your use of $_SESSION later, I'd say that its probably supposed to be $_SESSION['user_id'] and not $data['ID'] that you want there.
Next, simply add a hidden input to the form that includes ID:


<input type="hidden" name="ID" value="<?php echo $row_edits['ID'];?>" />


Now, given as well that you are using a loop, you may need to look at renaming of these input fields to handle arrays. If its only one entry, a loop is never required. On the otherhand, if its multiple entries you will require the loop, but must change your input naming (and IDs as well on the html, they must be unique) in order to handle arrays. This will change all the updating code as it needs to be replicated for each array result specified by the html form. Submit button and form definition will also need to be handled to allow multiple entries within them.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum