Jump to content

[SOLVED] Fields not inserted into database


codects

Recommended Posts

Hi,

 

I have a php/mysql form - but the field values are not being inserted into the database, although there are no errors.

Here is the code for the form i use:

 

<tr>
    <td><select id="contaminant" name="contaminant">
      <option>select </option>
      <option>benzo(a)pyrene </option>
      <option>naphthalene </option>
      <option>benzene </option>
      <option>ethylbenzene </option>
      <option>toluene </option>
      <option>xylene </option>
    </select></td>
    <td><input id="soil" name="soil" /></td>
    <td><input id="soilgas" name="soilgas" /></td>
    <td><input id="gwater" name="gwater" /></td>
    <td><input id="swater" name="swater" /></td>
</tr>

 

and the associated php code:<?php

$con = mysql_connect("localhost","xxxx","xxxx");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("xxxx", $con);


$sql="INSERT INTO xxxx (pid,contaminant,soil, soilgas, gwater, swater)
VALUES ('$_POST[pid]','$_POST[contaminant]','$_POST[soil]','$_POST[soilgas]','$_POST[gwater]', '$_POST[swater]')";


if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }

echo header("Location: index.php");

mysql_close($con)
?>

 

and the SQL code

 

+-------------+---------------+------+-----+---------+----------------+

| Field      | Type          | Null | Key | Default | Extra          |

+-------------+---------------+------+-----+---------+----------------+

| pid        | int(11)      | NO  | PRI | NULL    | auto_increment |

| contaminant | varchar(30)  | NO  |    | NULL    |                |

| soil        | decimal(10,6) | NO  |    | NULL    |                |

| soilgas    | decimal(10,6) | NO  |    | NULL    |                |

| gwater      | decimal(10,6) | NO  |    | NULL    |                |

| swater      | decimal(10,6) | NO  |    | NULL    |                |

+-------------+---------------+------+-----+---------+----------------+

 

 

I don't know what i'm missing - please help?

Link to comment
Share on other sites

Hi guys, thanks for the replies

 

I added and it's still creating empty entries ...

 

The thing is i used the same code for a different page - and it works just fine, could it be because this is in a table and the code is somehow 'confused'?

 

 

Link to comment
Share on other sites

About 85% of all problems come from people not knowing how to interpolate an array variable into a string. What's wrong with concatenating string rather than putting it directly into the string? I mean it would save you a heck loads of trouble in the long run. What I mean is instead of something like this:

 

echo "here's some static text and here's a $variable";

 

Do this:

echo "here's some static text and here's a " . $variable

 

See the difference. Now with variables that aren't arrays, it's fine, but people just don't know how to deal with arrays when interpolating with strings. So.. just use the second method because that will always work.

 

One more thing, with associative arrays, you call them like this:

$array['e'];

 

NOT like this:

$array[e];

 

See the difference? If you just use the second method, then PHP is treating the "e" as a constant variable. I doubt you defined it as a constant so the second way fails.

 

Now back to your code.

 

You have this -

$sql="INSERT INTO xxxx (pid,contaminant,soil, soilgas, gwater, swater)
VALUES ('$_POST[pid]','$_POST[contaminant]','$_POST[soil]','$_POST[soilgas]','$_POST[gwater]', '$_POST[swater]')";

 

2 things. Use string concatenations and use put quotes around the keys!

 

$sql="INSERT INTO xxxx (pid,contaminant,soil, soilgas, gwater, swater)
VALUES ('" . $_POST['pid'] . "','" . $_POST['contaminant'] . "','" . $_POST['soil'] . "','" . $_POST['soilgas'] . "','" . $_POST['gwater'] . "', '" . $_POST['swater'] . "')";

 

There! Understand everything I did? If not, read up on PHP basics. :)

Link to comment
Share on other sites

The code suggestions prior to your last post were to show what if any data your code was receiving (which we know it is not since rows with empty data are being inserted.)

 

It's likely that your form is invalid. Posting your complete form would allow someone to determine if it was the reason why no data is received on the form processing page.

Link to comment
Share on other sites

Oh ok ... thanks.

 

Here is the full form

 

<form action="ixxx.php" method="POST">
<table width="84%"  border="0">
  <tr>
    <td width="24%">CONTAMINANT</td>
    <td width="19%">SOIL</td>
    <td width="19%">SOIL GAS </td>
    <td width="19%">GROUNDWATER</td>
    <td width="19%">SURFACE WATER</td>
  </tr>
