Jump to content

[SOLVED] Database field in textbox


jeeves245

Recommended Posts

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>";
  ?>

 

Link to comment
Share on other sites

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."'

}

}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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."'");
   }
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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>";
  ?>

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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);
}

Link to comment
Share on other sites

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."'";

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.