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