Take the tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to insert textbox-fields text into MySQL table row, using while loop.

Here is a part of the code: ($datetoday = 270313)

                        <?php 


        $i=0;
        while ($i < $row_Num) {
            ${'date_'.$i} = $_REQUEST["{'date_'.$i}"];
            ${'text_'.$i} = $_REQUEST["{'text_'.$i}"];
            ${'con_name_'.$i} = $_REQUEST["{'con_name_'.$i}"];
            ${'con_phone_'.$i} = $_REQUEST["{'con_phone_'.$i}"];

           $values= array(${'date_'.$i},${'text_'.$i},${'con_name_'.$i},${'con_phone_'.$i});
           print_r($values);
           $sql = "INSERT INTO data.$datetoday(`KEY,`DATE`,`COLOR`,`TEXT`,`CON_NAME`,`PHONE`) VALUES (`$i`,`$values[0]`,`FFFFFF`,`$values[1]`,`$values[2]`,`$values[3]`)";
           $result = mysql_query($sql,$link);
           if (!$result) {
            die ("Can't create table named $datetoday : " . mysql_error());
            }
           $i++;
           }


        ?>

The URL of the page is:

> http://localhost:5110/test.php?date_0=000000&text_0=QWERTY&con_name_0=iuytre&con_phone_0=0000000000&date_1=111111&text_1=ASDFGHJK&con_name_1=lkjhgfd&con_phone_1=1212121212

However it doesn't update the table or print the array (for testing). Any suggestions?

share|improve this question
 
Localhost does not work for us :-) Furthermore you might want to get back to the basics of PHP. This is far from logical an simple what you are trying to do here. Also printr() is not a function (not by default -> print_r()). And why create variables to re-use them in a very weird way again.. –  PENDO Mar 27 at 15:16
 
Have you confirmed that $row_Num is greater than or equal to '1'? In other words are you certain you are getting in the while loop? –  Asok Mar 27 at 15:19
 
Thank you, but I still don't understend why it doesnt work. what need do be changed in the array part? –  user2216190 Mar 27 at 15:21
 
@Asok - Yes, Im sure. –  user2216190 Mar 27 at 15:22
 
what is $datetoday ? doesn't appear anywere before it is used –  SirDarius Mar 27 at 15:23
show 7 more comments

3 Answers

up vote 2 down vote accepted

There were several minor things wrong, using your URL substring I got it working, see below:

$row_Num = 1;
$i = 0;
while ($i <= $row_Num) {
    // In the $_REQUEST you were indexing the curly braces, remove them.
    ${"date_" . $i} = $_REQUEST["date_$i"];
    ${"text_" . $i} = $_REQUEST["text_$i"];
    ${"con_name_" . $i} = $_REQUEST["con_name_$i"];
    ${"con_phone_" . $i} = $_REQUEST["con_phone_$i"];

    $values = array(
        ${"date_" . $i},
        ${"text_" . $i},
        ${"con_name_" . $i},
        ${"con_phone_" . $i}
    );
    // Not printr() And this is how I print arrays, it is cleaner.
    echo '<pre>', print_r($values, true), '</pre>';
    $i++;
}

Outputs:

Array
(
    [0] => 000000
    [1] => QWERTY
    [2] => iuytre
    [3] => 0000000000
)
Array
(
    [0] => 111111
    [1] => ASDFGHJK
    [2] => lkjhgfd
    [3] => 1212121212
)

Edit

I am still not clear how you are setting $row_Num.

$_SESSION['row_Num'] = $_REQUEST["row_Num"];

This doesn't tell me anything since the $_REQUEST string you are showing doesn't contain "row_Num". So since your query requires DATE I made a foreach loop to count how many times DATE appears. Try the following code and let me know.

