All In One - PHP/MYSQL - Add/Edit/Delete Script

I have been working on a script to allow for events to be updated.

What I have been attempting to do is to include all of the abilities in one file by submitting the form contents to itself and using hidden fields process the data accordingly.

Unfortunately it seems this is not going to happen. If someone knows how please enlighten me.

Currently, I am attempting to incorporate the edit_day.php script to include both the actual form that brings up the info out of the database based on a variable $id that is passed to the page ( i.e. http://test.com/edit_day.php?id=3 ).

As it seems I am not able to run both a wildcard SQL statement and then an UPDATE statement afterwards. I am guessing this is because that you can only execute one query per page?

I figured that this would work since the form is being submitted and that would clear the memory of the first query. So it seems that I am at the point where I am not sure if I am going to have to create a page for each action - add/edit/delete. Obviously this takes a lot of pages so I am try to get around it.

All help is appreciated and code is attached.

Mike

<?

// Define MYSQL Connection
define (‘MYSQL_HOST’, ‘localhost’);
define (‘MYSQL_USER’, ‘user’);
define (‘MYSQL_PASS’, ‘pass’);
define (‘MYSQL_DB’, ‘boy’);

// Try to connect to the DB
if (! mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS) )
{
echo ‘Failed to connect to: “On This Day Database.”’;
exit;
}

// Select the appropriate db
mysql_select_db(MYSQL_DB);

// Define date variables
$m=date(“F”);
$d=date(“d”);

// Check to see if form was self-submitted
if ($HTTP_POST_VARS[‘do’]==‘submit’)
{
submit_edit($HTTP_POST_VARS);
}
else
{
// Add the passed data (if any) to fill the form
db_filldata($id);
show_edit_form($entry);
}

function show_edit_form($entry)
{
$id = $entry[0];
$month = $entry[1];
$day = $entry[2];
$year = $entry[3];
$content = $entry[4];
$link = $entry[5];
$date_added = $entry[6];
?>

<b>
<form method=“POST” action=‘<? echo $PHP_SELF ?>’ name=“edit_form”>
<input type=“hidden” name=“do” value=“submit”>

Entry ID: <? echo $id ?><br>
Date Entered: <? echo date(“F d, Y - h:iA”, $date_added) ?><p>
</b>

Month:<br>
<select name=“month” value=“<? echo $month ?>”>
<option value=“January”<? if ($month==“January”) { echo " selected"; } ?>>January</option>
<option value=“February”<? if ($month==“February”) { echo " selected"; } ?>>February</option>
<option value=“March”<? if ($month==“March”) { echo " selected"; } ?>>March</option>
<option value=“April”<? if ($month==“April”) { echo " selected"; } ?>>April</option>
<option value=“May”<? if ($month==“May”) { echo " selected"; } ?>>May</option>
<option value=“June”<? if ($month==“June”) { echo " selected"; } ?>>June</option>
<option value=“July”<? if ($month==“July”) { echo " selected"; } ?>>July</option>
<option value=“August”<? if ($month==“August”) { echo " selected"; } ?>>August</option>
<option value=“September”<? if ($month==“September”) { echo " selected"; } ?>>September</option>
<option value=“October”<? if ($month==“October”) { echo " selected"; } ?>>October</option>
<option value=“November”<? if ($month==“November”) { echo " selected"; } ?>>November</option>
<option value=“December”<? if ($month==“December”) { echo " selected"; } ?>>December</option>
</select>
<p>
Day:<br>
<select name=“day”>
<?
$i=1;
while ($i <= 31)
{
if ($i==$day)
{
echo “<option value=\”$i\" selected>$i</option>
“;
}
else
{
echo “<option value=\”$i\”>$i</option>
";
}
$i++;
}
?>
</select>
<p>
Year:<br>
<input type=“text” name=“year” onkeyup=“this.value=this.value.replace(/\D/g,‘’)”

onchange=“this.value=this.value.replace(/\D/g,‘’)” maxlength=“4” size=“2” value=“<? echo $year ?>”>
<p>
On This Day Entry:<br>
<textarea rows=“10” cols=“30” name=“content”>
<? echo $content ?>
</textarea>
<p>
Link:<br>
<input type=“text” name=“link” value=“<? echo $link ?>”>
<p>
<input type=“submit” value=“Save Changes” name=“submit”>
  
<input type=“reset” value=“Clear Info” name=“reset”>
</form>
<?
}

function db_filldata($value)
{
// Send SQL query
$sql = “SELECT *, UNIX_TIMESTAMP(date_added) AS date_added FROM on_this_day WHERE id = '” . $value . “'”;
$result = mysql_query($sql);

while ($line = mysql_fetch_array($result)) {
$id=$line[‘id’];
$month=$line[‘month’];
$day=$line[‘day’];
$year=$line[‘year’];
$content=$line[‘content’];
$link=$line[‘link’];
$date_added=$line[‘date_added’];

$entry=array($id,$month,$day,$year,$content,$link,$date_added);
$GLOBALS[‘entry’] = $entry;
}
}

function submit_edit($variables)
{
mysql_query(“UPDATE on_this_day SET month=‘{$variables[‘month’]}’ WHERE id=‘{$variables[‘id’]}’”);
echo "<font size=‘4’ face=‘arial’><b>The entry has been updated.</b></font><p>
";
echo "<a href=‘view_day.php’ alt=‘View Entries’>View Entries</a>
";
echo "  
";
echo “<a href=‘add_day.php’ alt=‘Add Entry’>Add Entry</a>”;
}
?>

you can include all the possible things like, delete, update into one form and on submit can workout with them. What I did is, when you have such a situation and submitting it to the same form, you can add simple ‘Button’ instead of ‘Submit’. Then, the user clicks on a button which would redirect him to the same page, you can set the conditions as per the button he has clicked, say :
<input type=button name=insert>
<input type=button name=delete>
<input type=button name=update>

Now, on top of the page, you can check for the button which has been clicked,
if(isset($insert)){
conditions with queries you want}
else if(isset($delete)){
…}
and so on.

I hope that can help you