etdsbastar Posted August 24, 2011 Share Posted August 24, 2011 hello there, Please help.. This code is not working: <?php if(isset($_POST['btnsave'])=="Save") { $name = $_POST['name']; $db = mysql_connect("localhost", $dbuser, $dbpass); if(!$db) die("Error connecting to MySQL database."); mysql_select_db("npms-2011", $db); $sql = "INSERT INTO test (name) VALUES (". $name .")"; mysql_query($sql); exit(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/245602-please-help-this-code-is-not-working/ Share on other sites More sharing options...
phpsycho Posted August 24, 2011 Share Posted August 24, 2011 if(isset($_POST['btnsave']) && $_POST['btnsave'] =="Save") Quote Link to comment https://forums.phpfreaks.com/topic/245602-please-help-this-code-is-not-working/#findComment-1261438 Share on other sites More sharing options...
etdsbastar Posted August 24, 2011 Author Share Posted August 24, 2011 No luck dear, any other solutions. Here is my form code and button code: <form name="wardnewform" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" onsubmit="return ValidateFormWardsNew()"> <input type="submit" name="btnsave" value="Save" /> Quote Link to comment https://forums.phpfreaks.com/topic/245602-please-help-this-code-is-not-working/#findComment-1261442 Share on other sites More sharing options...
phpsycho Posted August 24, 2011 Share Posted August 24, 2011 so then all you need is if(isset($_POST['btnsave'])) Quote Link to comment https://forums.phpfreaks.com/topic/245602-please-help-this-code-is-not-working/#findComment-1261445 Share on other sites More sharing options...
phpsycho Posted August 24, 2011 Share Posted August 24, 2011 well what exactly isn't working? any errors? or is it just blank screen? Quote Link to comment https://forums.phpfreaks.com/topic/245602-please-help-this-code-is-not-working/#findComment-1261447 Share on other sites More sharing options...
etdsbastar Posted August 24, 2011 Author Share Posted August 24, 2011 no errors nothing, just the field value is not saving in the table. Quote Link to comment https://forums.phpfreaks.com/topic/245602-please-help-this-code-is-not-working/#findComment-1261448 Share on other sites More sharing options...
etdsbastar Posted August 24, 2011 Author Share Posted August 24, 2011 so then all you need is if(isset($_POST['btnsave'])) Thanks a lot, that solved my problem. Will you please tell me that I want to show a information window after saving the data, how can i do that? Quote Link to comment https://forums.phpfreaks.com/topic/245602-please-help-this-code-is-not-working/#findComment-1261455 Share on other sites More sharing options...
phpsycho Posted August 24, 2011 Share Posted August 24, 2011 You want to just display the posted content? like name? just echo it out before you exit. echo $name; Quote Link to comment https://forums.phpfreaks.com/topic/245602-please-help-this-code-is-not-working/#findComment-1261458 Share on other sites More sharing options...
etdsbastar Posted August 24, 2011 Author Share Posted August 24, 2011 You want to just display the posted content? like name? just echo it out before you exit. echo $name; One more problem occured just now, only numbers are being stored in the table, If i enter some letter, it is not stored in the name field. Why is so? And this is what happening when I am putting the echo command. The code is executing before the button press. One more thing, On every page refresh a new record is being created in the table. [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/245602-please-help-this-code-is-not-working/#findComment-1261462 Share on other sites More sharing options...
phpsycho Posted August 24, 2011 Share Posted August 24, 2011 you are most likely using int. Use varchar(255) UTF-8 if(isset($_POST['btnsave'])){ //insert into db }else{ //display form } Quote Link to comment https://forums.phpfreaks.com/topic/245602-please-help-this-code-is-not-working/#findComment-1261465 Share on other sites More sharing options...
etdsbastar Posted August 24, 2011 Author Share Posted August 24, 2011 you are most likely using int. Use varchar(255) UTF-8 if(isset($_POST['btnsave'])){ //insert into db }else{ //display form } One more thing, On every page refresh a new record is being created in the table. Quote Link to comment https://forums.phpfreaks.com/topic/245602-please-help-this-code-is-not-working/#findComment-1261466 Share on other sites More sharing options...
etdsbastar Posted August 24, 2011 Author Share Posted August 24, 2011 Here is the complete code: I think i am missing something. The following are the problems: 1. On every refresh a new record is being created. 2. Only numbers are being saved in the table record. 3. $echo is executed before the button press. 4. No luck for VARCHAR(255), instead I am using TEXT, still unable to save text entries in the field Please help... <link rel=stylesheet HREF="/css/wards.css" type="text/css" media="screen"> <?php include '../header.php'; ?> <?php include '../config/config.php'; ?> <?php include '../functions/functions.php'; ?> <?php extract($GLOBALS); ?> <div id="content"> <div id="labelopt"><div class="box"></div> jax ds lHkh dkWye vfuok;Z gSa A 'kwU; ls Hkjs gq;s fdlh Hkh dkWye dks [kkyh ugha NksM+sa] vxj dksbZ izfo"Bh ugha gks rks 0 M+kysa A</div> <div id="labelopt"></div> <h2>u;s okMZ dk la;kstu</h2> <div id="dp-sp"> <form name="wardnewform" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" onsubmit="return ValidateFormWardsNew()"> <table border="1"> <tr> <td id="labelmust">okMZ dzekad %</td> <td><input class="english" type="text" size="20" name="wardno" maxlength="40" /> <td id="labelmust">okMZ dk uke %</td> <td><input class="hindi" type="text" size="30" name="name" maxlength="30" /> <td id="labelmust">t+ksu dzekad %</td> <td colspan="3"><input class="english" type="text" size="10" name="zone" maxlength="4" /> </tr> </table> <table cellspacing="0" cellpadding="2"> <tr> <th>vkoklh; ok"kZd HkkM+s dk C;ksjk</th> <th>fuekZ.k oxZ 01</th> <th>fuekZ.k oxZ 02</th> <th>fuekZ.k oxZ 03</th> </tr> <tr> <th id="thhead">eq[; lM+d ;k ckt+kj ij</th> <td align="center"><input class="english" type="text" size="10" name="a1" maxlength="5" value="0" /> <td align="center"><input class="english" type="text" size="10" name="b1" maxlength="5" value="0" /> <td align="center"><input class="english" type="text" size="10" name="c1" maxlength="5" value="0" /> </tr> <tr> <th id="thhead">eq[; lM+d ;k ckt+kj ls gVdj</th> <td align="center"><input class="english" type="text" size="10" name="a2" maxlength="5" value="0" /> <td align="center"><input class="english" type="text" size="10" name="b2" maxlength="5" value="0" /> <td align="center"><input class="english" type="text" size="10" name="c2" maxlength="5" value="0" /> </tr> <tr> <th>O;kolkf;d@vkS|ksfxd ok"kZd HkkM+s dk C;ksjk</th> </tr> <tr> <th id="thhead">eq[; lM+d ;k ckt+kj ij</th> <td align="center"><input class="english" type="text" size="10" name="a3" maxlength="5" value="0" /> <td align="center"><input class="english" type="text" size="10" name="b3" maxlength="5" value="0" /> <td align="center"><input class="english" type="text" size="10" name="c3" maxlength="5" value="0" /> </tr> <tr> <th id="thhead">eq[; lM+d ;k ckt+kj ls gVdj</th> <td align="center"><input class="english" type="text" size="10" name="a4" maxlength="5" value="0" /> <td align="center"><input class="english" type="text" size="10" name="b4" maxlength="5" value="0" /> <td align="center"><input class="english" type="text" size="10" name="c4" maxlength="5" value="0" /> </tr> </table> </div> <div id="menu"> <table> <tr> <td> <input type="submit" name="btnsave" value="Save" /> </form> <input type="button" value="View List" name="btnwardslist" onclick="location.href='/sp/spnew.php';" /> <input type="button" value="Back" name="btnspback" onclick="location.href='/wards.php';" /> </td> </tr> </table> </div> </div> <?php include '../footer.php'; ?> <?php if(isset($_POST['btnsave'])) { $name = $_POST['name']; $db = mysql_connect("localhost", $dbuser, $dbpass); if(!$db) die("Error connecting to MySQL database."); mysql_select_db("npms-2011", $db); $sql = "INSERT INTO test (name) VALUES (". $name .")"; mysql_query($sql); mysql_close($db); echo "successfully saved:".$name; exit(); } else { } /* $querys = "insert into wards (wardno, name, zone, a1, a2, a3, a4, b1, b2, b3, b4, c1, c2, c3, c4) values (wardno, name, zone, a1, a2, a3, b1, b2, b3, c1, c2, c4, d1, d2, d3, d4)"; */ ?> Quote Link to comment https://forums.phpfreaks.com/topic/245602-please-help-this-code-is-not-working/#findComment-1261468 Share on other sites More sharing options...
phpsycho Posted August 24, 2011 Share Posted August 24, 2011 thats because when you refresh you re-execute the post if(mysql_num_rows(mysql_query("SELECT * FROM `table` WHERE `name`='$clean_var_name'")) > 0){ echo "This record already exists"; }else{ //insert into db } Quote Link to comment https://forums.phpfreaks.com/topic/245602-please-help-this-code-is-not-working/#findComment-1261469 Share on other sites More sharing options...
etdsbastar Posted August 24, 2011 Author Share Posted August 24, 2011 No luck for VARCHAR(255), instead I am using TEXT, still unable to save text entries in the field Quote Link to comment https://forums.phpfreaks.com/topic/245602-please-help-this-code-is-not-working/#findComment-1261470 Share on other sites More sharing options...
phpsycho Posted August 24, 2011 Share Posted August 24, 2011 I tested your code and it looks fine to me. what do you mean by "$echo is executed before the button press."? and varchar(255) should work... could I see your table structure? oh also, you need to clean your vars before inserting to db. mysql_real_escape_string() Quote Link to comment https://forums.phpfreaks.com/topic/245602-please-help-this-code-is-not-working/#findComment-1261471 Share on other sites More sharing options...
PFMaBiSmAd Posted August 24, 2011 Share Posted August 24, 2011 You cannot save any text because you don't have any single-quotes around the data in the query. Without the single-quotes, mysql is treating the data as a mysql keyword or column name. If you were checking if the query returned an error, you would have been getting sql syntax/unknown column name errors. $sql = "INSERT INTO test (name) VALUES ('$name')"; Quote Link to comment https://forums.phpfreaks.com/topic/245602-please-help-this-code-is-not-working/#findComment-1261473 Share on other sites More sharing options...
etdsbastar Posted August 24, 2011 Author Share Posted August 24, 2011 I tested your code and it looks fine to me. what do you mean by "$echo is executed before the button press."? and varchar(255) should work... could I see your table structure? oh also, you need to clean your vars before inserting to db. mysql_real_escape_string() Thanks for the reply. Here is my table's SQL: CREATE TABLE `npms-2011`.`test` ( `name` varchar(255) CHARACTER SET utf8 NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1 Even with TEXT type it is not working. Secondly, what is this mysql_real_escape_string(), how can i use it and where to put, please tell me clearly. Sorry for the problems I am a newbie. Quote Link to comment https://forums.phpfreaks.com/topic/245602-please-help-this-code-is-not-working/#findComment-1261474 Share on other sites More sharing options...
etdsbastar Posted August 24, 2011 Author Share Posted August 24, 2011 You cannot save any text because you don't have any single-quotes around the data in the query. Without the single-quotes, mysql is treating the data as a mysql keyword or column name. If you were checking if the query returned an error, you would have been getting sql syntax/unknown column name errors. $sql = "INSERT INTO test (name) VALUES ('$name')"; Thanks a lot, you all guys helped me a lot. That solved my problem. I hope you will surely in future help me for all my queries. Thanks a lot. I will continue on this thread only if I will get some more errors regarding my current project. Quote Link to comment https://forums.phpfreaks.com/topic/245602-please-help-this-code-is-not-working/#findComment-1261475 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.