Adding edit function to mysql for end user

  • honeyham
  • Novice
  • Novice
  • honeyham
  • Posts: 16

Post 3+ Months Ago

Hi there,

I've successfully created a page with an html form to post to a table in my database. I've also been successful at viewing the contents of the table within the same page. However, when I added a php script and html form to retrieve the data from a specific row and have it appear in an html form, I get stuck there. After adding the script, and uploading the page to the server, the page appears blank. Being a complete newbie with scripting, I had copied and pasted the script from tutorials and customized it and with endless trial and error, still cannot figure this out.

Perhaps it'll be useful to give you a bit more info about my table.
table name=news
6 fields: ID, entrymonth, entrydatenum, entryyear,entrytitle, entrytext (the dates are three separate fields because they bring up specific graphics to represent the dates)
ID is the primary key

The ideal way I'd like this is to have post, view, edit and delete functions all on one or two pages, just like in a forum. At the top, would be the post form, followed by a view of the contents of the table. After each row will be an edit link. Once they click on the edit link, the contents would appear within an html form (either on the same page or to a new page). Clicking 'submit' would update the row data.

Can anyone tell me what is wrong with the following code?

Code: [ Select ]
<HTML>
 <HEAD>
  <TITLE>Add a News Entry</TITLE>
  <link href="assets/css/bullets.css" rel="stylesheet" type="text/css">
<link href="assets/css/alltext.css" rel="stylesheet" type="text/css">
 <style type="text/css">
<!--
.year {
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 0.6em;
    font-weight: bold;
    color: #333333;
    text-decoration: none;
    line-height: 35px;
    float: left;
    margin-right:5;
}
-->
 </style>
 </HEAD>
<BODY>
  <H1>Add News Entry</H1>
  <form method="POST" action="<?php echo($PHP_SELF); ?>">
    <select name="entrymonth">
      <option selected>Select Month</option>
      <option value="Jan">Jan</option>
      <option value="Feb">Feb</option>
      <option value="Mar">Mar</option>
      <option value="Apr">Apr</option>
      <option value="May">May</option>
      <option value="Jun">Jun</option>
      <option value="Jul">Jul</option>
      <option value="Aug">Aug</option>
      <option value="Sep">Sep</option>
      <option value="Oct">Oct</option>
      <option value="Nov">Nov</option>
      <option value="Dec">Dec</option>
    </select>
    <br><br>
    <select name="entrydatenum">
      <option selected>Select Date</option>
      <option value="01">01</option>
      <option value="02">02</option>
      <option value="03">03</option>
      <option value="04">04</option>
      <option value="05">05</option>
      <option value="06">06</option>
      <option value="07">07</option>
      <option value="08">08</option>
      <option value="09">09</option>
      <option value="10">10</option>
      <option value="11">11</option>
      <option value="12">12</option>
      <option value="13">13</option>
      <option value="14">14</option>
      <option value="15">15</option>
      <option value="16">16</option>
      <option value="17">17</option>
      <option value="18">18</option>
      <option value="19">19</option>
      <option value="20">20</option>
      <option value="21">21</option>
      <option value="22">22</option>
      <option value="23">23</option>
      <option value="24">24</option>
      <option value="25">25</option>
      <option value="26">26</option>
      <option value="27">27</option>
      <option value="28">28</option>
      <option value="29">29</option>
      <option value="30">30</option>
      <option value="31">31</option>
    </select><br><br>
    <select name="entryyear">
      <option selected>Select Year</option>
      <option value="2009">2009</option>
      <option value="2010">2010</option>
      <option value="2011">2011</option>
      <option value="2012">2012</option>
      <option value="2013">2013</option>
    </select><br><br>
   <b>Title:</b><br>
   <input type="text" name="entrytitle"><br>
   <b>News Entry:</b><br>
<textarea name="entrytext" cols="60" rows="6"></textarea><br>
  <input type="submit" name="submit" value="Submit">
  </form>
  <?php
  if ($_POST['submit']) {
    mysql_connect("localhost","design1_user","password");
    mysql_select_db("design1");
    $entrymonth=$_POST['entrymonth'];
    $entrydatenum=$_POST['entrydatenum'];
    $entryyear=$_POST['entryyear'];
    $entrytitle=$_POST['entrytitle'];
    $entrytext=$_POST['entrytext'];
    $query ="INSERT INTO news (entrymonth,entrydatenum,entryyear, entrytitle,entrytext)";
    $query.=" VALUES ('$entrymonth','$entrydatenum','$entryyear','$entrytitle','$entrytext')";
    $result=mysql_query($query);
    if ($result) echo "<b>Successfully Posted!</b>";
    else echo "<b>ERROR: unable to post.</b>";
  }
 ?>
 
 
