Jump to content

[SOLVED] mysql update problem


atticus

Recommended Posts

When I submit the form, I get my "company updated successfully message, but alas, it is not updated successfully.  I am not receiving any errors from the update mysql query.  I have tried to debug it by echoing the info from the form.  That info is there and being put into the variables. 

Form:

<?php
$result = mysql_query("SELECT * FROM company WHERE com_id='$_GET[com_id]' ") or die('Error, query failed : ' . mysql_error()); 
        while($myrow = mysql_fetch_assoc($result)) 
             {
                $id = $myrow['com_id']; 
                $name = $myrow['company_name'];
			$address1 = $myrow['address_1'];
			$address2 = $myrow['address_2'];
			$city = $myrow['city'];
			$state = $myrow['state'];
			$zip = $myrow['zip'];
			$phone = $myrow['phone'];
?>
<br>
<h3>Edit Company Profile :: <?php echo $name; ?> </h3>
<form method="post" action="<?php echo $PHP_SELF ?>">
<input type="hidden" id="com_id" name="com_id" value="<? echo $id; ?>">
Name: <input type="text" name="company_name" size="40" id="company_name" maxlength="255" value="<? echo $name; ?>">
<br>
Address 1: <input type="text" name="address_1" size="40" id="address_1" maxlength="255" value="<? echo $address1; ?>">
<br>
Address 2: <input name="address_2" type="text" size="40" id="address_2" maxlength="255" value="<? echo $address2; ?>">
<br>
City: <input name="city" type="text" size="40" id="city" maxlength="255" value="<? echo $city; ?>">
<br>
State: <input name="state" type="text" size="40" id="state" maxlength="255" value="<? echo $state; ?>">
<br>
Zip: <input name="zip" type="text" size="40" id="zip" maxlength="255" value="<? echo $zip; ?>">
<br>
Phone: <input name="phone" type="text" size="40" id="phone" maxlength="255" value="<? echo $phone; ?>">

<input type="submit" name="submit" value="Update">
</form>

 

The query for the form:

<?php
if(isset($_POST['submit']))

  {
  $id = mysql_escape_string($_POST['com_id']);
      $name = mysql_escape_string($_POST['company_name']);
      $address1 = mysql_escape_string($_POST['address_1']);
	$address2 = mysql_escape_string($_POST['address_2']);
	$city = mysql_escape_string($_POST['city']);
	$state = mysql_escape_string($_POST['state']); 
	$zip = mysql_escape_string($_POST['zip']);
	$phone = mysql_escape_string($_POST['phone']);


mysql_query("
UPDATE company SET company_name = '$name',
address_1 = '$address1',
address_2='$address2',
city = '$city',
state = '$state',
zip = '$zip',
phone = '$phone' 
WHERE com_id='.$_GET[com_id]'") 
or die('Error, query failed : ' . mysql_error());          

echo "<b>Thank you! Company UPDATED Successfully!<br />You will be redirected in 4 seconds";
          echo "<meta http-equiv=Refresh content=4;url=companies.php>";
}

Thanks

Link to comment
Share on other sites

Your where clause has an extra dot in it before the $_GET variable that is probably causing it to not match anything in your database -

 

WHERE com_id='[color=red].[/color]$_GET[com_id]'

 

Are your sure $_GET[com_id] has a value in your query, because your form is not setting any $_GET[com_id] variable.

 

You should also be using the mysql_real_escape_string() function and you should use it on the $_GET variable in your query to protect against sql injection.

Link to comment
Share on other sites

thanks.  The "." was the problem.  However I am still having an issue.  All of the input fields are working except for 'company_name'.  when I echo $name I get the data from the form.  But its not updating in the database.  No errors.

 

 

Link to comment
Share on other sites

I thought my problem was solved.  But when I update the record in the first row, there is no problem.  However, for other rows it doesn't update the database.  No mysql errors are being reported.  The info. in the form is correct, and I echoed the information from the form so I know my data is good going into the update query.  Any suggestions?

Link to comment
Share on other sites

Are you sure $_GET[com_id] has a value in your query, because your form is not setting any $_GET[com_id] variable.

 

Your form also has <?php echo $PHP_SELF ?> in the action="..." parameter. $PHP_SELF only has a value when register globals are on. Use <?php echo $_SERVER['PHP_SELF']; ?> instead.

 

Your code also using both a full <?php tag and a short <? tag. Be consistent and always use a full <?php tag so that your code will work no matter what the server setting is.

Link to comment
Share on other sites

 

 

Your form also has <?php echo $PHP_SELF ?> in the action="..." parameter. $PHP_SELF only has a value when register globals are on. Use <?php echo $_SERVER['PHP_SELF']; ?> instead.

 

Your code also using both a full <?php tag and a short <? tag. Be consistent and always use a full <?php tag so that your code will work no matter what the server setting is.

 

Thanks...you were right about the PHP_SELF in the action=.  I have the GET ID in the IF function which I did not include in the original posting,(trying to comply with forum rules about entire scripts).  That fixed my problem.  I will take your advice on standardizing all of my php opening brackets.  Thanks again

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.