1

while我正在尝试使用循环将文本框字段文本插入 MySQL 表行。

这是代码的一部分:($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++;
           }


        ?>

该页面的网址是:

> 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

但是它不会更新表格或打印数组(用于测试)。有什么建议么?

4

3 回答 3

2

有几个小问题,使用你的 URL 子字符串我得到了它的工作,见下文:

$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++;
}

输出:

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

编辑

我仍然不清楚你是如何设置的$row_Num

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

这并没有告诉我任何事情,因为$_REQUEST您显示的字符串不包含“row_Num”。因此,由于您的查询需要DATE我进行foreach循环来计算DATE出现的次数。试试下面的代码,让我知道。

$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++;
}

输出:

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'
        )
于 2013-03-27T15:37:58.597 回答
1

使用'date_' . $i代替'date_'.'$i'

'date_'.'$i'is always ,由于使用了单引号date_$i,因此不会对变量进行扩展。$i如果要在字符串中展开变量,请使用双引号(如果 $variables 位于字符串的开头或结尾,则不使用引号)。

于 2013-03-27T15:17:03.683 回答
0

请注意,调用 n 次 SQL 请求很慢。如果你发送一个 SQL 请求会更好。像这样的东西:

<?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());
?>
于 2013-03-27T16:26:39.997 回答