Edit Form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Edit Form

Post by Jim_Bo »

Hey,

Trying to create an update news form .. its parsing thru saying it was successfull .. but the record hasnt been updated at all

editnews.php:

Code: Select all

<?php

include 'db.php';

define('MAX_YEARS', 5);

$sql = mysql_query("SELECT * FROM teachers_news WHERE id='&#123;$_GET&#1111;'id']&#125;'"); 

while($row = mysql_fetch_array($sql))&#123; 
$id = $row&#1111;'id'];
$news = $row&#1111;'$news'];
$date = $row&#1111;'date'];

?>

<form action="../index.php?pages=editnewsparse" method="post">
<input type="hidden" name="id" value="<?php echo $row&#1111;'id']; ?>"> 
<table width="400" border="0" align="center" cellpadding="4" cellspacing="0">
  <tr>
    <td width="100" align="right"><font class="txt">Date:</font></td>
    <td width="278" align="left">
      <select name="day" id="day" class="input-box">

<?php

$currentDay = date('j');
for ($day = 1; $day <= 31; $day++) &#123;
  echo "<option";
  if ($currentDay == $day) echo " SELECTED";
  echo ">$day</option>\n";
&#125;

?>

</select> <select name="month" id="month" class="input-box">

<?php

$currentMonth = date('n');
for ($month = 1; $month <= 12; $month++) &#123;
  echo "<option";
  if ($currentMonth == $month) echo " SELECTED";
  echo ">$month</option>\n";
&#125;

?>

      </select>
      <select name="year" id="year" class="input-box">

<?php

$currentYear = date("Y");
for ($year = $currentYear; $year <= ($currentYear+MAX_YEARS-1); $year++) &#123;
  echo "<option";
  if ($currentYear == $year) echo " SELECTED";
  echo ">$year</option>\n";
&#125;

?>

      </select>

<font class="txt">dd-mm-yyyy</font></td>
  
</tr>
  <tr>
    <td align="right"><font class="txt">News:</font></td>
    <td align="left"> <textarea name="news" cols="30" rows="5" id="description" class="input-box"><?php echo $row&#1111;'news']; ?></textarea>
    </td>
  </tr>
  <tr align="center">
    <td colspan="2"> <input type="submit" name="Submit" value="Submit" class="submit-button">
      
    </td>
  </tr>
</table>
</form>

<?php
&#125;
?>
editnewsparse.php:

Code: Select all

<?php

include 'db.php';

define('MAX_YEARS', 5);

// Define post fields into simple variables
$id = (int)$_GET&#1111;'id'];
$news = (int)$_GET&#1111;'news'];
$year  = (int)$_GET&#1111;'year'];
$month = (int)$_GET&#1111;'month'];
$day   = (int)$_GET&#1111;'day'];

$currentYear = date('Y');

if ($year >= $currentYear && $year < $currentYear + MAX_YEARS && checkdate($month, $day, $year)) &#123;
    $Y = sprintf('%04s', $year);
    $m = sprintf('%02s', $month);
    $d = sprintf('%02s', $day);
    $date = "$Y-$m-$d";
&#125;


	
 // Enter info into the Database.

$sql = "UPDATE teachers_news SET date = '$date', news = '$news' WHERE id = '$id'";


if(!$sql)&#123;
	
	echo ("<center><font class="txt"There has been an error inserting the data. Please contact the webmaster.</font></center>");

&#125; else &#123;
	
	echo ("<center><font class="txt">Sucessfully Updated</font></center><br><br>");

	echo ("<center><font class="txt"><a href="../index.php?pages=teachers_news">BACK</a></font></center><br>"); 

&#125;

?>
Any ideas why its not updating?

Thanks
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

// Enter info into the Database. 

$sql = "UPDATE teachers_news SET date = '$date', news = '$news' WHERE id = '$id'"; 

$result = mysql_query($sql) or die(mysql_error());

if(!$result)&#123; 
    
   echo ("<center><font class="txt"There has been an error inserting the data. Please contact the webmaster.</font></center>"); 

&#125; else &#123; 
    
   echo ("<center><font class="txt">Sucessfully Updated</font></center><br><br>"); 

   echo ("<center><font class="txt"><a href="../index.php?pages=teachers_news">BACK</a></font></center><br>"); 

&#125;
You never execute your query, therefor, it is never FALSE.

php.net/mysql_query
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

Hey,

Arrg good point .. except with the changes it stills show the success statement .. but the record still isnt updated with the changes ..

Anything else lok out of place?

Thanks
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

If you are not getting any errors then your query is valid. Make sure your input is correct ?

Try outputting the query before you execute it to make sure it is what you want.
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

Hey

Maybe its not parsing the the data from editnews.php to editnewsparse.php?

Im not really sure how to add error checking etc :(
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

ahh stupid miss by me.

change

Code: Select all

// Define post fields into simple variables 
$id = (int)$_GET&#1111;'id']; 
$news = (int)$_GET&#1111;'news']; 
$year  = (int)$_GET&#1111;'year']; 
$month = (int)$_GET&#1111;'month']; 
$day   = (int)$_GET&#1111;'day'];
to

Code: Select all

// Define post fields into simple variables 
$id = $_POST&#1111;'id']; 
$news = $_POST&#1111;'news']; 
$year  = $_POST&#1111;'year']; 
$month = $_POST&#1111;'month']; 
$day   = $_POST&#1111;'day'];
The problem was your checking for $_GET variables while your form method is POST. I would always use the post method because $_GET does not have a very long char limit.
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

Hey,

Omg, I thought it was $_GET data from the posted form :(

Thanks $_POST sorted it ..

Can anyone suggest a good php/mysql book that gives tutorials on the general basis of what people would use in web design .. ie add/edit/delete, functions, sql seacrh queries etc etc ..

I try to learn of the net .. but so many tutorials have different styles to acheive the same thing .. I think learning would be easyer to follow one style of coding then venture to better ways after that ..


Another question .. when I edit a record .. the selected date is the server "todays" date .. not the date that is in the database for the record being edited .. what needs to change to have it select the date in the database within the drop down menus?

Cheers
Last edited by Jim_Bo on Wed Feb 09, 2005 5:54 pm, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

when it doubt

http://dev.mysql.com/doc/mysql/en/index.html

I'm sure a quick google/search of these forums will solve your problem
Last edited by John Cartwright on Wed Feb 09, 2005 5:54 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

anything O'Reilly. :)
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

Jim_Bo wrote:
Another question .. when I edit a record .. the selected date is the server "todays" date .. not the date that is in the database for the record being edited .. what needs to change to have it select the date in the database within the drop down menus?
Cheers
Post Reply