Page 1 of 1
Form based UPDATE problem
Posted: Mon Jul 29, 2002 11:50 am
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.
Posted: Mon Jul 29, 2002 12:23 pm
by RandomEngy
Try something like
Code: Select all
$query = "UPDATE applicants SET name='".$_POSTї'name']."' WHERE id=$id";
mysql_query($query) or die(mysql_error());
Posted: Mon Jul 29, 2002 1:26 pm
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ї'name']."' WHERE applicantid=".$_GETї'applicantid'].")";
Posted: Mon Jul 29, 2002 1:34 pm
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.
Posted: Mon Jul 29, 2002 2:01 pm
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.
Posted: Mon Jul 29, 2002 2:03 pm
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?
Posted: Mon Jul 29, 2002 2:23 pm
by BRE
Anyone know of a url with a good example scripts of an update form using the super globals?
Posted: Mon Jul 29, 2002 2:37 pm
by jason
What does echo mysql_error() show?
Posted: Mon Jul 29, 2002 2:58 pm
by BRE
jason wrote:What does echo mysql_error() show?
I put
after the UPDATE statement?
Posted: Tue Jul 30, 2002 1:47 am
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ї'name']."' WHERE applicantid='".$_GETї'applicantid']."'";
mysql_query($sql) or die(mysql_error().'<p>'.$sql.'</p>');
Mac
Posted: Tue Jul 30, 2002 10:44 am
by BRE
OK, i followed everyone's advice. I don't get an error message, it just doesn't update the record.
Posted: Tue Jul 30, 2002 11:51 am
by twigletmac
Why don't you post a bit more of your code so that we can help you a bit better then.
Mac
Posted: Tue Jul 30, 2002 1:15 pm
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ї'applicantid']."");
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)){
$_GETї'applicantid'] = $rowї"applicantid"];
$_GETї'name'] = $rowї"name"];
$_GETї'birth'] = $rowї"birth"];
$_GETї'sex'] = $rowї"sex"];
$_GETї'residence'] = $rowї"residence"];
$_GETї'equipment'] = $rowї"equipment"];
$_GETї'mental'] = $rowї"mental"];
$_GETї'diet'] = $rowї"diet"];
$_GETї'bedridden'] = $rowї"bedridden"];
$_GETї'smokes'] = $rowї"smokes"];
$_GETї'tobacco'] = $rowї"tobacco"];
$_GETї'hospitalized'] = $rowї"hospitalized"];
$_GETї'mentalfacility'] = $rowї"mentalfacility"];
$_GETї'diabetic'] = $rowї"diabetic"];
$_GETї'bladder'] = $rowї"bladder"];
$_GETї'bowel'] = $rowї"bowel"];
$_GETї'contact'] = $rowї"contact"];
$_GETї'street'] = $rowї"street"];
$_GETї'city'] = $rowї"city"];
$_GETї'state'] = $rowї"state"];
$_GETї'zip'] = $rowї"zip"];
$_GETї'dayphone'] = $rowї"dayphone"];
$_GETї'eveningphone'] = $rowї"eveningphone"];
$_GETї'cell'] = $rowї"cell"];
$_GETї'email'] = $rowї"email"];
$_GETї'dpsuite'] = $rowї"dpsuite"];
$_GETї'spsuite'] = $rowї"spsuite"];
$_GETї'dssuite'] = $rowї"dssuite"];
$_GETї'sssuite'] = $rowї"sssuite"];
$_GETї'dproom'] = $rowї"dproom"];
$_GETї'sproom'] = $rowї"sproom"];
$_GETї'dsroom'] = $rowї"ssroom"];
$_GETї'ssroom'] = $rowї"ssroom"];
$_GETї'dclong'] = $rowї"dclong"];
$_GETї'dcshort'] = $rowї"dcshort"];
$_GETї'services'] = $rowї"services"];
}
?>
<form action="updaterow.php" method="post">
<table border="0">
<tr>
<td>Applicant Name</td>
<td><input type="text" name="name" value="<?echo $_GETї'name'];?>" maxlength=30 size=30><br></td>
</tr>
<tr>
<td>Date of Birth</td>
<td><input type="text" name="birth" value="<?echo $_GETї'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ї'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ї'name']."',birth='".$_POSTї'birth']."',sex='".$_POSTї'sex']."',residence='".$_POSTї'residence']."',
equipment='".$_POSTї'equipment']."',mental='".$_POSTї'mental']."',diet='".$_POSTї'diet']."',bedridden='".$_POSTї'bedridden']."',
smokes='".$_POSTї'smokes']."',tobacco='".$_POSTї'tobacco']."',hospitalized='".$_POSTї'hospitalized']."',mentalfacility='".$_POSTї'mentalfacility']."',
diabetic='".$_POSTї'diabetic']."',bladder='".$_POSTї'bladder']."',bowel='".$_POSTї'bowel']."',contact='".$_POSTї'contact']."',street='".$_POSTї'street']."',
city='".$_POSTї'city']."',state='".$_POSTї'state']."',zip='".$_POSTї'zip']."',dayphone='".$_POSTї'dayphone']."',eveningphone='".$_POSTї'eveningphone']."',
cell='".$_POSTї'cell']."',email='".$_POSTї'email']."',dpsuite='".$_POSTї'dpsuite']."',spsuite='".$_POSTї'spsuite']."',dssuite='".$_POSTї'dssuite']."',
sssuite='".$_POSTї'sssuite']."',dproom='".$_POSTї'dproom']."',sproom='".$_POSTї'sproom']."',dsroom='".$_POSTї'dsroom']."',ssroom='".$_POSTї'ssroom']."',
dclong='".$_POSTї'dclong']."',dcshort='".$_POSTї'dcshort']."',services='".$_POSTї'services']."' WHERE applicantid='".$_POSTї'applicantid']."'";
mysql_query($sql) or die(mysql_error().'<p>'.$sql.'</p>');
?>
Posted: Tue Jul 30, 2002 8:31 pm
by fatalcure
Code: Select all
while ($row = mysql_fetch_array($result)){
$_GETї'applicantid'] = $rowї"applicantid"];
$_GETї'name'] = $rowї"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

Posted: Tue Jul 30, 2002 8:40 pm
by BRE
fatalcure wrote:Code: Select all
while ($row = mysql_fetch_array($result)){
$_GETї'applicantid'] = $rowї"applicantid"];
$_GETї'name'] = $rowї"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.