matthewst Posted May 30, 2007 Share Posted May 30, 2007 I have a form which is autopopulated. I need my users to be able to make changes to it when necessary. The problem is my users can change whatever needs to be changed but when they hit submit it doesn't change anything in the database. <form name="FormName" action="<?=$PHP_SELF;?>" enctype="multipart/form-data" method="post"> $query="SELECT * FROM abc_tables WHERE table_id=$table_id"; $result=mysql_query($query); while ($row = mysql_fetch_assoc($result)) { $rest_name = $row['rest_name']; <input type="text" class="formTextbox" name="rest_name" size="24" value="<?="$rest_name"?>"> <input type="submit" name="submit" class="formTextbox" value="Submit"> Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted May 30, 2007 Share Posted May 30, 2007 Well...you don't have any code telling it to update the DB...you need something like this at the top of your script: <?php if ($_POST['submit']){ $rest_name = $_POST['rest_name']; mysql_query("UPDATE tbl SET rest_name='$rest_name' WHERE condition"); echo 'Updated Successfully'; } ?> also as your form action you have: $PHP_SELF It should be: $_SERVER['PHP_SELF'] You also need to end your while loop, and use echo to display the HTML in between, or close the PHP tags. Quote Link to comment Share on other sites More sharing options...
matthewst Posted May 30, 2007 Author Share Posted May 30, 2007 I've got all the quotes and everything that was just a snippet so you would have an idea of what I was talking about. Heres what I have now: At the top: function update_db() { if ($_POST['submit']){ $contact_fname = $_POST['contact_fname']; mysql_query("UPDATE abc_tables SET contact_fname='$contact_fname'"); }} Then: <input type="text" class="formTextbox" name="contact_fname" size="24" value="<?="$contact_fname"?>"> At the bottom: <input type="submit" name="submit" class="formTextbox" value="Submit" onsubmit="<?=update_db();?>"> But it still dosen't update the database. Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted May 30, 2007 Share Posted May 30, 2007 On this line: mysql_query("UPDATE abc_tables SET contact_fname='$contact_fname'"); You have no condition, so it is going to update every records contact_fname to that variable, which I am sure you don't want. Why are you putting the code into a function? Take it out of the function and try it. Quote Link to comment Share on other sites More sharing options...
matthewst Posted May 30, 2007 Author Share Posted May 30, 2007 here is the current query: mysql_query("UPDATE abc_tables SET contact_fname='$contact_fname' WHERE table_id=$table_id"); I have it in a function so it dosen't update when the page loads. example: a user gets a legal name change and needs to update his info he would load this page (all fields autopopulate) change his name then click submit (onsubmit="<?=update_db();?>") Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted May 30, 2007 Share Posted May 30, 2007 Oh, your trying to mix JavaScript and PHP together....I can't help you there. Quote Link to comment Share on other sites More sharing options...
matthewst Posted June 1, 2007 Author Share Posted June 1, 2007 we can do it without java, i don't care as long as it works Quote Link to comment Share on other sites More sharing options...
penguin0 Posted June 1, 2007 Share Posted June 1, 2007 did you try an if statement: <? if (update_db) { "code you want to happen"; } ?> <input type="submit" name="update_db" value="Update"> Quote Link to comment Share on other sites More sharing options...
matthewst Posted June 1, 2007 Author Share Posted June 1, 2007 Still no good. Here is all the relevent code: <? if (submit) { "UPDATE abc_tables SET contact_fname=$contact_fname WHERE tabel_id=$table_id"; } ?> <form name="FormName" action="<?=$_server['PHP_SELF'];?>" enctype="multipart/form-data" method="post"> <?php $query="SELECT * FROM abc_tables WHERE table_id=$table_id"; $result=mysql_query($query); while ($row = mysql_fetch_assoc($result)) { $contact_fname = $row['contact_fname']; } ?> <table> <tr> <td>Contact First Name</td> </tr> <tr> <td> <input type="text" class="formTextbox" name="contact_fname" size="24" value="<?="$contact_fname"?>"></td> </tr> ////////////////////// <tr> <td> <input type="submit" name="submit" class="formTextbox" value="Submit"> </td> </tr> </table> <? mysql_close(); ?> Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted June 1, 2007 Share Posted June 1, 2007 <?php if ($_POST['submit']) { $table_id = "?"; //You need to give this variable the value of whatever you want it to be that fits the query mysql_query("UPDATE abc_tables SET contact_fname=$contact_fname WHERE tabel_id='$table_id'"); } ?> <form name="FormName" action="<?=$_server['PHP_SELF'];?>" enctype="multipart/form-data" method="post"> <?php $query="SELECT * FROM abc_tables WHERE table_id=$table_id"; $result=mysql_query($query); while ($row = mysql_fetch_assoc($result)) { $contact_fname = $row['contact_fname']; } ?> <table> <tr> <td>Contact First Name</td> </tr> <tr> <td> <input type="text" class="formTextbox" name="contact_fname" size="24" value="<?="$contact_fname"?>"></td> </tr> <tr> <td> <input type="submit" name="submit" class="formTextbox" value="Submit"> </td> </tr> </table> Quote Link to comment Share on other sites More sharing options...
matthewst Posted June 4, 2007 Author Share Posted June 4, 2007 OK so I changed this: <form name="FormName" action="<?=$_server['PHP_SELF'];?>" enctype="multipart/form-data" method="post"> to this: <form name="FormName" action="table_order_handler.php" enctype="multipart/form-data" method="post"> and added this: table_order_handler.php <?php error_reporting(0); include('include/user_check.php'); include('include/db_con.php'); $query="SELECT * FROM abc_tables WHERE table_id=$table_id"; $result=mysql_query($query); while ($row = mysql_fetch_assoc($result)) { $rest_name = $row['rest_name']; } $query_update="UPDATE abc_tables SET contact_fname=$contact_fname WHERE tabel_id=$table_id"; mysql_query($query_update); echo "The Table Order Form for $rest_name has been updated."; ?> but still nothing Quote Link to comment Share on other sites More sharing options...
matthewst Posted June 4, 2007 Author Share Posted June 4, 2007 As it turns out I needed to add a hidden field to my form <input type="hidden" name="table_id" value="<?="$table_id"?>"> that way $table_id gets posted to the handler page thanks for all your help! Quote Link to comment 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.