Form based UPDATE problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
BRE
Forum Newbie
Posts: 10
Joined: Mon Jul 29, 2002 11:50 am
Location: Cartersville Georgia

Form based UPDATE problem

Post by BRE »

I've been learning PHP/MySQL for a few weeks now, and I'm going through the basics like inserting, deleting and viewing records. I'm having problems updating records through a form. I'm using the new super globals as well.

I've created a form that fills the form with the appropriate record data using the following code snippet:
<td>Applicant Name</td>
<td><input type="text" name="name" value="<?echo $_GET['name'];?>" maxlength=30 size=30><br></td>

This form passes the changes onto another script that updates the record using the following code snippet:
"UPDATE applicants SET name='".$_POST['name'].")";

This doesn't work, so I'm guessing i'm messing up with the get and post methods. Do i need to use the same method across both the form and the script (2 separate files)? Is there a better way to fill the form with the records data prior to updating?

I've read the PHP 4.2 post and i've searched the forums, but I still haven't really found an answer.
User avatar
RandomEngy
Forum Contributor
Posts: 173
Joined: Wed Jun 26, 2002 3:24 pm
Contact:

Post by RandomEngy »

Try something like

Code: Select all

$query = "UPDATE applicants SET name='".$_POST&#1111;'name']."' WHERE id=$id";
mysql_query($query) or die(mysql_error());
BRE
Forum Newbie
Posts: 10
Joined: Mon Jul 29, 2002 11:50 am
Location: Cartersville Georgia

Post by BRE »

Oh, sorry i forgot to add that to my original post. Here is the actual code i'm using.

Code: Select all

$query="UPDATE applicants SET name='".$_POST&#1111;'name']."' WHERE applicantid=".$_GET&#1111;'applicantid'].")";
fatalcure
Forum Contributor
Posts: 141
Joined: Thu Jul 04, 2002 12:57 pm
Contact:

Post by fatalcure »

uhm, a form method can only be GET or POST, so you can't combine both GET and POST....

<form method='POST'> or <form method='GET'>

and use the corresponding global vars to get the form information:

$query="UPDATE applicants SET name='".$_POST['name']."' WHERE applicantid=".$_POST['applicantid'].")";

both have to be the same, depending on the method used in your form.
User avatar
RandomEngy
Forum Contributor
Posts: 173
Joined: Wed Jun 26, 2002 3:24 pm
Contact:

Post by RandomEngy »

Actually you can combine get and post, like this:

Code: Select all

<form action="test.php?testvar=testvalue" method="post">
When you submit, there are correct values in both $_GET and $_POST .

Also, BRE, note that you don't want the .")" at the end of your query.
BRE
Forum Newbie
Posts: 10
Joined: Mon Jul 29, 2002 11:50 am
Location: Cartersville Georgia

Post by BRE »

fatalcure wrote:uhm, a form method can only be GET or POST, so you can't combine both GET and POST....

<form method='POST'> or <form method='GET'>

and use the corresponding global vars to get the form information:

$query="UPDATE applicants SET name='".$_POST['name']."' WHERE applicantid=".$_POST['applicantid'].")";

both have to be the same, depending on the method used in your form.
hmmm, still doesn't work. maybe i have a problem somewhere else. If i use <form method="POST"> how do i fill in the form with the record data prior to updating it?
BRE
Forum Newbie
Posts: 10
Joined: Mon Jul 29, 2002 11:50 am
Location: Cartersville Georgia

Post by BRE »

Anyone know of a url with a good example scripts of an update form using the super globals?
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

What does echo mysql_error() show?
BRE
Forum Newbie
Posts: 10
Joined: Mon Jul 29, 2002 11:50 am
Location: Cartersville Georgia

Post by BRE »

jason wrote:What does echo mysql_error() show?

I put

Code: Select all

echo mysql_error();
after the UPDATE statement?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Remove the parenthesis at the end of the query as RandomEngy pointed out and put mysql_error() at the end of the line where you actually run the query (as RandomEngy again pointed out):

Code: Select all

$sql = "UPDATE applicants SET name='".$_POST&#1111;'name']."' WHERE applicantid='".$_GET&#1111;'applicantid']."'";
mysql_query($sql) or die(mysql_error().'<p>'.$sql.'</p>');
Mac
BRE
Forum Newbie
Posts: 10
Joined: Mon Jul 29, 2002 11:50 am
Location: Cartersville Georgia

