jeeves245 Posted August 9, 2009 Share Posted August 9, 2009 Hi guys, quick question. I have this line of code (below) that is basically a text box that shows a value pulled from a database. The point of that is the user can change what's in the textbox and save it back to the database. However I can't figure out how to actually save it back to the database. Any ideas? echo "<td> <form action='process.php' method='post'><input type='text' name='estimatedDelivery' value=".$new_date2."></td>"; The $new_date2 var has a value from the database stored in it. So when the user changes what is in the textbox, I want it put in a different variable and sent back to the same field that $new_date2 came from. Cheers in advance. Quote Link to comment https://forums.phpfreaks.com/topic/169433-solved-database-field-in-textbox/ Share on other sites More sharing options...
The Little Guy Posted August 9, 2009 Share Posted August 9, 2009 so... when the submit button is pressed, $input = mysql_real_escape_string($_POST['estimatedDelivery']); mysql_query("UPDATE `table_name` SET `column_name` = '$input' WHERE `row_id` = 'some_value'"); Quote Link to comment https://forums.phpfreaks.com/topic/169433-solved-database-field-in-textbox/#findComment-893938 Share on other sites More sharing options...
jeeves245 Posted August 9, 2009 Author Share Posted August 9, 2009 so... when the submit button is pressed, $input = mysql_real_escape_string($_POST['estimatedDelivery']); mysql_query("UPDATE `table_name` SET `column_name` = '$input' WHERE `row_id` = 'some_value'"); That's really helpful, thanks. One part i'm a bit confused about though. My original line of code was in a while loop. So let's assume that clicking the submit button goes to a process.php page which includes the line of code you wrote above - how do I write it so each value stored in $_POST['estimatedDelivery'] is inserted into the correct field? Quote Link to comment https://forums.phpfreaks.com/topic/169433-solved-database-field-in-textbox/#findComment-893939 Share on other sites More sharing options...
Highlander Posted August 9, 2009 Share Posted August 9, 2009 What is stored in $_POST['estimatedDelivery'] ? Quote Link to comment https://forums.phpfreaks.com/topic/169433-solved-database-field-in-textbox/#findComment-893954 Share on other sites More sharing options...
jeeves245 Posted August 9, 2009 Author Share Posted August 9, 2009 I'll just post the code, as it's a bit hard to explain in plain English. while($row = mysql_fetch_array($result)) { $date2 = $row['estimatedDelivery'];; $timestamp2 = strtotime($date2); $new_date2 = date("d-m-Y",$timestamp2); echo "<td> <form action='process.php' method='post'><input type='text' name='estimatedDelivery' value=".$new_date2."></td>"; } echo "<input type='submit' name='submit' value='Submit updated delivery dates'></form>"; So using the code "The Little Guy" posted above, I need to get $new_date2 put back in the same DB field it came from in the right order. It's confusing as hell haha. Cheers in advance for any info. Quote Link to comment https://forums.phpfreaks.com/topic/169433-solved-database-field-in-textbox/#findComment-893955 Share on other sites More sharing options...
jeeves245 Posted August 9, 2009 Author Share Posted August 9, 2009 This is what i'm trying to use for the process.php page: <?php $con = mysql_connect("localhost","",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("db", $con); while(not sure what goes here) { mysql_query("UPDATE 'deliveries' SET 'estimatedDelivery' = '$new_date var from previous page"); } if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } mysql_close($con); ?> I dunno.. probably way off track Quote Link to comment https://forums.phpfreaks.com/topic/169433-solved-database-field-in-textbox/#findComment-893959 Share on other sites More sharing options...
Highlander Posted August 9, 2009 Share Posted August 9, 2009 You need to a hidden field in the form that stores the ID of the row that you want to update, like this: <input type="hidden" name="id" value="<?php echo $id?>" /> And then, on the process.php page, get the ID and then perform the query: <?php $estDev = $_POST['estimatedDelivery']; $id = $_POST['id']; mysql_query("UPDATE 'deliveries' SET 'estimatedDelivery' = '$estDev' WHERE row_id = '$id"); ?> This is just an example, and you need to modify it to fit your need, but it is a start Quote Link to comment https://forums.phpfreaks.com/topic/169433-solved-database-field-in-textbox/#findComment-894035 Share on other sites More sharing options...
RestlessThoughts Posted August 9, 2009 Share Posted August 9, 2009 Remember with hidden values that users can edit them. Therefore, if this is going to be a live site, you can assume there will be users who will edit them. Make sure you compensate with plenty of security checks to make sure they're not editing entries their not allowed to, otherwise you may be sorry. Quote Link to comment https://forums.phpfreaks.com/topic/169433-solved-database-field-in-textbox/#findComment-894039 Share on other sites More sharing options...
br3nn4n Posted August 9, 2009 Share Posted August 9, 2009 In response to the above post, you could use sessions and store the id they're editing until they're done editing (so for only one page technically). Quote Link to comment https://forums.phpfreaks.com/topic/169433-solved-database-field-in-textbox/#findComment-894050 Share on other sites More sharing options...
jeeves245 Posted August 9, 2009 Author Share Posted August 9, 2009 Thanks for the help guys. How do I actually modify this to suit my needs though? <input type="hidden" name="id" value="<?php echo $id?>" /> I haven't done this type of thing before and i'm still a bit lost. Just not too sure how to link $id to the row ID. And this this is all in a while loop which makes it even harder. Quote Link to comment https://forums.phpfreaks.com/topic/169433-solved-database-field-in-textbox/#findComment-894265 Share on other sites More sharing options...
br3nn4n Posted August 9, 2009 Share Posted August 9, 2009 When you're building the page, where they can edit (a shipping order it sounds like?) you just grab the id of the table row they're editing, and echo it to a hidden input tag. So if it's all in a while, do so for the page with the form for editing: <?php while($row = mysql_fetch_array($result)) { $date2 = $row['estimatedDelivery']; $id = $row['id']; /* Grab the id of the row the user is editing. I'm assuming your DB column is named "id" */ $timestamp2 = strtotime($date2); $new_date2 = date("d-m-Y",$timestamp2); ?> <td> <form name="updateDelivery" action="process.php" method="post"> <input type="text" name="estimatedDelivery" value="<?=$new_date2;?>" /> <input type="hidden" name="id" value="<?=$id;?>" /> </td> <? } ?> <input type="submit" name="submit" value="Submit updated delivery dates »"> </form> And then for the process.php page: <?php $con = mysql_connect("localhost","",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("db", $con); // no while needed here, just doing a DB update. Use while when grabbing stuff from the DB. $newDelivdate = $_POST['estimatedDelivery']; $id = $_POST['id']; mysql_query("UPDATE 'deliveries' SET 'estimatedDelivery' = '$newDelivdate' WHERE `id` = '$id'"); //echo some sort of confirmation for the user... if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } mysql_close($con); ?> Let me know if you need anything else, hope that helps! Quote Link to comment https://forums.phpfreaks.com/topic/169433-solved-database-field-in-textbox/#findComment-894404 Share on other sites More sharing options...
jeeves245 Posted August 10, 2009 Author Share Posted August 10, 2009 Thanks for that. But why does there not have to be a while loop for the insert part of the code? Wouldn't the code that you gave me just update one line of the database? $_POST['estimatedDelivery']; contains about 20 different dates in the array. Quote Link to comment https://forums.phpfreaks.com/topic/169433-solved-database-field-in-textbox/#findComment-894430 Share on other sites More sharing options...
br3nn4n Posted August 10, 2009 Share Posted August 10, 2009 I'm not sure how it could contain 20 dates, since the user is only updating one entry right? Or do you mean to load multiple entries, say, all entries tied to a user id allow them to edit all of them? Quote Link to comment https://forums.phpfreaks.com/topic/169433-solved-database-field-in-textbox/#findComment-894436 Share on other sites More sharing options...
jeeves245 Posted August 10, 2009 Author Share Posted August 10, 2009 I'm not sure how it could contain 20 dates, since the user is only updating one entry right? Or do you mean to load multiple entries, say, all entries tied to a user id allow them to edit all of them? Well the original code (that I posted above) is in a while loop and it creates a text box for each date stored in the estimatedDelivery field in the database. The user can change the dates and then click submit to save them back So there could be 20 or so dates in that field at any given time, therefore there will be 20 or so text boxes containing each date. Sorry if that makes little sense. I can post a screenshot to go with the code? The code is basically all written i'm just having trouble with the part where they click submit and save all the dates back to the field in the DB.. Here's the code that creates the text boxes: while($row = mysql_fetch_array($result)) { $date2 = $row['estimatedDelivery'];; $timestamp2 = strtotime($date2); $new_date2 = date("d-m-Y",$timestamp2); echo "<td> <form action='process.php' method='post'><input type='text' name='estimatedDelivery' value=".$new_date2."></td>"; } echo "<input type='submit' name='submit' value='Submit updated delivery dates'></form>"; Quote Link to comment https://forums.phpfreaks.com/topic/169433-solved-database-field-in-textbox/#findComment-894442 Share on other sites More sharing options...
halfman Posted August 10, 2009 Share Posted August 10, 2009 You cant have multiple forms and only one submit button so put the form tag before the table tag <form action='process.php' method='post'> <?php while($row = mysql_fetch_array($result)) { $date2 = $row['estimatedDelivery'];; $timestamp2 = strtotime($date2); $new_date2 = date("d-m-Y",$timestamp2); echo "<td> <input type='text' name='estimatedDelivery[]' value=".$new_date2."></td>"; //use array if you want to send multiple values for one variable name then use foreach or while to extract the variable name and values } echo "<input type='submit' name='submit' value='Submit updated delivery dates'></form>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/169433-solved-database-field-in-textbox/#findComment-894474 Share on other sites More sharing options...
jeeves245 Posted August 10, 2009 Author Share Posted August 10, 2009 Thanks for that.. but I still don't know how to actually use it to update the DB on the process.php page Quote Link to comment https://forums.phpfreaks.com/topic/169433-solved-database-field-in-textbox/#findComment-894479 Share on other sites More sharing options...
br3nn4n Posted August 10, 2009 Share Posted August 10, 2009 How is your application structured? Quote Link to comment https://forums.phpfreaks.com/topic/169433-solved-database-field-in-textbox/#findComment-894517 Share on other sites More sharing options...
jeeves245 Posted August 10, 2009 Author Share Posted August 10, 2009 Sorry what do you mean by structured? I'm still pretty new to all this Quote Link to comment https://forums.phpfreaks.com/topic/169433-solved-database-field-in-textbox/#findComment-894520 Share on other sites More sharing options...
halfman Posted August 10, 2009 Share Posted August 10, 2009 Try this mate: <?php foreach ($_POST['estimatedDelivery'] as $key => $val) { // KEY WILL BE THE ID AND VAL IS THE VALUE OF EACH POST // your update script will be :: UPDATE tableName SET estimatedDelivery = '".$val."' WHERE id = '".$key."' } ?> <form action='process.php' method='post'> <?php while($row = mysql_fetch_array($result)) { $date2 = $row['estimatedDelivery'];; $timestamp2 = strtotime($date2); $new_date2 = date("d-m-Y",$timestamp2); echo "<td> <input type='text' name='estimatedDelivery[".$id."]' value=".$new_date2."></td>"; //use array if you want to send multiple values for one variable name then use foreach or while to extract the variable name and values } echo "<input type='submit' name='submit' value='Submit updated delivery dates'></form>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/169433-solved-database-field-in-textbox/#findComment-894536 Share on other sites More sharing options...
halfman Posted August 10, 2009 Share Posted August 10, 2009 Mate the logics behind your code is wrong: If you have got different stack you need to have only one submit button any how this will be your out put: I have got the below result after pressing the 2 different submit first you need to rathionalise your proccedure then code or it will take you forever to fix every single problem: <?php session_start(); ?> <pre> <?php print_r($_POST); ?> </pre> <?php $add = $_GET['add']; if (isset($add)) { if (empty($_SESSION['username'])) { $_SESSION['username'] = ""; } } ?> <p align='right'><input type='button' name='add' value='add' onClick="parent.location=('<?php $_SERVER['PHP_SELF']; ?>?add=add')"></p> <?php if (isset($_SESSION['username'])){ if (empty($_SESSION['stack'])) { $_SESSION['stack'] = ""; } $i = 0; if(isset($add)){ $i = $_SESSION['i']; $_SESSION['stack'][$i] = " <font color=white>Welcome, ".$_SESSION['username']."!<br><a href='Logout.php'>Log Out</a></font> <form enctype='multipart/form-data' action='index.php' method='post' name='changer'> <p align='center'> <input name='image[$i]' value='image' type='text'> <br> <input type='text' name='hyperlink[$i]'value='hyperlink'> <br> <input type='text' name='currency[$i]' value='currency'> <br> <input type='text' name='name[$i]' value='name'> <br> <input type='text' name='info[$i]' value='info'> <br> <input type='text' name='keywords[$i]' value='keywords'> <br> <input type='text' name='type[$i]' value='type'> <br> <input type='submit' value='Submit'><br></p>"; $_SESSION['i'] += 1; } else { $_SESSION['stack'][$i] = "you have not pressed add yet."; } } else { $_SESSION['stack'][$i] = "You must be logged in!"; } foreach ($_SESSION['stack'] as $key => $val){ echo $_SESSION['stack'][$key]; } ?> Response is: Array ( [image] => Array ( [0] => image [1] => image ) [hyperlink] => Array ( [0] => hyperlink [1] => hyperlink ) [currency] => Array ( [0] => currency [1] => currency ) [name] => Array ( [0] => name [1] => name ) [info] => Array ( [0] => info [1] => info ) [keywords] => Array ( [0] => keywords [1] => keywords ) [type] => Array ( [0] => type [1] => type ) ) Quote Link to comment https://forums.phpfreaks.com/topic/169433-solved-database-field-in-textbox/#findComment-894554 Share on other sites More sharing options...
jeeves245 Posted August 10, 2009 Author Share Posted August 10, 2009 Alllright. I'm completely lost. This is way over my head haha. Thanks anyway. Quote Link to comment https://forums.phpfreaks.com/topic/169433-solved-database-field-in-textbox/#findComment-894596 Share on other sites More sharing options...
halfman Posted August 10, 2009 Share Posted August 10, 2009 Alright. I'm completely lost. This is way over my head ha ha. Thanks anyway. dont give up mate. I am sure we can help you as the forum has been made for this, so we can share and help eachother. If still want us to help you then just let me know, I would be happy to help you out mate. Email me you code if you like and let me know what you try to achievr, or draw a flowchart and I see if I can be helpful. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/169433-solved-database-field-in-textbox/#findComment-894604 Share on other sites More sharing options...
jeeves245 Posted August 10, 2009 Author Share Posted August 10, 2009 I'll have a go at incorporating the code you posted above and let you know how it goes. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/169433-solved-database-field-in-textbox/#findComment-894606 Share on other sites More sharing options...
jeeves245 Posted August 10, 2009 Author Share Posted August 10, 2009 Ok been thinking (haven't looked at your code yet). With the code i've already written, wouldn't the while loop put the $new_date value into the $_POST['estimatedDelivery'] array each time it passes? So isn't $_POST['estimatedDelivery'] all I need for the process.php page to update the row? I don't actually see why I need to modify the code below... But that said, i'm no expert while($row = mysql_fetch_array($result)) { $date2 = $row['estimatedDelivery'];; $timestamp2 = strtotime($date2); $new_date2 = date("d-m-Y",$timestamp2); echo "<td> <form action='process.php' method='post'><input type='text' name='estimatedDelivery' value=".$new_date2."></td>"; } echo "<input type='submit' name='submit' value='Submit updated delivery dates'></form>"; Quote Link to comment https://forums.phpfreaks.com/topic/169433-solved-database-field-in-textbox/#findComment-894648 Share on other sites More sharing options...
jeeves245 Posted August 10, 2009 Author Share Posted August 10, 2009 Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/169433-solved-database-field-in-textbox/#findComment-895154 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.