php-general Digest 23 Jun 2013 22:15:05 -0000 Issue 8275

Topics (messages 321460 through 321465):

Re: scandir doesn't find all files
        321460 by: Carlos Medina

Hmm remarkable things?
        321461 by: Karl-Arne Gjersøyen
        321462 by: Serge Fonville

One more newbie question. About foreach..
        321463 by: Karl-Arne Gjersøyen
        321464 by: Tim Streater
        321465 by: Maciek Sokolewicz

Administrivia:

To subscribe to the digest, e-mail:
        php-general-digest-subscr...@lists.php.net

To unsubscribe from the digest, e-mail:
        php-general-digest-unsubscr...@lists.php.net

To post to the list, e-mail:
        php-gene...@lists.php.net


----------------------------------------------------------------------
--- Begin Message ---
Hi,
check the permissions:

if the server can access to files or create files
if the FTP creates the files with the right permission
if a script is running, check if the script can creates the files with
the right permission


Regards

Carlos Medina


Am 22.06.2013 21:10, schrieb Daniel Pöllmann:
> Hi,
> I have some files in a directory - some are uploaded via ftp and some other
> are created by a php script.
> 
> Scandir just finds the uploaded files, but none of the created files.
> I can't run chown() because the server is part of shared hosting.
> 
> I can't find anything about this behavour in the documentation.
> 
> Best wishes,
> Daniel
> 


--- End Message ---
--- Begin Message ---
Hello again.
Thanks for last answere. It works very well but now I have another
remarkable things. Perhaps logic but not for me yet..

I have this source code: (In norwegian but I translate my thoughts through
it in english.)

The problem with this source code is that it work very well if I select one
single item and post it. But when select 2 or more items the same amount
are filled in all field named stk_pa_lager in the database.

// count and increase the amount of tubes.
if(isset($_POST['oppdater_antall_stk_rorladning'])){

// Get store
    $valgt_lager = $_POST['valgt_lager'];

// Get serial number   (Array <input type="number" name="varenr[] size="3">)
  $varenr = $_POST['varenr'];

// number of items in store
    $stk_pa_lager = $_POST['stk_pa_lager'];

// Create an array (<input type="number"
name="oppdater_antall_stk_rorladning[] size="3">)
    $oppdater_antall_stk_rorladning =
$_POST['oppdater_antall_stk_rorladning'];

// Extract the array and save it induvidual as items in $rorladning
    foreach($oppdater_antall_stk_rorladning as $rorladning){

// Multiply and add new items to that one already is in store
        $oppdatert_antall_stk_rorladning = $stk_pa_lager + $rorladning;

// Extract serialnumber and save them one by one in $vnr
    foreach($varenr as $vnr){

// Connect to MySQL database
    include('../../tilkobling.php');

// Update table rorladning
    $sql = "UPDATE rorladning SET stk_pa_lager =
'$oppdatert_antall_stk_rorladning' WHERE valgt_lager = '$valgt_lager' AND
varenr = '$vnr'";
    mysql_query($sql, $tilkobling) or die(mysql_error());

        }

// Output the result to screen
        echo "<p>R&oslash;rladning med varenr: <b>$vnr</b> er oppdatert fra
<b>$stk_pa_lager</b> til <b>$oppdatert_antall_stk_rorladning</b> p&aring;
lager: <b>$valgt_lager</b>.</p>";
        //echo "$oppdatert_antall_stk_rorladning<br>";
    unset($rorladning);
    unset($vnr);
    }
}


// My database table:
mysql> SELECT * FROM rorladning;
+------------+-------------+-------+-------------+--------------+---------+--------------+-------------+
| leverandor | valgt_lager | un_nr | varenavn    | varenr       | dim_mm  |
stk_pa_lager | kg_pa_lager |
+------------+-------------+-------+-------------+--------------+---------+--------------+-------------+
| Orica      | Tengs       | 0081  | Hvit Rør   | ETX1.22X1000 | 22x1000 |
70           | 3.7         |
| Orica      | Tengs       | 0081  | Orange Rør | ETX1.17X460  | 17x460  |
70           | 0.95        |
+------------+-------------+-------+-------------+--------------+---------+--------------+-------------+
2 rows in set (0.00 sec)