<?
mysql_connect("localhost","design1_user","password");
mysql_select_db("design1");
 
if(!isset($cmd))
{
   $result = mysql_query("select * from news order by ID");
   
   while($r=mysql_fetch_array($result))
   {
 
      $entrymonth=$r["entrymonth"];
      $entrydatenum=$r["entrydatenum"];
      $entryyear=$r["entryyear"];
      $entrytitle=$r["entrytitle"];//take out the title
      $entrytext=$r["entrytext"];
      $IDe=$r["ID"];
     
      echo "<p><span class='$entrymonth'>$entrydatenum</span><span class='year'>$entryyear</span><span class='lefttitles'>$entrytitle</span></p>";
      echo "<p><span class='lefttext'>$entrytext</span></p>";
      echo "<a href='addentry2.php?cmd=edit&ID=$ID'>Edit</a>";
      echo "<br>";
    }
}
?>
 
//The issue seems to begin from here.
 
<?
if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
{
   if (!isset($_POST["submit"]))
   {
      $ID = $_GET["ID"];
      $sql = "SELECT * FROM news WHERE ID=$ID";
      $result = mysql_query($sql);        
      $myrow = mysql_fetch_array($result);
      ?>
     
      <form action="<?php echo($PHP_SELF); ?>" method="post">
      <input type=hidden name="ID" value="<?php echo $myrow["ID"] ?>">
 
 
      Month:<INPUT TYPE="TEXT" NAME="entrymonth" VALUE="<?php echo $myrow["entrymonth"] ?>" SIZE=30><br>
      Date:<INPUT TYPE="TEXT" NAME="entrydatenum" VALUE="<?php echo $myrow["entrydatenum"] ?>"><br>
      Title:<INPUT TYPE="TEXT" NAME="entrytitle" VALUE="<?php echo $myrow["entrytitle"] ?>" SIZE=30><br>
      Text:<TEXTAREA NAME="entrytext" ROWS=10 COLS=30><? echo $myrow["entrytext"] ?></TEXTAREA><br>
 
   
      <input type="hidden" name="cmd" value="edit">
   
      <input type="submit" name="submit" value="submit">
   
      </form>
   
<?
   if ($_POST["$submit"])
   {
      $entrymonth = $_POST["entrymonth"];
      $entrydatenum = $_POST["entrydatenum"];
      $entrytitle = $_POST["entrytitle"];
      $entrytext = $_POST["entrytext"];
     
      $sql = "UPDATE news SET entrymonth,='$entrymonth',entrydatenum,='$entrydatenum',entrytitle='$entrytitle',entrytext='$entrytext', WHERE ID=$ID";
      //replace news with your table name above
      $result = mysql_query($sql);
      echo "Thank you! Information updated.";
    }
}
?>
 
   
 
</body>
 </html>
  1. <HTML>
  2.  <HEAD>
  3.   <TITLE>Add a News Entry</TITLE>
  4.   <link href="assets/css/bullets.css" rel="stylesheet" type="text/css">
  5. <link href="assets/css/alltext.css" rel="stylesheet" type="text/css">
  6.  <style type="text/css">
  7. <!--
  8. .year {
  9.     font-family: Verdana, Arial, Helvetica, sans-serif;
  10.     font-size: 0.6em;
  11.     font-weight: bold;
  12.     color: #333333;
  13.     text-decoration: none;
  14.     line-height: 35px;
  15.     float: left;
  16.     margin-right:5;
  17. }
  18. -->
  19.  </style>
  20.  </HEAD>
  21. <BODY>
  22.   <H1>Add News Entry</H1>
  23.   <form method="POST" action="<?php echo($PHP_SELF); ?>">
  24.     <select name="entrymonth">
  25.       <option selected>Select Month</option>
  26.       <option value="Jan">Jan</option>
  27.       <option value="Feb">Feb</option>
  28.       <option value="Mar">Mar</option>
  29.       <option value="Apr">Apr</option>
  30.       <option value="May">May</option>
  31.       <option value="Jun">Jun</option>
  32.       <option value="Jul">Jul</option>
  33.       <option value="Aug">Aug</option>
  34.       <option value="Sep">Sep</option>
  35.       <option value="Oct">Oct</option>
  36.       <option value="Nov">Nov</option>
  37.       <option value="Dec">Dec</option>
  38.     </select>
  39.     <br><br>
  40.     <select name="entrydatenum">
  41.       <option selected>Select Date</option>
  42.       <option value="01">01</option>
  43.       <option value="02">02</option>
  44.       <option value="03">03</option>
  45.       <option value="04">04</option>
  46.       <option value="05">05</option>
  47.       <option value="06">06</option>
  48.       <option value="07">07</option>
  49.       <option value="08">08</option>
  50.       <option value="09">09</option>
  51.       <option value="10">10</option>
  52.       <option value="11">11</option>
  53.       <option value="12">12</option>
  54.       <option value="13">13</option>
  55.       <option value="14">14</option>
  56.       <option value="15">15</option>
  57.       <option value="16">16</option>
  58.       <option value="17">17</option>
  59.       <option value="18">18</option>
  60.       <option value="19">19</option>
  61.       <option value="20">20</option>
  62.       <option value="21">21</option>
  63.       <option value="22">22</option>
  64.       <option value="23">23</option>
  65.       <option value="24">24</option>
  66.       <option value="25">25</option>
  67.       <option value="26">26</option>
  68.       <option value="27">27</option>
  69.       <option value="28">28</option>
  70.       <option value="29">29</option>
  71.       <option value="30">30</option>
  72.       <option value="31">31</option>
  73.     </select><br><br>
  74.     <select name="entryyear">
  75.       <option selected>Select Year</option>
  76.       <option value="2009">2009</option>
  77.       <option value="2010">2010</option>
  78.       <option value="2011">2011</option>
  79.       <option value="2012">2012</option>
  80.       <option value="2013">2013</option>
  81.     </select><br><br>
  82.    <b>Title:</b><br>
  83.    <input type="text" name="entrytitle"><br>
  84.    <b>News Entry:</b><br>
  85. <textarea name="entrytext" cols="60" rows="6"></textarea><br>
  86.   <input type="submit" name="submit" value="Submit">
  87.   </form>
  88.   <?php
  89.   if ($_POST['submit']) {
  90.     mysql_connect("localhost","design1_user","password");
  91.     mysql_select_db("design1");
  92.     $entrymonth=$_POST['entrymonth'];
  93.     $entrydatenum=$_POST['entrydatenum'];
  94.     $entryyear=$_POST['entryyear'];
  95.     $entrytitle=$_POST['entrytitle'];
  96.     $entrytext=$_POST['entrytext'];
  97.     $query ="INSERT INTO news (entrymonth,entrydatenum,entryyear, entrytitle,entrytext)";
  98.     $query.=" VALUES ('$entrymonth','$entrydatenum','$entryyear','$entrytitle','$entrytext')";
  99.     $result=mysql_query($query);
  100.     if ($result) echo "<b>Successfully Posted!</b>";
  101.     else echo "<b>ERROR: unable to post.</b>";
  102.   }
  103.  ?>
  104.  
  105.  
  106. <?
  107. mysql_connect("localhost","design1_user","password");
  108. mysql_select_db("design1");
  109.  
  110. if(!isset($cmd))
  111. {
  112.    $result = mysql_query("select * from news order by ID");
  113.    
  114.    while($r=mysql_fetch_array($result))
  115.    {
  116.  
  117.       $entrymonth=$r["entrymonth"];
  118.       $entrydatenum=$r["entrydatenum"];
  119.       $entryyear=$r["entryyear"];
  120.       $entrytitle=$r["entrytitle"];//take out the title
  121.       $entrytext=$r["entrytext"];
  122.       $IDe=$r["ID"];
  123.      
  124.       echo "<p><span class='$entrymonth'>$entrydatenum</span><span class='year'>$entryyear</span><span class='lefttitles'>$entrytitle</span></p>";
  125.       echo "<p><span class='lefttext'>$entrytext</span></p>";
  126.       echo "<a href='addentry2.php?cmd=edit&ID=$ID'>Edit</a>";
  127.       echo "<br>";
  128.     }
  129. }
  130. ?>
  131.  
  132. //The issue seems to begin from here.
  133.  
  134. <?
  135. if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
  136. {
  137.    if (!isset($_POST["submit"]))
  138.    {
  139.       $ID = $_GET["ID"];
  140.       $sql = "SELECT * FROM news WHERE ID=$ID";
  141.       $result = mysql_query($sql);        
  142.       $myrow = mysql_fetch_array($result);
  143.       ?>
  144.      
  145.       <form action="<?php echo($PHP_SELF); ?>" method="post">
  146.       <input type=hidden name="ID" value="<?php echo $myrow["ID"] ?>">
  147.  
  148.  
  149.       Month:<INPUT TYPE="TEXT" NAME="entrymonth" VALUE="<?php echo $myrow["entrymonth"] ?>" SIZE=30><br>
  150.       Date:<INPUT TYPE="TEXT" NAME="entrydatenum" VALUE="<?php echo $myrow["entrydatenum"] ?>"><br>
  151.       Title:<INPUT TYPE="TEXT" NAME="entrytitle" VALUE="<?php echo $myrow["entrytitle"] ?>" SIZE=30><br>
  152.       Text:<TEXTAREA NAME="entrytext" ROWS=10 COLS=30><? echo $myrow["entrytext"] ?></TEXTAREA><br>
  153.  
  154.    
  155.       <input type="hidden" name="cmd" value="edit">
  156.    
  157.       <input type="submit" name="submit" value="submit">
  158.    
  159.       </form>
  160.    
  161. <?
  162.    if ($_POST["$submit"])
  163.    {
  164.       $entrymonth = $_POST["entrymonth"];
  165.       $entrydatenum = $_POST["entrydatenum"];
  166.       $entrytitle = $_POST["entrytitle"];
  167.       $entrytext = $_POST["entrytext"];
  168.      
  169.       $sql = "UPDATE news SET entrymonth,='$entrymonth',entrydatenum,='$entrydatenum',entrytitle='$entrytitle',entrytext='$entrytext', WHERE ID=$ID";
  170.       //replace news with your table name above
  171.       $result = mysql_query($sql);
  172.       echo "Thank you! Information updated.";
  173.     }
  174. }
  175. ?>
  176.  
  177.    
  178.  
  179. </body>
  180.  </html>
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • honeyham
  • Novice
  • Novice
  • honeyham
  • Posts: 16

Post 3+ Months Ago

I was able to solve part of my own problem. It turned out I had one typo ($IDe) should have been ($ID) and the code I had copied and pasted was missing two "}" to close the script. With those fixed, I was able to click "edit" under one of the entries, which would then retrieve the mysql row data and fill the html form.

Now, my next problem is submitting the form for update. Clicking the 'submit' button clears the form, but does not update the row at all. Here is the section of code I have for updating the row by clicking 'submit' on the form. Can anyone tell me what is wrong with it?:

Code: [ Select ]
<?
if ($_POST["$submit"])
{
     $entrymonth = $_POST["entrymonth"];
     $entrydatenum = $_POST["entrydatenum"];
     $entrytitle = $_POST["entrytitle"];
     $entrytext = $_POST["entrytext"];
    
     $sql = "UPDATE INTO news SET entrymonth=$entrymonth,entrydatenum=$entrydatenum,entrytitle=$entrytitle,entrytext=$entrytext WHERE ID=$ID";
     $result = mysql_query($sql);
     echo "Thank you! Information updated.";
    
    }

?>
  1. <?
  2. if ($_POST["$submit"])
  3. {
  4.      $entrymonth = $_POST["entrymonth"];
  5.      $entrydatenum = $_POST["entrydatenum"];
  6.      $entrytitle = $_POST["entrytitle"];
  7.      $entrytext = $_POST["entrytext"];
  8.     
  9.      $sql = "UPDATE INTO news SET entrymonth=$entrymonth,entrydatenum=$entrydatenum,entrytitle=$entrytitle,entrytext=$entrytext WHERE ID=$ID";
  10.      $result = mysql_query($sql);
  11.      echo "Thank you! Information updated.";
  12.     
  13.     }
  14. ?>
  • honeyham
  • Novice
  • Novice
  • honeyham
  • Posts: 16

Post 3+ Months Ago

Okay, I've found the solution to this problem too.

I changed
Code: [ Select ]
 if ($_POST["$submit"])

to
Code: [ Select ]
 if ($_POST["submit"])


and added
Code: [ Select ]
$ID=$_POST['ID'];

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 108 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.