surochek Posted July 3, 2007 Share Posted July 3, 2007 The form works great... up to a point. It submits and updates the data into the MySQL database. but it doesn't get the data from the db to display it in the form. I have the sinking feeling it's something really stupid that's staring me in the face, unless it's something I haven't learned yet. Here's the code: <?php //both checked and working fine session_start(); include("connect.php"); ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title></title> <link rel="stylesheet" type="text/css" href="builder2.css" /> </head> <body> <div id="wrapper"> <div class="header"> <!--header content--> </div> <br /> <br /> <div class="left"> <?php //navigation links -- work and include a logout include_once("left.inc"); ?> </div> <div class="content"> <?php $user=$_SESSION['username']; $sql = "SELECT * FROM traits WHERE username = '$user'"; $query = mysql_query($sql) or die (mysql_error(). " with query : ". $query."<br>"); $result = mysql_num_rows($query); if ($result ==0) {require("basictraits.php"); //the html + php form if (isset($_POST['submit'])) { $user=$_SESSION['username']; $indate=date("Y:m:d"); $title=$_POST['title']; $genre=$_POST['genre']; //more variables in full script //db query $query = "INSERT INTO traits (indate, username, title, genre)VALUES('$indate', '$user', '$title', '$genre')"; //execute db update $update=mysql_query($query) or die("<br>Error: ". mysql_error(). " with query : ". $query); echo "<span style='color:#330099;'><h3 align='center'>Data saved!</h3></span>";}} else { $title=$info['title']; $genre=$info['genre']; //and the rest of the variables if (isset($_POST['submit'])) { $title=$_POST['title']; $genre=$_POST['genre']; //and so on $query="UPDATE traits SET title='$title', genre='$genre' WHERE username = '$user'"; $update=mysql_query($query) or die("<br>Error: ". mysql_error(). " with query : ". $query); echo "<span style='color:#330099;'><h3 align='center'>Form updaded successfully.</h3></span>";} $title=$info['title']; $genre=$info['genre']; //etc //query db to display info in form $query = "SELECT * FROM traits WHERE username='$user'"; $result= mysql_query($query); $info=mysql_fetch_array($result, MYSQL_ASSOC) or die (mysql_error(). " with query : ". $query."<br>"); } //display form include("basictraits.php"); ?> </table> </div> <br /> <br /> <br /> </div> </body> </html> Here's the code for the form: (include()) <form action="charactertraits2.php" method="post"> <table align="center"> <table border="0" cellpadding="3" cellspacing="3" summary="List of Essential Character Traits"> <tr><td align="right"><b>AUTHOR (Username):</b></td> <td><textarea rows="1" cols="40" name="user"><?php echo $_SESSION['username']?></textarea></td></tr> <tr><td align="right"><b>TITLE:</b></td> <td><textarea rows="1" cols="40" name="title"><?php echo $_POST['title']?></textarea></td></tr> <tr><td align="right">Genre:</td><td><textarea rows="1" cols="40" name="character_name"><?php echo $_POST['genre']?></textarea></td></tr> <tr><td align="right">Role:</td><td><select name="role" size="1"> <option value="Protagonist" name="Protagonist">Protagonist</option> <option value="Contagonist" name="Contagonist">Contagonist</option> <option value="Antagonist" name="Antagonist">Antagonist (Villain)</option> <option value="Mentor" name="Mentor">Mentor</option> <option value="Logic" name="Logic">Logic (Reason)</option> <option value="Emotion" name="Emotion">Emotion (Heart)</option> <option value="Sidekick" name="Sidekick">Sidekick (Blind Supporter)</option> <option value="Skeptic" name="Skeptic">Skeptic (Naysayer)</option> </select></td></tr> <tfoot><td> </td><td colspan="3"><center> <?php echo "<input type='submit' name='submit'>"; echo "</form>"; ?> So how can I have a form that submits to or updates the db, AND displays the data to the form? Quote Link to comment https://forums.phpfreaks.com/topic/58211-am-i-illogical-form-problems/ Share on other sites More sharing options...
KevinM1 Posted July 3, 2007 Share Posted July 3, 2007 When, exactly, do you want the form to display the user submitted info? Because if you have a correct submission (meaning, all info has been entered and entered correctly), at best you'll only display the session value with your current setup. Quote Link to comment https://forums.phpfreaks.com/topic/58211-am-i-illogical-form-problems/#findComment-288653 Share on other sites More sharing options...
surochek Posted July 3, 2007 Author Share Posted July 3, 2007 That's what I discovered. If there is nothing yet in the db for a given user (username=session), I need to enter the info and then see what I have in the db. If I already have a row for that user, I need to get the information from the database onto the form, update it if I want (i.e. edit text in the form), submit it, and the see the new info as pulled from the db. Quote Link to comment https://forums.phpfreaks.com/topic/58211-am-i-illogical-form-problems/#findComment-288928 Share on other sites More sharing options...
sasa Posted July 3, 2007 Share Posted July 3, 2007 move lines $title=$info['title']; $genre=$info['genre']; //etc after line $info=mysql_fetch_array($result, MYSQL_ASSOC) or die (mysql_error(). " with query : ". $query."<br>"); and change form echo $_POST['title']?></textarea></td></tr> <tr><td align="right">Genre:</td><td><textarea rows="1" cols="40" name="character_name"><?php echo $_POST['genre']?></textarea></td></tr> to echo $title;?></textarea></td></tr> <tr><td align="right">Genre:</td><td><textarea rows="1" cols="40" name="character_name"><?php echo $genre;?></textarea></td></tr> Quote Link to comment https://forums.phpfreaks.com/topic/58211-am-i-illogical-form-problems/#findComment-288990 Share on other sites More sharing options...
surochek Posted July 4, 2007 Author Share Posted July 4, 2007 Nope. Now the nothing goes into the db. Quote Link to comment https://forums.phpfreaks.com/topic/58211-am-i-illogical-form-problems/#findComment-289265 Share on other sites More sharing options...
teng84 Posted July 4, 2007 Share Posted July 4, 2007 can you tell us what you really want from that code? Quote Link to comment https://forums.phpfreaks.com/topic/58211-am-i-illogical-form-problems/#findComment-289269 Share on other sites More sharing options...
surochek Posted July 4, 2007 Author Share Posted July 4, 2007 To allow registered users to fill out a form, save their info to a db, and see the info on the form. If the user fills out the form for the first time, INSERT into the db. Otherwise, UPDATE the db. In both cases, show the results after the changes are made so the user can modify the info at will. Quote Link to comment https://forums.phpfreaks.com/topic/58211-am-i-illogical-form-problems/#findComment-289283 Share on other sites More sharing options...
teng84 Posted July 4, 2007 Share Posted July 4, 2007 use php_self action="<? $_SERVER['PHP_SELF']?>" tehn put the display above the form Quote Link to comment https://forums.phpfreaks.com/topic/58211-am-i-illogical-form-problems/#findComment-289287 Share on other sites More sharing options...
surochek Posted July 4, 2007 Author Share Posted July 4, 2007 put the display above the form? How so? Quote Link to comment https://forums.phpfreaks.com/topic/58211-am-i-illogical-form-problems/#findComment-289291 Share on other sites More sharing options...
teng84 Posted July 4, 2007 Share Posted July 4, 2007 <?php //now extract file from db the eco it the way you want //sample some select select * from ewan where id = $_POST[''] ?> <html> </head> <body> <br><form action="<?=$_SERVER['PHP_SELF']?>" method="post" > <input name="x" type="text" /> <input name="hit_player_2" type="submit" value="submit"> </form> </body> Quote Link to comment https://forums.phpfreaks.com/topic/58211-am-i-illogical-form-problems/#findComment-289297 Share on other sites More sharing options...
surochek Posted July 4, 2007 Author Share Posted July 4, 2007 calling <? $_SERVER['PHP_SELF']?> or the file's own name makes no difference. Besides, <? $_SERVER['PHP_SELF']?> doesn't work with the GoDaddy set up (they run php as CGI). I'm not entirely sure I have it set up on my hard drive the right way, either. Quote Link to comment https://forums.phpfreaks.com/topic/58211-am-i-illogical-form-problems/#findComment-289298 Share on other sites More sharing options...
teng84 Posted July 4, 2007 Share Posted July 4, 2007 ok then use action="put the page"// you can put there the page itself and that will be the same Quote Link to comment https://forums.phpfreaks.com/topic/58211-am-i-illogical-form-problems/#findComment-289302 Share on other sites More sharing options...
teng84 Posted July 4, 2007 Share Posted July 4, 2007 or if it still doesnt work the have this <form method="post" name="FormName" target="_self" > that will surely work Quote Link to comment https://forums.phpfreaks.com/topic/58211-am-i-illogical-form-problems/#findComment-289304 Share on other sites More sharing options...
surochek Posted July 4, 2007 Author Share Posted July 4, 2007 The action="pageurl" works. However, the form still wont pull the info from the db at login. ??? argh Quote Link to comment https://forums.phpfreaks.com/topic/58211-am-i-illogical-form-problems/#findComment-289315 Share on other sites More sharing options...
teng84 Posted July 4, 2007 Share Posted July 4, 2007 are u using session here Quote Link to comment https://forums.phpfreaks.com/topic/58211-am-i-illogical-form-problems/#findComment-289339 Share on other sites More sharing options...
eagleweb Posted July 4, 2007 Share Posted July 4, 2007 Try this for each form field. I use it regularly: <textarea rows="1" cols="40" name="title"><?php if(empty['$_POST'] && !empty($row['title'])) {echo $row['title'];} else {echo $_POST['title']; }?></textarea> I did not pay attention to the rest of your code, so you might have to change the !empty($row['title']) good luck Quote Link to comment https://forums.phpfreaks.com/topic/58211-am-i-illogical-form-problems/#findComment-289351 Share on other sites More sharing options...
surochek Posted July 4, 2007 Author Share Posted July 4, 2007 No, that didn't help. It still doesn't pull data from the db. And I don't have problems connecting to the db, because I can submit and update. Quote Link to comment https://forums.phpfreaks.com/topic/58211-am-i-illogical-form-problems/#findComment-289737 Share on other sites More sharing options...
surochek Posted July 4, 2007 Author Share Posted July 4, 2007 Correction: <textarea rows="1" cols="40" name="title"><?php if(empty($_POST) && !empty($info['title'])) {echo $info['title'];} else {echo $_POST['title']; }?></textarea> Note that I had to change: if(empty['$_POST']) to if(empty($_POST)) and adjust the variable $row to my variable $info (took me a minute, and a duh moment while I took care of garbage, how fitting is that?) It's beautiful, it makes perfect sense, I'm off to play with my shiny new form, but is there a way to do this once instead of repeating it in every field of the table? Quote Link to comment https://forums.phpfreaks.com/topic/58211-am-i-illogical-form-problems/#findComment-289743 Share on other sites More sharing options...
surochek Posted July 4, 2007 Author Share Posted July 4, 2007 And after testing, I'm back to the same frustrating point. If I log in (and create a session) for a user that already exists (i.e. there is a row in the table for the form with that username=session), everything is fine. But if it's a new user, everything falls apart. I keep running around in circles. ARGH :'( Quote Link to comment https://forums.phpfreaks.com/topic/58211-am-i-illogical-form-problems/#findComment-289825 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.