Jump to content

Multitasking form


surochek

Recommended Posts

I would like to have a form that pulls info from a database, displays it in the form fields, and allows the user to update the info.

 

Or if there is no info, allows the user to insert the data into the database while keeping the info displayed in the fields. All of it in one self-submitting page.

 

Is it even possible? I would love to find a good tutorial for this. Even if I have to buy a book.

 

All I've been able to do so far, is set up a "while" loop that hides the form when there's no data to display. Argh. See the newbie running around in circles.

 

PS: I can post some code when I get home. For now, a theoretical answer will do.

 

Link to comment
Share on other sites

There really is no need to overcomplicate the process with Ajax. Its relativly straight forward php.

 

If the user is to update infomation, you would need to pass an id (relating to the infomation they are to update) through the url. If this is set, they'll be updating, if not they'll be adding new data. eg;

 

<?php

  if (isset($_POST['action'])) {
    $id = isset($_POST['id']) ? mysql_real_escape_string($_POST['id']) : '';
    $data = isset($_POST['data']) ? mysql_real_escape_string($_POST['data']) : '';
    switch ($_POST['action']) {
      case 'edit':
        $sql = "UPDATE tbl SET data = '$data' WHERE id = '$id'";
        break;
      case 'add':
        $sql = "INSERT INTO tbl (fld) VALUES ('$data')";
        break;
    }
    if (mysql_query($sql)) {
      echo "Data {$_POST['action']}ed succesfully";
    }
  }
      
  if (isset($_GET['id'])) {
    $sql = "SELECT data FROM fld WHERE id = $id";
    if ($result = mysql_query($result) && mysql_num_rows($result)) {
      $row = mysql_fetch_assoc($result);
      echo "<form method='post'>";
      echo "  <input type='text' name='data' value='{$row['data']}'>";
      echo "  <input type='hidden' name='id' value='{$_GET['id']}'>";
      echo "  <input type='hidden' name='action' value='edit'>";
      echo "  <input type='submit' name='submit'>";
      echo "</form>";
    }
  } else {
    echo "<form method='post'>";
    echo "  <input type='text' name='data'>";
    echo "  <input type='hidden' name='id'>";
    echo "  <input type='hidden' name='action' value='add'>";
    echo "  <input type='submit' name='submit'>";
    echo "</form>";
  }

?>

Link to comment
Share on other sites

Emerging from a space-time warp of motherhood and "other" work -- I think I understand. However, can the code be altered so that it can work with a session? Users are logging in, and I'm using sessions to allow their username to follow them from form to form. Seemed so simple and logical at the time!

Link to comment
Share on other sites

As I was saying, it all looked beautiful until I started playing with it and tried to add a $_SESSION[] variable. Can I make it work that way? Or should it be working already and I should be working at the login script to begin with?

Link to comment
Share on other sites

The form is beautiful. At least until I go through the login process and the session actually gets registered. Then the form disappears.

Here's what I'm working with:

What am I doing wrong??

 

<?php
session_start();
include("connect.inc");

if (isset($_POST['action'])) {
	$username = isset($_POST['username']) ? mysql_real_escape_string($_POST['username']) : '';
	$title = isset($_POST['title']) ? mysql_real_escape_string($_POST['title']) : '';
	$genre = isset($_POST['genre']) ? mysql_real_escape_string($_POST['genre']) : '';
	$character_name = isset($_POST['character_name']) ? mysql_real_escape_string($_POST['character_name']) : '';
	//and so forth -- the variables for the form go on in the full script;

    switch ($_POST['action']) {
      case 'edit':
        $sql = "UPDATE traits SET ('title', 'genre','character_name') = ('$username','$title', '$genre','$character_name') WHERE username = $_SESSION[$username]";
        break;
      case 'add':
        $sql = "INSERT INTO traits ('title', 'genre','character_name') VALUES ('$title', '$genre','$character_name')";
        break;
    }
    if (mysql_query($sql)) {
      echo "Data {$_POST['action']}ed succesfully";
    }
  }

if (isset($_SESSION['username'])) {
    $sql = "SELECT * FROM traits WHERE username = $username";
    if ($result = mysql_query($result) && mysql_num_rows($result)) {
      $row = mysql_fetch_assoc($result);
      echo "<form 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><input type="text" size="53" name="username" value=<?php echo "'{$row['username']}'";?> ></td></tr>

<tr><td align="right"><b>TITLE:</b></td><td><input type="text" size="53" name="title" value=<?php "'{$row['title']}'";?> ></td></tr>

<tr><td align="right"><b>CHARACTER:</b></td><td><input type="text" size="53" name="character_name" value=<?php 
echo "'{$row['character_name']}'";?> ></td></tr>

<!-- and so forth; the form goes on-->

<tfoot><td> </td><td colspan="3"><center><?php 
      echo "  <input type='hidden' name='username' value='{$_SESSION['username']}'>";
      echo "  <input type='hidden' name='action' value='edit'>";
      echo "  <input type='submit' name='submit'>";
      echo "</form>";
    }
?> </center></td></tfoot>

</table>
      
<?php 
      
  } else {
    echo "<form 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><input type="text" size="53" name="username" value=<?php echo "'{$row['username']}'";?> ></td></tr>

<tr><td align="right"><b>TITLE:</b></td><td><input type="text" size="53" name="title" value=<?php "'{$row['title']}'";?> ></td></tr>

<tr><td align="right"><b>CHARACTER:</b></td><td><input type="text" size="53" name="character_name" value=<?php 
echo "'{$row['character_name']}'";?> ></td></tr>

<!-- and so forth; the form goes on again-->

<tfoot><td> </td><td colspan="3"><center>
<?php
    echo "  <input type='hidden' name='username'value='{$_SESSION['username']}'>";
    echo "  <input type='hidden' name='action' value='add'>";
    echo "  <input type='submit' name='submit'>";
    echo "</form>";
  }
