Log in

View Full Version : Post Form data to Update MySQL get 500 Server Error



dnevels
12-16-2015, 09:11 PM
I am trying to put together a form that is populated from data in a MySQL. Then I want to allow the visitor to change information in that form and by pressing a button, update the database with the new information. This is going to be used by only about 700 people or less to update their member information and access to the form will be through an email that the member gets, so it doesn't have to be a bullet proof system. I found an example in this thread from 6 years ago posted by forum_amnesiac:

http://www.dynamicdrive.com/forums/showthread.php?45895-How-to-populate-php-html-form-with-MySQL-data

I am giving the member a link that displays their member information (that part works) It is just when I attempt to send them to the last page that would do the update is where I get the 500 Server Error. Here is some of my code from the form for them to update. It populates the fields from the database and allows the member to edit the field :


<form name="update" action="submitupdate.php" method="POST" />

<input type="hidden" name="id" value="<?=$record['id']?>"
<tr><b>Business Name: </b></br><input type="text" size="55" name="business_name" value="<?=$record['business_name']?>" ></tr></br></br>
<tr><input type="submit" name="submit" value="update" > </tr>

Here is my submitupdate.php (That I get the 500 Server error on:


<?php
$con =new mysqli('localhost', xxxxxx, 'xxxxxx','xxxxxx');
mysqli_select_db($con, "xxxxxx") or die(mysqli_error());


$id = $_POST['id'];
$business_name = $_POST['business_name'];
$phone = $_POST['phone'];
$fax = $_POST['fax'];
$address1 = $_POST['address1'];
$address2 = $_POST['address2'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$website = $_POST['website'];
$contact = $_POST['contact'];
$email = $_POST['email'];


if(isset($_POST['id'])) {
$UpdateQuery = "UPDATE members SET business_name='$_POST[business_name]', phone='$_POST[phone]', fax='$_POST[fax]', address1='$_POST[address1]', address2='$_POST[address2]', city='$_POST[city]', state='$_POST[state]', zip='$_POST[zip]', website='$_POST[website]', contact='$_POST[contact]', email='$_POST[email]', update_flag='$_POST[update_flag]', WHERE id='$id'";
mysqli_query($UpdateQuery, $con);

echo "$UpdateQuery";exit();

$sql = "SELECT * FROM members WHERE id = $id";
$my_Data = mysqli_query($sql,$con);

while($record = mysqli_fetch_array($my_Data)) {
?>

</br>

<tr><b>Business Name: </b></br><input type="text" size="55" name="business_name" value="<?=$record['business_name']?>" ></tr></br></br>
<tr><b>Phone: </b></br><input type="text" size="55" name="phone" value="<?=$record['phone']?>" > </tr></br></br>

Just display the updated record here.

james438
12-16-2015, 10:58 PM
First, you are using the deprecated mysql() queries as opposed to the current mysqli() queries. See this thread (http://www.dynamicdrive.com/forums/showthread.php?78984-Help-with-database) for more.

Second, check your query before sending it to the database. For example insert


echo "$UpdateQuery";exit();

just before


mysql_query($UpdateQuery, $con);

Third, I do not see a place where $_POST['update'] has been set. It looks like other values have not been set either. echo "$UpdateQuery";exit(); should help expose the empty unpassed values.

Last,


if ($retval )

is always true ;)

There may be other errors, but those are the ones I saw at first glance.

dnevels
12-17-2015, 04:20 AM
Thanks for the suggestions james438! I updated the code above (hopefully correctly) to mysqli. added the "echo "$UpdateQuery";exit();" and got rid of the if ($retval ) since I always want it to show the updated record. However, I still have the same problem, I get the Server error 500! Did I do the conversion to mysqli wrong?

Beverleyh
12-17-2015, 05:55 AM
Shouldn't there be apostrophes around each value too?;
$con =new mysqli('localhost', 'xxxxxx', 'xxxxxx', 'xxxxxx');

james438
12-17-2015, 03:45 PM
You may want to fix this too:


<tr><b>Business Name: </b></br><input type="text" size="55" name="business_name" value="<?=$record['business_name']?>" ></tr></br></br>

The following is only to be used to test your code to look for empty values or unusual data. What exactly does the 500 server error say?
echo "$UpdateQuery";exit();


You may want to change your UPDATE to the following:

UPDATE members SET business_name='$business_name', phone='$phone', etc.

but it is fine either way.

The 500 server error generally means there is something else going on. In the past whenever I get a 500 server error as opposed to other errors it means I will be making a telephone call to my hosting service provider. It often meant my hosting service was updating the databases or I had a problem with some sort of infinite loop or was trying to get PCRE to process more than it reasonably should. You may even be having trouble with your .htaccess file.

Try pruning your code down to as little as possible until the error no longer shows up so that you can pinpoint exactly what is causing the error to show up.

styxlawyer
12-17-2015, 04:01 PM
You may also wish to fix all the "</br>" tags. The correct format for the self-closing line break tag is "<br/>".

james438
12-17-2015, 04:39 PM
You may also wish to fix all the "</br>" tags. The correct format for the self-closing line break tag is "<br/>".


I have not heard of that. Just use <br>

See http://dev.w3.org/html5/html-author/#the-br-element