Jump to content

Am I illogical? -- Form problems


surochek

Recommended Posts

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?

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<?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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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  :'(

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.