mysql>

What I think i am doing wrong is the way I write the php/mysql stuff. What
I want is to store differt values in stk_pa_lager based on the particular
serialnumber. (field varenr).

I will be very thankful if you can tell me what is wrong here and point out
what to do with it.
Thanks for your time.

Karl

--- End Message ---
--- Begin Message ---
Hi,

name="varenr[] size="3" seems to be missing a closing "

HTH

Kind regards/met vriendelijke groet,

Serge Fonville

http://www.sergefonville.nl

Convince Microsoft!
They need to add TRUNCATE PARTITION in SQL Server
https://connect.microsoft.com/SQLServer/feedback/details/417926/truncate-partition-of-partitioned-table


2013/6/23 Karl-Arne Gjersøyen <karlar...@gmail.com>

> Hello again.
> Thanks for last answere. It works very well but now I have another
> remarkable things. Perhaps logic but not for me yet..
>
> I have this source code: (In norwegian but I translate my thoughts through
> it in english.)
>
> The problem with this source code is that it work very well if I select one
> single item and post it. But when select 2 or more items the same amount
> are filled in all field named stk_pa_lager in the database.
>
> // count and increase the amount of tubes.
> if(isset($_POST['oppdater_antall_stk_rorladning'])){
>
> // Get store
>     $valgt_lager = $_POST['valgt_lager'];
>
> // Get serial number   (Array <input type="number" name="varenr[]
> size="3">)
>   $varenr = $_POST['varenr'];
>
> // number of items in store
>     $stk_pa_lager = $_POST['stk_pa_lager'];
>
> // Create an array (<input type="number"
> name="oppdater_antall_stk_rorladning[] size="3">)
>     $oppdater_antall_stk_rorladning =
> $_POST['oppdater_antall_stk_rorladning'];
>
> // Extract the array and save it induvidual as items in $rorladning
>     foreach($oppdater_antall_stk_rorladning as $rorladning){
>
> // Multiply and add new items to that one already is in store
>         $oppdatert_antall_stk_rorladning = $stk_pa_lager + $rorladning;
>
> // Extract serialnumber and save them one by one in $vnr
>     foreach($varenr as $vnr){
>
> // Connect to MySQL database
>     include('../../tilkobling.php');
>
> // Update table rorladning
>     $sql = "UPDATE rorladning SET stk_pa_lager =
> '$oppdatert_antall_stk_rorladning' WHERE valgt_lager = '$valgt_lager' AND
> varenr = '$vnr'";
>     mysql_query($sql, $tilkobling) or die(mysql_error());
>
>         }
>
> // Output the result to screen
>         echo "<p>R&oslash;rladning med varenr: <b>$vnr</b> er oppdatert fra
> <b>$stk_pa_lager</b> til <b>$oppdatert_antall_stk_rorladning</b> p&aring;
> lager: <b>$valgt_lager</b>.</p>";
>         //echo "$oppdatert_antall_stk_rorladning<br>";
>     unset($rorladning);
>     unset($vnr);
>     }
> }
>
>
> // My database table:
> mysql> SELECT * FROM rorladning;
>
> +------------+-------------+-------+-------------+--------------+---------+--------------+-------------+
> | leverandor | valgt_lager | un_nr | varenavn    | varenr       | dim_mm  |
> stk_pa_lager | kg_pa_lager |
>
> +------------+-------------+-------+-------------+--------------+---------+--------------+-------------+
> | Orica      | Tengs       | 0081  | Hvit Rør   | ETX1.22X1000 | 22x1000 |
> 70           | 3.7         |
> | Orica      | Tengs       | 0081  | Orange Rør | ETX1.17X460  | 17x460  |
> 70           | 0.95        |
>
> +------------+-------------+-------+-------------+--------------+---------+--------------+-------------+
> 2 rows in set (0.00 sec)
>
> mysql>
>
> What I think i am doing wrong is the way I write the php/mysql stuff. What
> I want is to store differt values in stk_pa_lager based on the particular
> serialnumber. (field varenr).
>
> I will be very thankful if you can tell me what is wrong here and point out
> what to do with it.
> Thanks for your time.
>
> Karl
>

