esscher Posted February 29, 2008 Share Posted February 29, 2008 The mysql insertion phase of my application is not working as expected. For some reason, when it comes time to insert data, the variables holding the data lose their value. For example, here is the php page that does the insertion: <?php include ("privatedbconfig.php"); include ("dbconnection.php"); echo $_POST['comname']; $_POST['comname'] = $comname; echo $comname; mysql_query("insert into plants set common_name='$comname'"); echo mysql_error(); ?> the echo of $comname works just fine, but when during the insert query it does not hold a value.. i know this because when i check the table to see if it has been inserted, it shows that a row has been added, but the column has no value. phpmyadmin shows that the null value normally there has been replaced with "" (nothingness) here is the first page of code, demo_add_plant.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script language="javascript" type="text/javascript" src="../prototype-1.6.0.2.js"></script> <script language="javascript"> function getData() { var url="add_plant.php" var pars="id="+document.getElementById("Plant2Check").value; var target="PlantInfoDiv"; var myAjax=new Ajax.Updater(target, url, {method: 'post', parameters: pars, evalScripts: true}); } function postForm() //prototype is used here instead of plain javascript { var url2="insert.php" var pars2="comname="+document.getElementById("comname").value; var target2="putItHere"; var myAjax2=new Ajax.Updater(target2, url2, {method: 'post', parameters: pars2}); } </script> <title>An Ajax Call using Prototype</title> </head> <body> <form> Add Plant Profile (type anything):<input id="Plant2Check" type="text" name="inputplant" value=""> <p><input type="button" value="Click 4 Plant Profile Form" onClick="getData()" /> </form> <div id="PlantInfoDiv"><b></b></div> </body> </html> and the second page, add_plant.php <?php include ("privatedbconfig.php"); include ("dbconnection.php"); ?> <form id="formid" name="formname"> <table border="1" cellpadding="3" cellspacing="0"> <tr> <td align="center" bgcolor="#FFCC00"><strong>Property</strong></td> <td align="center" bgcolor="#FFCC00"><strong>What to Insert</strong></td> </tr> <tr> <td bgcolor="#FFFFCC"><?php echo "Common Name"; ?></td> <td bgcolor="#FFFFCC"><input id="comname" name="common_name" type="text" value="<?php echo $_POST['id'] ?>"></td> </tr> </table> <input type="button" name="submit_plant_profile" onClick="postForm()" value="Add Info to DB" /> </form> <div id="putItHere">This should change when the data has been inserted</div> I have simplied my code as much as possible to eliminate any stray errors or whatnot. the form normally would have more than one text input. and here is the link to the app itself - http://66.87.141.7/phytobase/p2/simple/demo_add_plant.php any ideas? Esscher Quote Link to comment Share on other sites More sharing options...
Northern Flame Posted February 29, 2008 Share Posted February 29, 2008 your SQL was wrong, try this: mysql_query("insert into plants (common_name) VALUES('$comname')"); Quote Link to comment Share on other sites More sharing options...
esscher Posted February 29, 2008 Author Share Posted February 29, 2008 It turns out that it was this line: $_POST['comname'] = $comname; $comname never did get the correct value.. it always stayed at "" because the line is backwards... a result of coding 10 hours that day. Obviously it should be: $comname = $_POST['comname']; Following that correction it did work. The syntax I've always used to insert is what I used in my code. I'm not sure if mysql allows another way to insert such as the example you gave? anyway, thanks for the suggestion Esscher Quote Link to comment Share on other sites More sharing options...
Northern Flame Posted March 1, 2008 Share Posted March 1, 2008 oh well the example i gave is how i always insert into databases so i guess theres more than one way and dont worry, i too have made the mistake you made Quote Link to comment 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.