surochek Posted May 31, 2007 Share Posted May 31, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/53756-multitasking-form/ Share on other sites More sharing options...
per1os Posted May 31, 2007 Share Posted May 31, 2007 Google.com AJAX Javascript/PHP together. Look into Ajax my friend. Quote Link to comment https://forums.phpfreaks.com/topic/53756-multitasking-form/#findComment-265690 Share on other sites More sharing options...
trq Posted May 31, 2007 Share Posted May 31, 2007 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>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/53756-multitasking-form/#findComment-265700 Share on other sites More sharing options...
surochek Posted May 31, 2007 Author Share Posted May 31, 2007 Frost: Thanks. I did think Ajax, but I'm not quite up to it. Thorpe: Thank you! Time to play now! I'll be back with results! Masha Quote Link to comment https://forums.phpfreaks.com/topic/53756-multitasking-form/#findComment-265908 Share on other sites More sharing options...
surochek Posted June 2, 2007 Author Share Posted June 2, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/53756-multitasking-form/#findComment-266833 Share on other sites More sharing options...
surochek Posted June 2, 2007 Author Share Posted June 2, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/53756-multitasking-form/#findComment-267180 Share on other sites More sharing options...
penguin0 Posted June 3, 2007 Share Posted June 3, 2007 did you try just putting the $_SESSION[] above the if statement? Quote Link to comment https://forums.phpfreaks.com/topic/53756-multitasking-form/#findComment-267213 Share on other sites More sharing options...
surochek Posted June 3, 2007 Author Share Posted June 3, 2007 Which if statement? the first one? How? Quote Link to comment https://forums.phpfreaks.com/topic/53756-multitasking-form/#findComment-267214 Share on other sites More sharing options...
surochek Posted June 3, 2007 Author Share Posted June 3, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/53756-multitasking-form/#findComment-267399 Share on other sites More sharing options...
surochek Posted June 4, 2007 Author Share Posted June 4, 2007 <bump> Quote Link to comment https://forums.phpfreaks.com/topic/53756-multitasking-form/#findComment-267584 Share on other sites More sharing options...
surochek Posted June 5, 2007 Author Share Posted June 5, 2007 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> Quote Link to comment https://forums.phpfreaks.com/topic/53756-multitasking-form/#findComment-268540 Share on other sites More sharing options...
trq Posted June 5, 2007 Share Posted June 5, 2007 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']}'";?> Quote Link to comment https://forums.phpfreaks.com/topic/53756-multitasking-form/#findComment-268550 Share on other sites More sharing options...
surochek Posted June 5, 2007 Author Share Posted June 5, 2007 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','', '','') Quote Link to comment https://forums.phpfreaks.com/topic/53756-multitasking-form/#findComment-268674 Share on other sites More sharing options...
per1os Posted June 5, 2007 Share Posted June 5, 2007 Your column names need to be encapsulated in back ticks ( ` ) and not single quotes ( ' ) single quotes are for values. Quote Link to comment https://forums.phpfreaks.com/topic/53756-multitasking-form/#findComment-268675 Share on other sites More sharing options...
surochek Posted June 5, 2007 Author Share Posted June 5, 2007 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). Quote Link to comment https://forums.phpfreaks.com/topic/53756-multitasking-form/#findComment-268686 Share on other sites More sharing options...
surochek Posted June 6, 2007 Author Share Posted June 6, 2007 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> Quote Link to comment https://forums.phpfreaks.com/topic/53756-multitasking-form/#findComment-268872 Share on other sites More sharing options...
surochek Posted June 6, 2007 Author Share Posted June 6, 2007 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!! Quote Link to comment https://forums.phpfreaks.com/topic/53756-multitasking-form/#findComment-269093 Share on other sites More sharing options...
surochek Posted June 6, 2007 Author Share Posted June 6, 2007 <BUMP> Quote Link to comment https://forums.phpfreaks.com/topic/53756-multitasking-form/#findComment-269392 Share on other sites More sharing options...
penguin0 Posted June 8, 2007 Share Posted June 8, 2007 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]"; Quote Link to comment https://forums.phpfreaks.com/topic/53756-multitasking-form/#findComment-271061 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.