?>
</center></td></tfoot>

</table>
</form>
</div>

<br />
<br />
<br />

</div>
</body>
</html>

 

A very puzzled and frustrated newbie.

Link to comment
Share on other sites

It seems I got my form to work with sessions. But it still doesn't work with the database (that side is set up and functioning). Nor does the form keep the data in the fields as it should when you post to self. What am I missing?

 

working with php 5.1

MySQL 5.0

 

<?php
session_start();

if (isset($_POST['action'])) {
	$username = isset($_SESSION['username']) ? mysql_real_escape_string($_POST['username']) : '';
	$title = isset($_POST['title']) ? mysql_real_escape_string($_POST['title']) : '';
	$genre = isset($_POST['genre']) ? mysql_real_escape_string($_POST['genre']) : '';
	$character_name = isset($_POST['character_name']) ? mysql_real_escape_string($_POST['character_name']) : '';
	//and so forth -- the variables for the form go on in the full script;

    switch ($_POST['action']) {
      case 'edit':
        $sql = "UPDATE traits SET ('title', 'genre','character_name') = ('$title', '$genre','$character_name') WHERE username = $_SESSION[$username]";
        break;
      case 'add':
        $sql = "INSERT INTO traits ('traits_id','username','title', 'genre','character_name') VALUES ('','$_SESSION[$username]','$title', '$genre','$character_name')";
        break;
    }
    if (mysql_query($sql)) {
      echo "Data {$_POST['action']}ed succesfully";
    }
  }

if (isset($_SESSION['username'])) {
    $sql = "SELECT * FROM traits WHERE username = $_SESSION[$username]";
    if ($result = mysql_query($result) && mysql_num_rows($result)) {
      $row = mysql_fetch_assoc($result);
      echo "<form 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><input type="text" size="53" name="username" value=<?php echo "'{'$_SESSION[$username]'}'";?> ></td></tr>

<tr><td align="right"><b>TITLE:</b></td><td><input type="text" size="53" name="title" value=<?php "'{$row['title']}'";?> ></td></tr>

<tr><td align="right"><b>CHARACTER:</b></td><td><input type="text" size="53" name="character_name" value=<?php 
echo "'{$row['character_name']}'";?> ></td></tr>

<!-- and so forth; the form goes on-->

<tfoot><td> </td><td colspan="3"><center><?php 
      echo "  <input type='hidden' name='username' value='{$_SESSION['username']}'>";
      echo "  <input type='hidden' name='action' value='edit'>";
      echo "  <input type='submit' name='submit'>";
      echo "</form>";
    
?> </center></td></tfoot>

</table>
      
<?php 
      
  } else {
    echo "<form 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><input type="text" size="53" name="username" value=<?php echo "{$_SESSION['username']}";?> ></td></tr>

<tr><td align="right"><b>TITLE:</b></td><td><input type="text" size="53" name="title" value=<?php "'{$row['title']}'";?> ></td></tr>

<tr><td align="right"><b>CHARACTER:</b></td><td><input type="text" size="53" name="character_name" value=<?php 
echo "'{$row['character_name']}'";?> ></td></tr>

<!-- and so forth; the form goes on again-->

<tfoot><td> </td><td colspan="3"><center>
<?php
    echo "  <input type='hidden' name='title'>";
    echo "  <input type='hidden' name='character_name'>";
    echo "  <input type='hidden' name='action' value='add'>";
    echo "  <input type='submit' name='submit'>";
    echo "</form>";
  }}
?>
</center></td></tfoot>

</table>
</form>
</div>

<br />
<br />
<br />

</div>
</body>
</html>

Link to comment
Share on other sites

But it still doesn't work with the database

 

You need to add some debuging. Its a little hard to see in your code but the syntx is something like....

 

<?php

  if (mysql_query($sql)) {
    // success.
  } else {
    echo "Query failed " . mysql_error() . $sql;
  }

?>

 

Nor does the form keep the data in the fields as it should

 

Your code is inconsistent. Your missing some echos, eg... this...

 

<?php "'{$row['title']}'";?>

 

Should be...

 

<?php echo "'{$row['title']}'";?>

Link to comment
Share on other sites

Here's where I get the error message:

 

