halfman Posted August 11, 2009 Share Posted August 11, 2009 estimatedDelivery is a reference when it is in the name = "estimatedDelivery" in your text box, when PHP collect all the vars they all will have the same name and they over right eachother, and you will get the last $_POST['estimatedDelivery'] whereas if you have got name="estimatedDelivery[$id]" then every reference has its own name and you will be end up with multiple POSTs like this $_POST[estimatedDelivery][1] = "DATE" $_POST[estimatedDelivery][2] = "DATE" $_POST[estimatedDelivery][3] = "DATE" Give a shot at this code and see what will happen <?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/page/2/#findComment-895223 Share on other sites More sharing options...
jeeves245 Posted August 11, 2009 Author Share Posted August 11, 2009 Thanks again for the info. I gave that code a go but when I click submit i'm just getting a blank page with "Error: Query was empty". Chances are i've done something wrong.. i'll keep at it Quote Link to comment https://forums.phpfreaks.com/topic/169433-solved-database-field-in-textbox/page/2/#findComment-895232 Share on other sites More sharing options...
halfman Posted August 11, 2009 Share Posted August 11, 2009 Thanks again for the info. I gave that code a go but when I click submit i'm just getting a blank page with "Error: Query was empty". Chances are i've done something wrong.. i'll keep at it check your query again, check if you extracted values and keys correctly, // HOW TO EXTRACT THE VALUE -- very simplified mate not a very clever approach foreach ($_POST as $key => $val) { foreach ($val as $k => $v) { echo $v; // PUT THE SQL CODE IN HERE AND $key WILL BE YOUR TRANSFFERED id // UPDATE tableName SET 'filedName' = '".$v."' WHERE id = '".$key."' } } Quote Link to comment https://forums.phpfreaks.com/topic/169433-solved-database-field-in-textbox/page/2/#findComment-895273 Share on other sites More sharing options...
jeeves245 Posted August 11, 2009 Author Share Posted August 11, 2009 Ok. I tried this to see what was being echoed: foreach ($_POST['estimatedDelivery'] as $key => $val) { echo "<br>"; echo $val; echo "<br>"; echo $key; } And I get this result which is correct: 16-07-2009 0 17-07-2009 1 18-07-2009 2 19-07-2009 3 20-07-2009 4 21-07-2009 5 22-07-2009 6 23-07-2009 7 But when I insert the SQL statement in the for loop I still get "Error: Query was empty".. I'm using this query: foreach ($_POST['estimatedDelivery'] as $key => $val) { mysql_query("UPDATE smail SET estimatedDelivery = '".$val."' WHERE id = '".$key."'"); } } I also tried your modified for loop that you posted above but I got the same result. Quote Link to comment https://forums.phpfreaks.com/topic/169433-solved-database-field-in-textbox/page/2/#findComment-895279 Share on other sites More sharing options...
halfman Posted August 11, 2009 Share Posted August 11, 2009 Ok. I tried this to see what was being echoed: foreach ($_POST['estimatedDelivery'] as $key => $val) { echo "<br>"; echo $val; echo "<br>"; echo $key; } And I get this result which is correct: 16-07-2009 0 17-07-2009 1 18-07-2009 2 19-07-2009 3 20-07-2009 4 21-07-2009 5 22-07-2009 6 23-07-2009 7 But when I insert the SQL statement in the for loop I still get "Error: Query was empty".. I'm using this query: foreach ($_POST['estimatedDelivery'] as $key => $val) { mysql_query("UPDATE smail SET estimatedDelivery = '".$val."' WHERE id = '".$key."'"); } } I also tried your modified for loop that you posted above but I got the same result. Try to echo this and let me know what is the result. foreach ($_POST['estimatedDelivery'] as $key => $val) { echo "UPDATE smail SET estimatedDelivery = '".$val."' WHERE id = '".$key."'"."<br>"; // mysql_query("UPDATE smail SET estimatedDelivery = '".$val."' WHERE id = '".$key."'"); } } Quote Link to comment https://forums.phpfreaks.com/topic/169433-solved-database-field-in-textbox/page/2/#findComment-895284 Share on other sites More sharing options...
jeeves245 Posted August 11, 2009 Author Share Posted August 11, 2009 UPDATE smail SET estimatedDelivery = '16-07-2009' WHERE id = '0' UPDATE smail SET estimatedDelivery = '17-07-2009' WHERE id = '1' UPDATE smail SET estimatedDelivery = '18-07-2009' WHERE id = '2' UPDATE smail SET estimatedDelivery = '19-07-2009' WHERE id = '3' UPDATE smail SET estimatedDelivery = '20-07-2009' WHERE id = '4' UPDATE smail SET estimatedDelivery = '21-07-2009' WHERE id = '5' UPDATE smail SET estimatedDelivery = '22-07-2009' WHERE id = '6' UPDATE smail SET estimatedDelivery = '23-07-2009' WHERE id = '7' Error: Query was empty Quote Link to comment https://forums.phpfreaks.com/topic/169433-solved-database-field-in-textbox/page/2/#findComment-895286 Share on other sites More sharing options...
halfman Posted August 11, 2009 Share Posted August 11, 2009 UPDATE smail SET estimatedDelivery = '16-07-2009' WHERE id = '0' UPDATE smail SET estimatedDelivery = '17-07-2009' WHERE id = '1' UPDATE smail SET estimatedDelivery = '18-07-2009' WHERE id = '2' UPDATE smail SET estimatedDelivery = '19-07-2009' WHERE id = '3' UPDATE smail SET estimatedDelivery = '20-07-2009' WHERE id = '4' UPDATE smail SET estimatedDelivery = '21-07-2009' WHERE id = '5' UPDATE smail SET estimatedDelivery = '22-07-2009' WHERE id = '6' UPDATE smail SET estimatedDelivery = '23-07-2009' WHERE id = '7' Error: Query was empty What is the dataType for the estimatedDelivery field in your mysql? set it to varchar 200. also I would suggest running one of the update sql directly from your mysql and see if there is any error. Quote Link to comment https://forums.phpfreaks.com/topic/169433-solved-database-field-in-textbox/page/2/#findComment-895289 Share on other sites More sharing options...
jeeves245 Posted August 11, 2009 Author Share Posted August 11, 2009 I can't set it to varchar as it's a date and I'm using some format date functions to format it. But I'll try using the update query in phpmyadmin and see if I get any errors. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/169433-solved-database-field-in-textbox/page/2/#findComment-895301 Share on other sites More sharing options...
jeeves245 Posted August 11, 2009 Author Share Posted August 11, 2009 Wait... mysql_query("UPDATE smail SET estimatedDelivery = '".$val."' WHERE id = '".$key."'"); id? Where did that come from? I have no id column... If $key is just a number, how will the WHERE = clause work? Wouldn't it need to reference the date that I am updating? Quote Link to comment https://forums.phpfreaks.com/topic/169433-solved-database-field-in-textbox/page/2/#findComment-895331 Share on other sites More sharing options...
halfman Posted August 11, 2009 Share Posted August 11, 2009 Mate have you read the code?? You need to pass the id. name='estimatedDelivery[".$id."]' Maybe your id is $row['id'] so your $id = $row['id']; <?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/page/2/#findComment-895351 Share on other sites More sharing options...
jeeves245 Posted August 11, 2009 Author Share Posted August 11, 2009 Right, gotcha. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/169433-solved-database-field-in-textbox/page/2/#findComment-895356 Share on other sites More sharing options...
jeeves245 Posted August 11, 2009 Author Share Posted August 11, 2009 Ok i'm getting there, got rid of all the errors, i'm just still a bit confused on the WHERE clause. WHERE estimatedDelivery = '".$key."'"; Could you please elaborate on how this works? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/169433-solved-database-field-in-textbox/page/2/#findComment-895368 Share on other sites More sharing options...
halfman Posted August 11, 2009 Share Posted August 11, 2009 Mate, I dont know the structure of your table so I assume that you need some kind of the WHERE condition. I thought if you pass the id from each post you know exactly each post belongs to where in your database (Instead of using a hidden file to pass the id), so the $key is the actual passed id from your FORM, you can use it to point each record properly in its place. for example: UPDATE tableName SET estimatedDelivery = '".$val."' WHERE id = '".$id."' PHP takes care of the array and the value as the array structures will get in place in the order of the submission. for example : [1] => ['10-10-09'] [id] => ['estimatedDelivery'] Have a read on the PHP Array in php.net it will help you a lot. Quote Link to comment https://forums.phpfreaks.com/topic/169433-solved-database-field-in-textbox/page/2/#findComment-895369 Share on other sites More sharing options...
jeeves245 Posted August 11, 2009 Author Share Posted August 11, 2009 Thanks mate. Quote Link to comment https://forums.phpfreaks.com/topic/169433-solved-database-field-in-textbox/page/2/#findComment-895381 Share on other sites More sharing options...
halfman Posted August 11, 2009 Share Posted August 11, 2009 Thanks mate. No worries mate. Any time, Let me know if you needed any more help. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/169433-solved-database-field-in-textbox/page/2/#findComment-895382 Share on other sites More sharing options...
jeeves245 Posted August 11, 2009 Author Share Posted August 11, 2009 Actually one more question about for loops... I've changed things around a bit so the dates go into the correct DB rows.. so with the code below, how do I modify the for loop accordingly? $id = $_POST['id']; $date = $_POST['estimatedDelivery']; foreach ($_POST['estimatedDelivery'] ??????) { $sql2 = "UPDATE smail SET estimatedDelivery = '".$date."' WHERE estimatedDelivery = '".$id."'"; mysql_query($sql2); } Quote Link to comment https://forums.phpfreaks.com/topic/169433-solved-database-field-in-textbox/page/2/#findComment-895448 Share on other sites More sharing options...
halfman Posted August 11, 2009 Share Posted August 11, 2009 Actually one more question about for loops... I've changed things around a bit so the dates go into the correct DB rows.. so with the code below, how do I modify the for loop accordingly? $id = $_POST['id']; $date = $_POST['estimatedDelivery']; foreach ($_POST['estimatedDelivery'] ??????) { $sql2 = "UPDATE smail SET estimatedDelivery = '".$date."' WHERE estimatedDelivery = '".$id."'"; mysql_query($sql2); } Mate this code is wrong you wont be able to update a recored and have it as the where condition, it is not very clever of coding and I dont think it is logicly possible. "UPDATE smail SET estimatedDelivery = '".$date."' WHERE estimatedDelivery = '".$id."'"; Needs to be: "UPDATE smail SET estimatedDelivery = '".$date."' WHERE `id` = '".$id."'"; Quote Link to comment https://forums.phpfreaks.com/topic/169433-solved-database-field-in-textbox/page/2/#findComment-896057 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.