<tr>
    <td><select id="contaminant" name="contaminant">
      <option>select </option>
      <option value="benzo(a)pyrene">benzo(a)pyrene </option>
      <option value="naphthalene">naphthalene </option>
      <option value="benzene">benzene </option>
      <option value="ethylbenzene">ethylbenzene </option>
      <option value="toluene">toluene </option>
      <option value="xylene">xylene </option>
    </select></td>
    <td><input type="text" id="soil" name="soil" /></td>
    <td><input type="text" id="soilgas" name="soilgas" /></td>
    <td><input type="text" id="gwater" name="gwater" /></td>
    <td><input type="text" id="swater" name="swater" /></td>
</tr>
  <tr>
    <td><select id="contaminant" name="contaminant">
      <option>select </option>
      <option>benzo(a)pyrene </option>
      <option>naphthalene </option>
      <option>benzene </option>
      <option>ethylbenzene </option>
      <option>toluene </option>
      <option>xylene </option>
    </select></td>
    <td><input id="soil" name="soil" /></td>
    <td><input id="soilgas" name="soilgas" /></td>
    <td><input id="gwater" name="gwater" /></td>
    <td><input id="swater" name="swater" /></td>
  </tr>
<tr>
    <td><select id="contaminant" name="contaminant">
      <option>select </option>
      <option>benzo(a)pyrene </option>
      <option>naphthalene </option>
      <option>benzene </option>
      <option>ethylbenzene </option>
      <option>toluene </option>
      <option>xylene </option>
    </select></td>
    <td><input id="soil" name="soil" /></td>
    <td><input id="soilgas" name="soilgas" /></td>
    <td><input id="gwater" name="gwater" /></td>
    <td><input id="swater" name="swater" /></td>
  </tr>
<tr>
    <td><select id="contaminant" name="contaminant">
      <option>select </option>
      <option>benzo(a)pyrene </option>
      <option>naphthalene </option>
      <option>benzene </option>
      <option>ethylbenzene </option>
      <option>toluene </option>
      <option>xylene </option>
    </select></td>
    <td><input id="soil" name="soil" /></td>
    <td><input id="soilgas" name="soilgas" /></td>
    <td><input id="gwater" name="gwater" /></td>
    <td><input id="swater" name="swater" /></td>
  </tr>
<tr>
    <td><select id="contaminant" name="contaminant">
      <option>select </option>
      <option>benzo(a)pyrene </option>
      <option>naphthalene </option>
      <option>benzene </option>
      <option>ethylbenzene </option>
      <option>toluene </option>
      <option>xylene </option>
    </select></td>
    <td><input id="soil" name="soil" /></td>
    <td><input id="soilgas" name="soilgas" /></td>
    <td><input id="gwater" name="gwater" /></td>
    <td><input id="swater" name="swater" /></td>
  </tr>
<tr>
    <td><select id="contaminant" name="contaminant">
      <option>select </option>
      <option>benzo(a)pyrene </option>
      <option>naphthalene </option>
      <option>benzene </option>
      <option>ethylbenzene </option>
      <option>toluene </option>
      <option>xylene </option>
    </select></td>
    <td><input id="soil" name="soil" /></td>
    <td><input id="soilgas" name="soilgas" /></td>
    <td><input id="gwater" name="gwater" /></td>
    <td><input id="swater" name="swater" /></td>
  </tr>
<tr>
    <td><select id="contaminant" name="contaminant">
      <option>select </option>
      <option>benzo(a)pyrene </option>
      <option>naphthalene </option>
      <option>benzene </option>
      <option>ethylbenzene </option>
      <option>toluene </option>
      <option>xylene </option>
    </select></td>
    <td><input id="soil" name="soil" /></td>
    <td><input id="soilgas" name="soilgas" /></td>
    <td><input id="gwater" name="gwater" /></td>
    <td><input id="swater" name="swater" /></td>
  </tr>
  <tr>
    <td colspan="5"> </td>
    </tr>
  <tr>
    <td colspan="5">
<input type="submit" value="Save data" /> 
<input type="submit" value="Compare GAC" /> 
<input type="submit" value="Compare SSC" /></td>
    </tr>
  
</table>

</form>

Link to comment
Share on other sites

Is there a reason you have so many selects that have the same ID and option values?

 

Please know that IDs on a HTML page is unique and you should only have one with the same name. Up there, you have like 6 or something with the same ID - contaminant.

Link to comment
Share on other sites

The name parameter of each form field determines the name of the POST variable that data will be available as. All your same type form fields have the same name. Only the last form field with a same name will be submitted.

 

I recommend using arrays where the index indicates which row the field belongs with.

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.