--- End Message ---
--- Begin Message ---
Hello again. I Got the solution for my last mention problem. Now I can
update several rows at once by one single submit action.

In my form I have many records and therefor I use an checkbox to just mark
those records I like to update. This work just fine in PHP/HTML5 form and I
got it as I want.

But the problem now is, how to recognize just those selected records by
serialnumber?

include('../../connect.php');

// Foreach get all given serialnumbers as I want it
    foreach($serialnumber as $snr){

// I got the number of serialnumbers given in the array
   $count = count($serialnumber);

// I thought that a for loop would do the trick together with foreach
// And repeat the $sql = SELECT below but that did not happen.
    for($i = 0; $i <= $count; $i++){


// Connect and get only those records with given serialnumber
$sql = "SELECT * FROM explosive WHERE serialnumber = '$snr'";
    } // End of for loop
    } // End of foreach loop
$result = mysql_query($sql, $connect) or die(mysql_error());
$count = mysql_num_rows($result);

I have tried to search in google and on PHP.net but can't fine anything
that explain my problem.
I like to have new $sql = SELECT queries for every given serialnumber ($snr)

Thanks for your help.

Karl

--- End Message ---
--- Begin Message ---
On 23 Jun 2013 at 16:11, Karl-Arne Gjersøyen <karlar...@gmail.com> wrote: 

> // Foreach get all given serialnumbers as I want it
>    foreach($serialnumber as $snr){
>
> // I got the number of serialnumbers given in the array
>   $count = count($serialnumber);

Why not do this *before* the foreach loop?

> // I thought that a for loop would do the trick together with foreach
> // And repeat the $sql = SELECT below but that did not happen.
>    for($i = 0; $i <= $count; $i++){
>
>
> // Connect and get only those records with given serialnumber
> $sql = "SELECT * FROM explosive WHERE serialnumber = '$snr'";
>    } // End of for loop
>    } // End of foreach loop

All this for loop does is overwrite $sql each time.

> $result = mysql_query($sql, $connect) or die(mysql_error());
> $count = mysql_num_rows($result);

You need to have your mysql_query *inside* the for loop. Then, what are you 
doing with the results of the mysql_query? Nothing?

Also, why do you need the for loop anyway? What is it supposed to do?

--
Cheers  --  Tim

--- End Message ---
--- Begin Message ---
On 23-6-2013 17:11, Karl-Arne Gjersøyen wrote:
Hello again. I Got the solution for my last mention problem. Now I can
update several rows at once by one single submit action.

[...]
I have tried to search in google and on PHP.net but can't fine anything
that explain my problem.
I like to have new $sql = SELECT queries for every given serialnumber ($snr)

Thanks for your help.

Karl


Ok, Karl, I've seen quite a few messages from you to this list with various questions; from all these questions it becomes clear that you're trying to work with lists in a single form without understanding the basics of a form in the first place. You also seem to not understand what an array is exactly, or how to process it.

So let's answer all these questions at once; I will attempt to explain (in short) how to do all this, using a custom form here. I don't speak Norwegian, and I find your variable names to be horribly long and complex, so let's not use them :)

Say you want a simple form online which gives you a product name and asks you to update the amount of it in stock.
<form action="..." method="post">
<input type="text" name="number_in_stock" value="0">
<input type="submit" name="submit" value="store">
</form>

When you submit this simple form, the PHP script defined in the action property is called, and the $_POST array looks like:
$_POST = array(
   'number_in_stock' = '0'
);

You can then run your simple query
mysql_query("UPDATE some_table SET stock='" . $_POST['number_in_stock']); // note: you should always sanitize these values; i.e. make sure it is EXACTLY what you expect, and CAN NOT possibly be anything else. So if you expect it to be a number, check if it IS a number before running this query!!!