$i = 0;
$count = 0;
// I added this line to take care of how many times the loop runs
foreach ($_REQUEST as $key => $value) { (strstr($key, 'date')) ? $count++ : NULL; }
// Changed '<=' to '<' because it would loop 3 times since we start $i at 0.
// $i needs to remain at 0 since your URL substring indexing starts at 0.
while ($i < $count) {
    ${'date_' . $i} = $_REQUEST["date_$i"];
    ${'text_' . $i} = $_REQUEST["text_$i"];
    ${'con_name_' . $i} = $_REQUEST["con_name_$i"];
    ${'con_phone_' . $i} = $_REQUEST["con_phone_$i"];

    $values = array(
        ${"date_" . $i},
        ${"text_" . $i},
        ${"con_name_" . $i},
        ${"con_phone_" . $i}
    );

    ### Echo for troubleshooting ###
    echo '<pre>', print_r($values, true), '</pre>';
    $sql = "INSERT INTO `data`.`".$datetoday."` (`KEY`,`DATE`,`COLOR`,`TEXT`,`CON_NAME`,`PHONE`) VALUES 
        (
            '".$i."',
            '".$values[0]."',
            'FFFFFF',
            '".$values[1]."',
            '".$values[2]."',
            '".$values[3]."'
        )";

    ### Echo for troubleshooting ###
    echo '<pre>', $sql, '</pre>';
    $result = mysql_query($sql, $link) or die ('Could not insert values: '.mysql_error());
    $i++;
}

Output:

Array
(
    [0] => 000000
    [1] => QWERTY
    [2] => iuytre
    [3] => 0000000000
)
INSERT INTO `data`.`280313` (`KEY`,`DATE`,`COLOR`,`TEXT`,`CON_NAME`,`PHONE`) VALUES 
        (
            '0',
            '000000',
            'FFFFFF',
            'QWERTY',
            'iuytre',
            '0000000000'
        )
Array
(
    [0] => 111111
    [1] => ASDFGHJK
    [2] => lkjhgfd
    [3] => 1212121212
)
INSERT INTO `data`.`280313` (`KEY`,`DATE`,`COLOR`,`TEXT`,`CON_NAME`,`PHONE`) VALUES 
        (
            '1',
            '111111',
            'FFFFFF',
            'ASDFGHJK',
            'lkjhgfd',
            '1212121212'
        )
share|improve this answer
 
Thank you for you'r solution- but the code (the first one) prints only the 1st array. EDIT - I think that the problem is with the $row_Num,cheking it now... –  user2216190 Mar 27 at 19:35
 
That's interesting, I was able to get both. How are you setting $row_Num? –  Asok Mar 27 at 19:37
 
I changed $row_Num into a session called "$_SESSION['row_Num']". when I print $_SESSION['row_Num'] it shows the right value, but when I use it in the while-loop it doesn't work well.. –  user2216190 Mar 27 at 20:17
 
also only the 1st MySQL row is updated –  user2216190 Mar 27 at 20:24
 
Can you edit your question showing exactly how you are setting $row_Num or $_SESSION['row_Num'] and can you explain a little more what you mean by "it doesn't work well". Sorry, I am a little confused. –  Asok Mar 27 at 20:31
show 7 more comments

Use 'date_' . $i instead of 'date_'.'$i'

'date_'.'$i' is always date_$i, no expansion of the variable $i is done due to the use of single quotes. If you want to expand variables inside strings, use double quotes (or no quotes if the $variables is at the start or end of the string).

share|improve this answer
 
Thank you for answering. but the code still doesn't work... –  user2216190 Mar 27 at 15:19
add comment

Note that call n times SQL request is slow. It will be better if you send one SQL request. Something like this:

<?php 
 $i=0;$comma="";$all_values="";
 while($i<$row_Num)
 {${'date_'.$i}=$_REQUEST["date_".$i];
  ${'text_'.$i}=$_REQUEST["text_".$i];
  ${'con_name_'.$i}=$_REQUEST["con_name_"$i];
  ${'con_phone_'.$i}=$_REQUEST["con_phone_".$i];

  $values=array(${'date_'.$i},${'text_'.$i},${'con_name_'.$i},${'con_phone_'.$i});
  print_r($values);
  $all_values.=$comma."('".$i."','".$values[0]."','FFFFFF','".$values[1]."','".$values[2]."','".$values[3]."')";
  $comma=",";
  $i++;
 }
 $sql="INSERT INTO data.$datetoday (`KEY,`DATE`,`COLOR`,`TEXT`,`CON_NAME`,`PHONE`) VALUES ".$all_values;
 $result=mysql_query($sql,$link);
 if(!$result)
     die("Can't create table named $datetoday : ".mysql_error());
?>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.