Jump to content

[SOLVED] changing data in an autopopulating form


matthewst

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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();?>")

Link to comment
Share on other sites

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();
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

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.