Jump to content

Recommended Posts

I'm going batty trying to find two problems in this small bit of code. I'm hoping that someone will point out the typo or whatever at best, or at worse tell me that the error is somewhere else (though I have no idea where else it could be).

 

One problem is that one of the values in a form is not being posted: in the code/results below, you can see that the variable $_POST['weight'] corresponding to the form field named "weight" is set in the $_POST array, but there's nothing being echoed—even when there is something entered into the weight field in the form.

 

The other problem is the syntax error MySQL v5.0 is returning on the INSERT statement. It's such a simple statement that I can't believe this is the real problem. Typo? Reserved word?

 

(Also, this is not my own code, I'm helping someone learn PHP/MySQL, and I'm dreading the idea that there is something wrong with some distant part of the spaghetti).

 

Thanks for looking over the code below and I appreciate whatever thoughts you can offer.

 

 

This is the code for the form—with nonessential stuff removed:

<form name="member_metrics_weight_add" method="post" action="member_metrics_weight.php">
  <table width="500" align="center">
...
<tr>
      <td colspan="2">Weight</td>
      <td width="538">Goal Weight </td>
</tr>
<tr>
      <td height="35" colspan="2" valign="top"><input type="text" name="weight" class="form_fields" id="weight" value="<?PHP echo $weight;?>" size="30" maxlength="30">
      </td>
      <td height="35" colspan="2" valign="top"><input type="text" name="goal_weight" class="form_fields" id="goal_weight" value="<?php echo $goal_weight;?>" size="30" maxlength="30">
</tr>

 

And here is the PHP code that processes that part of the form:

//member_id is already retrieved from session variables

if (isset($_POST['submit'])) {

//the following is just trying to find the error, why the $_POST['weight'] is set, but empty, even though the field was filled out
if (isset($_POST['weight'])) {
	echo("<p>weight is set</p><p>weight: ".$_POST['weight']."</p>");
}
else {
	echo("<p>weight is NOT set</p>");
}
//real code starts here
$weight = $_POST['weight'];
$goal_weight = $_POST['goal_weight'];

$e_sql="	INSERT INTO members_metrics(member_id,weight,goal_weight) VALUES($member_id,$weight,$goal_weight)";
echo("<p>".$e_sql."</p>"); //more bug seeking code
$e_results = mysql_query($e_sql) or die(mysql_errno()." ".mysql_error());
}

 

And the output from the PHP code is the following:

weight is set

weight:

INSERT INTO members_metrics(member_id, weight, goal_weight) VALUES (383, , 456)

1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' 456)' at line 2

 

 

And, just to be sure, here's the relevant bits of the members_metrics table:

id                  int(11)                      No                auto_increment

member_id    varchar(255)    utf8_general_ci      No

weight            varchar(255)    utf8_general_ci      Yes    NULL

goal_weight    varchar(255)    utf8_general_ci      Yes    NULL

 

Link to comment
https://forums.phpfreaks.com/topic/239112-going-crazy-looking-for-mysql-error/
Share on other sites

Well all your problems stem from $_POST['weight'] being an empty string. I don't see any real problems in your code that would make $_POST['weight'] unset.

 

Try doing

print_r($_POST);

 

at the top of the page and see what it outputs. Other than that, your form and code all seem fine

Most types of form fields are set, even if they are empty.

 

As to your weight field, you either have another field in the form by that same name later in the form or the markup of your actual form is invalid thereby making the weight field invalid or you have some javascript in the form that is overwriting the field value or you have some php code in your form processing code that is overwriting the value.

 

The sql error is because your weight value is missing from the query (you should not execute the query if the validation of the values being put into the query has failed with an empty value.)

THAT'S IT!!

 

It turns out the EVERY field in the whole darn form was named "weight" except the one named "goal_weight", which were the only two I was using for testing.

 

Fixing other people's code is harder than writing your own.

 

Thanks!

 

 

 

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.