banditbirds Posted April 4, 2014 Share Posted April 4, 2014 Hi people, I'm trying to update a database table by submitting a form. I have a template file which has a simple html form. <form action="insert.php" method="post"> Request Details: <input type="text" name="request_details_form"> <input type="submit"> </form> When I submit the form I run the following code in my insert.php <?php if(isset($_POST['submit'])) { $name = Tools::getValue('request_details_form'); Db::getInstance()->insert('custom_order', array( 'id_custom_order' => (int) 1, 'request_details' => pSQL($name), )); header('Location: http://www.banditbirds.co.uk'); } ?> I'm using the Tools::getValue instead of $_POST. But when I submit the form, it's not updating the data in the database table. Does anyone know what I'm doing wrong here? It also then directs me to the 404 page. Reading another forum post, I was hoping I could direct the user to the home page using the above 'header()'. Is there a better way of doing this? Thanks a lot guys, Simon Link to comment Share on other sites More sharing options...
netplayer Posted April 6, 2014 Share Posted April 6, 2014 I think you should comment out the redirect link and try again. Post and redirect are competing server side with re-direction winning but in the end you get 404 because actually php headers have already been submitted with post request so neither of the two executes. A way to accomplish both is to call the db function through ajax request and after result is returned to client, redirect using javascript. Using Jquery: jQuery.post("myfunction.php",{name:name}, function (result){ if(result){ location.href('http://www.banditbirds.co.uk'); } }); myfunction.php contains the server side thing that probably would echo back some conditional variable indicating success or failure of function. Link to comment Share on other sites More sharing options...
banditbirds Posted April 6, 2014 Author Share Posted April 6, 2014 Thanks for the advice! I may have misunderstood, but this is what I've tried: display.tpl : Welcome to the Shop! <form action="insert.php" method="post"> Request Details: <input type="text" name="request_details_form"> <input type="submit"> </form> insert.php : <?php jQuery.post("myfunction.php",{name:name}, function (result){ if(result){ location.href('http://www.banditbirds.co.uk'); } }); ?> linked to myfunction.php : <?php $name = Tools::getValue('request_details_form'); Db::getInstance()->insert('custom_order', array( 'id_custom_order' => (int) 1, 'request_details' => pSQL($name), )); ?> I'm still getting a 404 page error. But I was getting this even before I put in the db code...I think it's something to do with submitting the form on the display.tpl Any ideas?? Thanks again! Simon Link to comment Share on other sites More sharing options...
netplayer Posted April 6, 2014 Share Posted April 6, 2014 display.tpl: Welcome to the Shop! <form > Request Details: <input type="text" id="request_details_form" > <input type="submit" id="submit"> </form> <script> {literal} //used to distinguish Js scripts from smarty template scripts. $('#submit').click(function(){ //triggers when submit button is clicked var name=$('#request_details_form').val(); //gets the text field value $.post("insert.php",{name:name}, //sends the text field data to insert.php function (result){ //executes only if data are successfully submited and processed by insert.php //result variable takes the value of insert.php output e.g //if insert.php ends with echo "ok" result will be "ok" //so you could use: if(result=='ok'){ ...code here..}. //For now in order to redirect unconditionally use straight: location.href('http://www.banditbirds.co.uk'); }); }); {/literal} </script> What this does is post data to the server function and executes redirection after it has been executed. Pay attention there have to be id's in the elements. Also i noticed now that in your previous implementation you didnt define a name="submit" to your submit button which is the reason that the form post wasn't triggered. Now form doesnt need to have any method and action as long data are posted via ajax (in fact you don't need to have a form element at all). Next, all you need is insert.php insert.php: <?php $name = Tools::getValue('name'); //gets the value 'name' submited on .tpl Db::getInstance()->insert('custom_order', array( 'id_custom_order' => (int) 1, 'request_details' => pSQL($name), )); //whatever you echo here assigns value to the result variable on .tpl function(result) ?> Hope it works for you 1 Link to comment Share on other sites More sharing options...
banditbirds Posted April 24, 2014 Author Share Posted April 24, 2014 Thanks for the help, and the explanations netplayer. Much appreciated. I tried your examples. It did re-direct correctly after submitting the form...but it hasn't inserted the data in my database :/ Have you got any ideas why this isn't happening? Or how I can debug it? Thanks!!!! Simon Link to comment Share on other sites More sharing options...
netplayer Posted April 24, 2014 Share Posted April 24, 2014 Check your sql query. See Prestashop documentation here http://doc.prestashop.com/display/PS15/DB+class+best+practices For sure that $psql(name) is not correct, depending on what you want to do, just adopt the corresponding query format in documentation. Link to comment Share on other sites More sharing options...
banditbirds Posted April 25, 2014 Author Share Posted April 25, 2014 Thanks netplayer! I took the code from the documentation: $target = Tools::getValue('id'); $name = Tools::getValue('name'); Db::getInstance()->insert('target_table', array( 'id_target' => (int)$target, 'name' => pSQL($name), )); Their comment regarding pSQL: Make sure that your data is always checked and protected when doing an insertion. In our example, we want to make sure that we do have an integer with an explicit (int) cast, and that the name is protected against SQL injections thanks to the pSQL() method. 'id_custom_order' I want to be an auto-incremented Id, so I'm thinking I shouldn't be inserting this with the array anyway? Or do I need to keep it in the array...but with a blank value? I've set the field to AUTO_INCREMENT on the Db. Without the pSQL method, I guess I should be able to just use: <?php $name = Tools::getValue('name'); Db::getInstance()->insert('custom_order', array( 'request_details' => $name, )); ?> Thanks again!! Link to comment Share on other sites More sharing options...
karthiiiiiiiiiik Posted April 28, 2014 Share Posted April 28, 2014 Hi bandit, You can use insert query like this : $variablename1 = "INSERT INTO`"._DB_PREFIX_."your table name` (`column1`,`column2` etc) VALUES('{$variable1}','{variable}'); "; $variable2name = Db::getInstance()->Execute($variablename1); you can also use die() to check the query has executed properly Link to comment Share on other sites More sharing options...
banditbirds Posted April 28, 2014 Author Share Posted April 28, 2014 Thanks karthik! I tried this, but it still doesn't work. I don't even get the error message from the die(). I wonder if the .tpl is not loading the php code at all? <?php $name = Tools::getValue('name'); $variablename1 = "INSERT INTO`"._DB_PREFIX_."custom_order` ('request_details`) VALUES('{$name}'); "; if (!Db::getInstance()->execute($variablename1)) die('Error etc.)'; ?> Thanks! Link to comment Share on other sites More sharing options...
karthiiiiiiiiiik Posted April 28, 2014 Share Posted April 28, 2014 (edited) In which file you were using the insert query in tpl file or php file ? whether the table custom_order has exist in your database..? Edited April 28, 2014 by karthik who_am_i (see edit history) Link to comment Share on other sites More sharing options...
banditbirds Posted April 28, 2014 Author Share Posted April 28, 2014 Hi karthik, Here is my code, php: <?php $name = Tools::getValue('name'); $variablename1 = "INSERT INTO`"._DB_PREFIX_."custom_order` ('request_details`) VALUES('{$name}'); "; if (!Db::getInstance()->execute($variablename1)) die('Error etc.)'; ?> Which is being called from my tpl: Please enter you custom request details here: <form > Request Details: <input type="text" id="request_details" > <input type="submit" id="submit"> </form> <script> {literal} $('#submit').click(function(){ var name=$('#request_details').val(); $.post("insert.php",{name:name},function (result){ location.href('http://www.banditbirds.co.uk'); }); }); {/literal} </script> And here is my table - custom_order (image attached) Thanks! Link to comment Share on other sites More sharing options...
karthiiiiiiiiiik Posted April 28, 2014 Share Posted April 28, 2014 hi think, You can try like this <form name="formname" method="post" action="{$link->getModuleLink(modulename,'phpfilename with out .php extension')}" > </form> where is your template located , are you generating a module ? Link to comment Share on other sites More sharing options...
banditbirds Posted May 6, 2014 Author Share Posted May 6, 2014 (edited) Thanks for the reply. My tpl file is in: /public_html/modules/blockquote/views/templates/front/ And yes, I'm generating a module using the tutorial from prestashop. Am I using your form statement to replace the script? For example: Please enter your custom request details here: <form name="formname" method="post" action="{$link->getModuleLink(blockquote,'insert.php')}" > Request Details: <input type="text" id="request_details_form" > <input type="submit" id="submit"> </form> And I'd have to go back to getting my name variable in insert.php from the form: $name = Tools::getValue('request_details_form'); If the above is correct, then it's still not working. And with this I don't have the redirect to the home page( from the script function). Thanks!! Simon Edited May 6, 2014 by banditbirds (see edit history) Link to comment Share on other sites More sharing options...
srinivasgoud Posted February 26, 2015 Share Posted February 26, 2015 I want to display a id, name, price and i have to edit the fields and it has to be updated in the database, how can i write the functionality.. please help me Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now