Query failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''traits_id','username','title', 'genre','character_name') VALUES ('','mashaholl'' at line 1INSERT INTO traits ('traits_id','username','title', 'genre','character_name') VALUES ('','mashaholl','', '','')

 

 

Link to comment
Share on other sites

Well, that changed the error message. Now I get:

 

Query failed: Out of range value adjusted for column 'traits_id' at row 1INSERT INTO traits (`traits_id`,`username`,`title`, `genre`,`character_name`) VALUES ('','mholl','', 'sci-fi','')

 

The test database on my local drive has the "traits_id" column set at INT(5).

Link to comment
Share on other sites

I solved one problem. Somehow, the auto_increment got removed from the "traits_id" column. So now the INSERT part works beautifully, but not the UPDATE or the SELECT.

 

Where can I add a debugging line to see what's not working? Advice, please?

 

<?php
session_start();

if (isset($_POST['action'])) {
	$username = isset($_SESSION['username']) ? mysql_real_escape_string($_POST['username']) : '';
	$title = isset($_POST['title']) ? mysql_real_escape_string($_POST['title']) : '';
	$genre = isset($_POST['genre']) ? mysql_real_escape_string($_POST['genre']) : '';
	$character_name = isset($_POST['character_name']) ? mysql_real_escape_string($_POST['character_name']) : '';
	//and so forth -- the variables for the form go on in the full script;

    switch ($_POST['action']) {
      case 'edit':
        $sql = "UPDATE traits SET (`title`, `genre`,`character_name`) = ('$title', '$genre','$character_name') WHERE username = $_SESSION[$username]";
        break;
      case 'add':
        $sql = "INSERT INTO traits (`username`,`title`, `genre`,`character_name`) VALUES ('".$_SESSION['username']."','$title', '$genre','$character_name')";
        break;
    }
    if (mysql_query($sql)) {
      echo "<center>Data {$_POST['action']}ed succesfully</center>";
    }
    else {
    echo "Query failed: " . mysql_error(). $sql;
  }
  }

if (isset($_SESSION['username'])) {
    $sql = "SELECT * FROM traits WHERE username = $_SESSION[$username]";
    if ($result = mysql_query($sql) && mysql_num_rows($sql)) {
      $row = mysql_fetch_assoc($result);
      echo "<form 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><input type="text" size="53" name="username" value=<?php echo "'{'$_SESSION[$username]'}'";?> ></td></tr>

<tr><td align="right"><b>TITLE:</b></td><td><input type="text" size="53" name="title" value=<?php "'{$row['title']}'";?> ></td></tr>

<tr><td align="right"><b>GENRE:</b></td><td><input type="text" size="53" name="genre" value=<?php "'{$row['genre']}'";?> ></td></tr>

<tr><td align="right"><b>CHARACTER:</b></td><td><input type="text" size="53" name="character_name" value=<?php 
echo "'{$row['character_name']}'";?> ></td></tr>

<!-- and so forth; the form goes on-->

<tfoot><td> </td><td colspan="3"><center><?php 
      echo "  <input type='hidden' name='traits_id'>";
      echo "  <input type='hidden' name='action' value='edit'>";
      echo "  <input type='submit' name='submit'>";
      echo "</form>";
    
?> </center></td></tfoot>

</table>
      
<?php 
     
  } else {
    echo "<form 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><input type="text" size="53" name="username" value=<?php echo "{$_SESSION['username']}";?> ></td></tr>

<tr><td align="right"><b>TITLE:</b></td><td><input type="text" size="53" name="title" value=<?php  echo "{$_POST['title']}";?> ></td></tr>

<tr><td align="right"><b>GENRE:</b></td><td><input type="text" size="53" name="genre" value=<?php  echo "{$_POST['genre']}";?> ></td></tr>

<tr><td align="right"><b>CHARACTER:</b></td><td><input type="text" size="53" name="character_name" value=<?php 
echo "{$_POST['character_name']}";?> ></td></tr>

<!-- and so forth; the form goes on again-->

<tfoot><td> </td><td colspan="3"><center>
<?php
    
    echo "  <input type='hidden' name='action' value='add'>";
    echo "  <input type='submit' name='submit'>";
    echo "</form>";
  }}
?>
</center></td></tfoot>

</table>
</form>



Link to comment
Share on other sites

The problem seems to be with the sql query. I can't get a debug response, but when I try to run the query directly through mysql, I get this message:

 

UPDATE traits SET (`title`, `genre`,`character_name`) = ('value1', 'value2','value3') WHERE `username` =
user

Error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server
version for the right syntax to use near '(`title`, `genre`,`character_name`) = ('value1', 'value2','value3') 
WHERE' at line 1

 

I've been staring at it, now I'm blind and I can't see what's wrong. Help!!

Link to comment
Share on other sites

I really dont think you can do it like this:

$sql = "UPDATE traits SET (`title`, `genre`,`character_name`) = ('$title', '$genre','$character_name') WHERE username = $_SESSION[$username]";

 

I would do this:

$sql = "UPDATE traits SET title = '$title', genre = '$genre', character_name = '$character_name'  WHERE username = $_SESSION[$username]";

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.