This is a discussion on newbie question within the PHP Language forums, part of the PHP Programming Forums category; Sorry if missed something obvious. I'm trying to write a notebook program that allows me to select a "...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Sorry if missed something obvious. I'm trying to write
a notebook program that allows me to select a "subject" from a pulldown menu and read the corresponding "data" back into a form where I can edit and save it. The MySQL database table looks like, ('id', 'subject, 'data'). Everything works find except for the last part (if post op = update). The program doesn't remember the $_POST[id] although it does remember all the other $_POST variables. Thanks for any hints or suggestions. Max ------------------------------------------------------------- <?php //echo $_POST[op]; $conn=mysql_connect("localhost","root","mypass"); mysql_select_db("it",$conn); $sql="select * from data"; $result=mysql_query($sql,$conn); if ($_POST[op] != "view") { //show the form echo "<form method=\"post\" action=\"$_SERVER[PHP_SELF]\"> <select name=\"id\"><option value=\"\">--select page--</option>"; while ($na = mysql_fetch_array($result)) { echo "<option value=".$na['0'].">".$na['1']."</option>"; } echo "</select>"; echo "<input type=\"submit\" name=\"op\" value=\"view\"></form>"; } if ($_POST[op] == "view") { //get data and put into form. $query="select * from data where id=".$_POST[id].""; $res=mysql_query($query,$conn); $array=mysql_fetch_array($res); echo "<form action=\"$_SERVER[PHP_SELF]\" method=\"post\"> Subject:<br> <input type=\"text\" name=\"subject\" size=\"81\" value=\"".$array[1]."\"><br> Description:<br> <textarea name=\"body\" rows=\"40\" cols=\"80\">".$array[2]."</textarea><br> <input type=\"submit\" name=\"op\" value=\"update\"> </form>"; } if ($_POST[op] == "update") { //insert updated info into table $update = "replace into data values('$_POST[id]','$_POST[subject]','$_POST[body]')"; mysql_query($update) or die(mysql_error()); //echo for testing echo $_POST['subject']."<br>"; echo $_POST['body']."<br>"; echo $_POST[op]."<br>"; echo $_POST[id]; //why is this always 'null'? echo "Database updated."; } ?> |
|
|||
|
maxborn555 wrote:
> Sorry if missed something obvious. I'm trying to write > a notebook program that allows me to select a "subject" > from a pulldown menu and read the corresponding "data" back > into a form where I can edit and save it. > > The MySQL database table looks like, ('id', 'subject, 'data'). > > Everything works find except for the last part (if post op = update). > The program doesn't remember the $_POST[id] although it does remember > all the other $_POST variables. > > Thanks for any hints or suggestions. > > Max > > > ------------------------------------------------------------- > <?php > > //echo $_POST[op]; > $conn=mysql_connect("localhost","root","mypass"); > mysql_select_db("it",$conn); > $sql="select * from data"; > $result=mysql_query($sql,$conn); > > if ($_POST[op] != "view") { > > //show the form > > echo "<form method=\"post\" action=\"$_SERVER[PHP_SELF]\"> > <select name=\"id\"><option value=\"\">--select > page--</option>"; > while ($na = mysql_fetch_array($result)) { > echo "<option value=".$na['0'].">".$na['1']."</option>"; } Either use echo "<option value=".$na[0].">".$na[1]."</option>"; } or echo "<option value=".$na['id'].">".$na['subject']."</option>"; } Steve > echo "</select>"; > echo "<input type=\"submit\" name=\"op\" > value=\"view\"></form>"; > } >[snip] |