codects Posted May 7, 2009 Share Posted May 7, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/157222-solved-fields-not-inserted-into-database/ Share on other sites More sharing options...
PFMaBiSmAd Posted May 7, 2009 Share Posted May 7, 2009 That is not a complete form, so it is likely that no form data is actually being submitted to the server. Your form <input fields also don't have any type="..." parameters. Quote Link to comment https://forums.phpfreaks.com/topic/157222-solved-fields-not-inserted-into-database/#findComment-828414 Share on other sites More sharing options...
codects Posted May 7, 2009 Author Share Posted May 7, 2009 Thank you ... The form is complete, this was just an excerpt: <form action="xxxx.php" method="post"> </form> It creates entries in the database, but they're all just empty. Quote Link to comment https://forums.phpfreaks.com/topic/157222-solved-fields-not-inserted-into-database/#findComment-828427 Share on other sites More sharing options...
kickstart Posted May 7, 2009 Share Posted May 7, 2009 Hi Put this in the code and see what is echoed out. foreach ($_REQUEST as $field => $value) echo "$field $value </br>"; Lets see what fields are returned with the form. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/157222-solved-fields-not-inserted-into-database/#findComment-828431 Share on other sites More sharing options...
revraz Posted May 7, 2009 Share Posted May 7, 2009 Echo $sql. Quote Link to comment https://forums.phpfreaks.com/topic/157222-solved-fields-not-inserted-into-database/#findComment-828441 Share on other sites More sharing options...
codects Posted May 7, 2009 Author Share Posted May 7, 2009 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'? Quote Link to comment https://forums.phpfreaks.com/topic/157222-solved-fields-not-inserted-into-database/#findComment-828505 Share on other sites More sharing options...
Ken2k7 Posted May 7, 2009 Share Posted May 7, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/157222-solved-fields-not-inserted-into-database/#findComment-828514 Share on other sites More sharing options...
PFMaBiSmAd Posted May 7, 2009 Share Posted May 7, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/157222-solved-fields-not-inserted-into-database/#findComment-828524 Share on other sites More sharing options...
codects Posted May 7, 2009 Author Share Posted May 7, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/157222-solved-fields-not-inserted-into-database/#findComment-828548 Share on other sites More sharing options...
Ken2k7 Posted May 7, 2009 Share Posted May 7, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/157222-solved-fields-not-inserted-into-database/#findComment-828552 Share on other sites More sharing options...
PFMaBiSmAd Posted May 7, 2009 Share Posted May 7, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/157222-solved-fields-not-inserted-into-database/#findComment-828559 Share on other sites More sharing options...
codects Posted May 7, 2009 Author Share Posted May 7, 2009 Opps, yeah - they're supposed to be numbered 1, 2, ... etc 'eventually'. Trying first to get the first entry in the db .... Quote Link to comment https://forums.phpfreaks.com/topic/157222-solved-fields-not-inserted-into-database/#findComment-828560 Share on other sites More sharing options...
codects Posted May 7, 2009 Author Share Posted May 7, 2009 That is just soooooooooooo obvious i feel <no words> ... Thank you so very much! Quote Link to comment https://forums.phpfreaks.com/topic/157222-solved-fields-not-inserted-into-database/#findComment-828561 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.