Post by BRE »

OK, i followed everyone's advice. I don't get an error message, it just doesn't update the record.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Why don't you post a bit more of your code so that we can help you a bit better then.

Mac
BRE
Forum Newbie
Posts: 10
Joined: Mon Jul 29, 2002 11:50 am
Location: Cartersville Georgia

Post by BRE »

I know this code is very ugly. I own an Assisted Living company, if you wonder about the variable names.

The update form (i cut it off at the bottom to shorten this post but you should get the idea, it obviously continues with the remaining fields):

Code: Select all

<?
$connection = mysql_connect("localhost","user","password")      
$db = mysql_select_db("waitinglist", $connection);
$sql = ("SELECT * FROM applicants WHERE applicantid=".$_GET&#1111;'applicantid'].""); 
$result = mysql_query($sql);         
while ($row = mysql_fetch_array($result))&#123;
$_GET&#1111;'applicantid'] = $row&#1111;"applicantid"];
$_GET&#1111;'name'] = $row&#1111;"name"];  
$_GET&#1111;'birth'] = $row&#1111;"birth"];
$_GET&#1111;'sex'] = $row&#1111;"sex"];
$_GET&#1111;'residence'] = $row&#1111;"residence"];
$_GET&#1111;'equipment'] = $row&#1111;"equipment"];
$_GET&#1111;'mental'] = $row&#1111;"mental"];
$_GET&#1111;'diet'] = $row&#1111;"diet"];
$_GET&#1111;'bedridden'] = $row&#1111;"bedridden"];
$_GET&#1111;'smokes'] = $row&#1111;"smokes"];
$_GET&#1111;'tobacco'] = $row&#1111;"tobacco"];
$_GET&#1111;'hospitalized'] = $row&#1111;"hospitalized"];
$_GET&#1111;'mentalfacility'] = $row&#1111;"mentalfacility"];
$_GET&#1111;'diabetic'] = $row&#1111;"diabetic"];
$_GET&#1111;'bladder'] = $row&#1111;"bladder"];
$_GET&#1111;'bowel'] = $row&#1111;"bowel"];
$_GET&#1111;'contact'] = $row&#1111;"contact"];
$_GET&#1111;'street'] = $row&#1111;"street"];
$_GET&#1111;'city'] = $row&#1111;"city"];
$_GET&#1111;'state'] = $row&#1111;"state"];
$_GET&#1111;'zip'] = $row&#1111;"zip"];
$_GET&#1111;'dayphone'] = $row&#1111;"dayphone"];
$_GET&#1111;'eveningphone'] = $row&#1111;"eveningphone"];
$_GET&#1111;'cell'] = $row&#1111;"cell"];
$_GET&#1111;'email'] = $row&#1111;"email"];
$_GET&#1111;'dpsuite'] = $row&#1111;"dpsuite"];
$_GET&#1111;'spsuite'] = $row&#1111;"spsuite"];
$_GET&#1111;'dssuite'] = $row&#1111;"dssuite"];
$_GET&#1111;'sssuite'] = $row&#1111;"sssuite"];
$_GET&#1111;'dproom'] = $row&#1111;"dproom"];
$_GET&#1111;'sproom'] = $row&#1111;"sproom"];
$_GET&#1111;'dsroom'] = $row&#1111;"ssroom"];
$_GET&#1111;'ssroom'] = $row&#1111;"ssroom"];
$_GET&#1111;'dclong'] = $row&#1111;"dclong"];
$_GET&#1111;'dcshort'] = $row&#1111;"dcshort"];
$_GET&#1111;'services'] = $row&#1111;"services"];
&#125;
?>

<form action="updaterow.php" method="post">
<table border="0">
<tr>
<td>Applicant Name</td>
<td><input type="text" name="name" value="<?echo $_GET&#1111;'name'];?>" maxlength=30 size=30><br></td>
</tr>
<tr>
<td>Date of Birth</td>
<td><input type="text" name="birth" value="<?echo $_GET&#1111;'birth'];?>" maxlength=11 size=11> format YYYY-MM-DD<br></td>
</tr>
<tr>
<td>Sex</td>
<td><input type="text" name="sex" value="<?echo $_GET&#1111;'sex'];?>" maxlength=1 size=1><br></td>

</tr>

<tr>
<td colspan=2><input type=submit name="submit" value="Update Applicant Data"></td>
</tr>
</form>


the updaterow script:

Code: Select all

<?
$connection = mysql_connect("localhost","user","password")  
or die ("Error: ".mysql_error());         
$db = mysql_select_db("waitinglist", $connection);

$sql="UPDATE applicants SET name='".$_POST&#1111;'name']."',birth='".$_POST&#1111;'birth']."',sex='".$_POST&#1111;'sex']."',residence='".$_POST&#1111;'residence']."',
equipment='".$_POST&#1111;'equipment']."',mental='".$_POST&#1111;'mental']."',diet='".$_POST&#1111;'diet']."',bedridden='".$_POST&#1111;'bedridden']."',
smokes='".$_POST&#1111;'smokes']."',tobacco='".$_POST&#1111;'tobacco']."',hospitalized='".$_POST&#1111;'hospitalized']."',mentalfacility='".$_POST&#1111;'mentalfacility']."',
diabetic='".$_POST&#1111;'diabetic']."',bladder='".$_POST&#1111;'bladder']."',bowel='".$_POST&#1111;'bowel']."',contact='".$_POST&#1111;'contact']."',street='".$_POST&#1111;'street']."',
city='".$_POST&#1111;'city']."',state='".$_POST&#1111;'state']."',zip='".$_POST&#1111;'zip']."',dayphone='".$_POST&#1111;'dayphone']."',eveningphone='".$_POST&#1111;'eveningphone']."',
cell='".$_POST&#1111;'cell']."',email='".$_POST&#1111;'email']."',dpsuite='".$_POST&#1111;'dpsuite']."',spsuite='".$_POST&#1111;'spsuite']."',dssuite='".$_POST&#1111;'dssuite']."',
sssuite='".$_POST&#1111;'sssuite']."',dproom='".$_POST&#1111;'dproom']."',sproom='".$_POST&#1111;'sproom']."',dsroom='".$_POST&#1111;'dsroom']."',ssroom='".$_POST&#1111;'ssroom']."',
dclong='".$_POST&#1111;'dclong']."',dcshort='".$_POST&#1111;'dcshort']."',services='".$_POST&#1111;'services']."' WHERE applicantid='".$_POST&#1111;'applicantid']."'";
mysql_query($sql) or die(mysql_error().'<p>'.$sql.'</p>');

?>
fatalcure
Forum Contributor
Posts: 141
Joined: Thu Jul 04, 2002 12:57 pm
Contact:

Post by fatalcure »

Code: Select all

while ($row = mysql_fetch_array($result))&#123; 
$_GET&#1111;'applicantid'] = $row&#1111;"applicantid"]; 
$_GET&#1111;'name'] = $row&#1111;"name"];
why are u doing that?? you're setting GET variabes?

just use $applicantid = $row["applicantid"] etc. or if u WANT to use an array just do $info['applicantid'] = $row["applicantid"]...you dont want to set any GET values urself....

and in your form make sure you have a hidden field that holds the applicantid like so:
echo "<input type='hidden' name='applicantid' value='" . $applicantid . "'> ";
since your update form is looking for that variable, and if its not found, it cant update any records...

hope it helps...if im not clear or anything, reply :)
BRE
Forum Newbie
Posts: 10
Joined: Mon Jul 29, 2002 11:50 am
Location: Cartersville Georgia

Post by BRE »

fatalcure wrote:

Code: Select all

while ($row = mysql_fetch_array($result))&#123; 
$_GET&#1111;'applicantid'] = $row&#1111;"applicantid"]; 
$_GET&#1111;'name'] = $row&#1111;"name"];
why are u doing that?? you're setting GET variabes?

just use $applicantid = $row["applicantid"] etc. or if u WANT to use an array just do $info['applicantid'] = $row["applicantid"]...you dont want to set any GET values urself....

and in your form make sure you have a hidden field that holds the applicantid like so:
echo "<input type='hidden' name='applicantid' value='" . $applicantid . "'> ";
since your update form is looking for that variable, and if its not found, it cant update any records...

hope it helps...if im not clear or anything, reply :)
Thanks. I figured my logic was flawed, and sure enough it was. I was unclear about the proper way to go about doing an update. This makes sense, thanks. I had just realized about an hour ago that I wasn't providing the applicantid as well. I'll see how it goes with this. Thanks again.
Post Reply