Form based UPDATE problem
Moderator: General Moderators
Form based UPDATE problem
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.
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.
- RandomEngy
- Forum Contributor
- Posts: 173
- Joined: Wed Jun 26, 2002 3:24 pm
- Contact:
Try something like
Code: Select all
$query = "UPDATE applicants SET name='".$_POSTї'name']."' WHERE id=$id";
mysql_query($query) or die(mysql_error());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'].")";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.
<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.
- RandomEngy
- Forum Contributor
- Posts: 173
- Joined: Wed Jun 26, 2002 3:24 pm
- Contact:
Actually you can combine get and post, like this:
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.
Code: Select all
<form action="test.php?testvar=testvalue" method="post">Also, BRE, note that you don't want the .")" at the end of your query.
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?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.
jason wrote:What does echo mysql_error() show?
I put
Code: Select all
echo mysql_error();- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
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):
Mac
Code: Select all
$sql = "UPDATE applicants SET name='".$_POSTї'name']."' WHERE applicantid='".$_GETї'applicantid']."'";
mysql_query($sql) or die(mysql_error().'<p>'.$sql.'</p>');- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
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):
the updaterow script:
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>');
?>Code: Select all
while ($row = mysql_fetch_array($result)){
$_GETї'applicantid'] = $rowї"applicantid"];
$_GETї'name'] = $rowї"name"];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.fatalcure wrote:why are u doing that?? you're setting GET variabes?Code: Select all
while ($row = mysql_fetch_array($result)){ $_GETї'applicantid'] = $rowї"applicantid"]; $_GETї'name'] = $rowї"name"];
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