Now, you said you wanted to update multiple items.
Imagine you have a form showing multiple items:
<form action="..." method="post">
Item A: <input type="text" name="number_in_stock" value="8">
Item B: <input type="text" name="number_in_stock" value="2">
Item C: <input type="text" name="number_in_stock" value="258">
<input type="submit" name="submit" value="store">
</form>

If you do this, PHP will have no idea what is what, and it will just keep overwriting the number_in_stock value until it reaches the last one. So you would end up with a $_POST array looking like this:
$_POST = array(
   'number_in_stock' = '258'
);

Obviously, you don't want that. The solution would be to tell PHP to turn all recieved values into an array. This can be done by manually specifying the key for each array item; or letting PHP do it automatically. This automatic way was suggested earlier, like so:
<form action="..." method="post">
Item A: <input type="text" name="number_in_stock[]" value="8">
Item B: <input type="text" name="number_in_stock[]" value="2">
Item C: <input type="text" name="number_in_stock[]" value="258">
<input type="submit" name="submit" value="store">
</form>

This then results in a $_POST array like this:
$_POST = array(
   'number_in_stock' = array(
      0 => '8',
      1 => '2',
      2 => '258'
   )
);

So now, do you have any idea what is what? No. You don't. Why? because you don't supply a link between the value and the meaning. Instead, you could decide to supply a certain unique key per item, like so:
<form action="..." method="post">
Item A: <input type="text" name="number_in_stock['ItemA']" value="8">
Item B: <input type="text" name="number_in_stock['ItemB']" value="2">
Item C: <input type="text" name="number_in_stock['ItemC']" value="258">
<input type="submit" name="submit" value="store">
</form>

This results in:
$_POST = array(
   'number_in_stock' = array(
      'ItemA' => '8',
      'ItemB' => '2',
      'ItemC' => '258'
   )
);

Wow, now you can actually use this info when updating your table in the DB!
foreach($_POST['number_in_stock'] as $item=>$number) {
mysql_query("UPDATE table SET stock='".$number."' WHERE itemId='".$item);
}
This will run over each element in the $_POST['number_in_stock'] array. It will (for that single loop-run) stick the key in $item and the value in $number. For each run, it will run the update query. Then in the next run, a new set of values is supplied, and a new query is ran.

If you want to expand this to give you the ability to define which items should be updated and which should not, you could add checkboxes. When checked, the item will be updated; otherwise it won't. Checkboxes have a great feature where if they are checked, they have the value supplied in their value attribute. If they are not checked, they have no value. So, you could add something like:
<form action="..." method="post">
<input type="checkbox" name="item_list['ItemA'] value="1"> Item A: <input type="text" name="number_in_stock['ItemA']" value="8"> <input type="checkbox" name="item_list['ItemB'] value="1">Item B: <input type="text" name="number_in_stock['ItemB']" value="2"> <input type="checkbox" name="item_list['ItemC'] value="1">Item C: <input type="text" name="number_in_stock['ItemC']" value="258">
<input type="submit" name="submit" value="store">
</form>

When you submit this form, and have only the first checkbox ticked, the $_POST array will look like this:
$_POST = array(
   'item_list' = array(
      'itemA' => '1',
      'itemB' => '',
      'itemC' => ''
   ),
   'number_in_stock' = array(
      'ItemA' => '8',
      'ItemB' => '2',
      'ItemC' => '258'
   )
);

You now know that only itemA should be updated. For this, you could run a foreach on item_list, check which value == 1 and then for those, run the query:

foreach($_POST['item_list'] as $item=>$value) {
   if($value == 1) {
      // apparently we want to update this item
$number = $_POST['number_in_stock'][$item]; // $item = 'ItemA' or 'ItemB' or whatever name was selected mysql_query("UPDATE table SET stock='".$number."' WHERE itemId='".$item);
   }
}

Simple as that :)
Don't start unsetting, and doing I don't know what, just KISS (Keep It Stupidly Simple)

- Tul

--- End Message ---

Reply via email to