更新表MySql / PHP - 插入变量名而不是值

时间:2014-07-06 03:37:30

标签: php mysql forms

我对它进行了太多搜索,但是,我甚至不是PHP脚本中的新手......所以我需要帮助。

我正在开发一个基于PHP / MySql的自制家庭自动化系统,我需要一个后端来更新数据库表,让事情真正发生在现场。

所以,我找到了我需要的代码,经过一些调整,这里就是。

<?php
$username = "root";
$password = "";
$hostname = "localhost"; 
$database = "domonet";
$tabela = "AtuadorAnalogico";
$valor1 = "AA_id";
$valor2 = "AA_nome";
$valor3 = "AA_valor";

//connection to the database

$dbhandle = mysql_connect($hostname, $username, $password) 
  or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";


//select a database to work with

$selected = mysql_select_db($database,$dbhandle) 
  or die("Could not select examples");
  echo "Connected ao banco<br>";

//get data from db

$sql = mysql_query("SELECT * FROM $tabela");
$count=mysql_num_rows($sql);

//start a table

echo '<form name="form1" method="post" action="">
<table width="292" border="0" cellspacing="1" cellpadding="0">';

//start header of table

echo '<tr>
<th>&nbsp;</th>
<th>Name</th>
<th>Email</th>
</tr>';

//loop through all results

while($r=mysql_fetch_object($sql)){

//print out table contents and add id into an array and email into an array

echo '<tr>
<td><input type="hidden" name="id[]" value='.$r->$valor1.' readonly></td>
<td>'.$r->$valor2.'</td>
<td><input name="email[]" type="text" id="price" value="'.$r->$valor3.'"></td>
</tr>';
}

//submit button


echo' <tr>
<td colspan="3" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>';


// if form has been submitted, process it


if(isset($_POST["Submit"]))
{
   // get data from form

 $name = $_POST['name'];

   // loop through all array items

   foreach($_POST['id'] as $value)
   {

   // minus value by 1 since arrays start at 0      

           $item = $value-1;

                //update table

    //appending suggested changes  
    //place of var_dumps
   //$sql1 = mysql_query("UPDATE $tabela SET $valor3='$valor3[$item]' WHERE     $valor1='$value'") or die(mysql_error());
   $sql1 = mysql_query("UPDATE $tabela SET $valor3='$email[$item]' WHERE $valor1='$value'") or die(mysql_error());



   }

// redirect user

$_SESSION['success'] = 'Updated';
header("location:index.php");
}

问题是,点击“提交”按钮后,数据库表就变得很奇怪。

From This:
mysql> select * from AtuadorAnalogico;
+-------+---------+----------+
| AA_id | AA_nome | AA_valor |
+-------+---------+----------+
|     0 | nome1   | d        |
|     1 | nome2   | Agfd     |
|     2 | nome3   | Aasd     |
|     3 | nome4   | _asda    |
|     5 | teste5  | a        |
|     6 | teste6  | asda     |
|     7 | testte1 | o        |
|     8 | testte2 | rwqdq    |
|     9 | asdadsd | gwrg     |
|    10 | asdadsd | qdw      |
|    11 | adasd   | 234      |
|    12 | adasd   | g42      |
+-------+---------+----------+
12 rows in set (0.00 sec)

To This:
mysql> select * from AtuadorAnalogico
+-------+---------+----------+
| AA_id | AA_nome | AA_valor |
+-------+---------+----------+
|     0 | nome1   |          |
|     1 | nome2   |          |
|     2 | nome3   |          |
|     3 | nome4   |          |
|     5 | teste5  |          |
|     6 | teste6  |          |
|     7 | testte1 |          |
|     8 | testte2 |          |
|     9 | asdadsd |          |
|    10 | asdadsd |          |
|    11 | adasd   |          |
|    12 | adasd   |          |
+-------+---------+----------+
12 rows in set (0.00 sec)

拜托,我真的看不出我的错误。

非常感谢代码修订。

PS:只是要记住。当我在浏览器中运行php时,它会显示数据库表的内容。点击提交后,事情变得疯狂。

EDIT1

将'valor3'重命名为'email'后,它不再将字符串写入表格了。 但是,将整个列留空(在所有列单元格中写入空白,如上所示)。

EDIT2

在评论中,我们只是认为这是代码的逻辑问题。 只是无法弄清楚该怎么做。

更新

the output from just before the sql1 query:
var_dump($email);
var_dump($tabela);
var_dump($item);
var_dump($valor3);
var_dump($valor1);
var_dump($value);

vardump $email: NULL
vardump $tabela: string(16) "AtuadorAnalogico"
vardump $item: int(-1)
vardump $valor3: string(8) "AA_valor"
vardump $valor1: string(5) "AA_id"
vardump $value: string(1) "0"

vardump $email: NULL
vardump $tabela: string(16) "AtuadorAnalogico"
vardump $item: int(0)
vardump $valor3: string(8) "AA_valor"
vardump $valor1: string(5) "AA_id"
vardump $value: string(1) "1"
vardump $email: NULL

vardump $tabela: string(16) "AtuadorAnalogico"
vardump $item: int(1)
vardump $valor3: string(8) "AA_valor"
vardump $valor1: string(5) "AA_id"
vardump $value: string(1) "2" 

ps:我减少了行数,因此更容易看到和思考。

我发现了它!

堆栈不允许我回答我自己的问题,因此,我正在按照自己的意愿进行编辑。

将此行字段'name'和'id'重命名为“”anything“”:

<td><input name="valores[]" type="text" id="valores" value="'.$r->$valor3.'"></td>

将其重命名为与之前相同:

     $valores = $_POST['valores'];

将此更改为EQUAL(如@JA所述):

  $item = $value;

并按如下方式调整查询:

    $sql1 = mysql_query("UPDATE $tabela SET $valor3='$valores[$item]' WHERE $valor1='$value'") or die(mysql_error());

和IT完成了!

非常感谢你的头脑风暴。

特别感谢@arielnmz,他给了我转储变量的想法,以便我找到逻辑错误。

顺便说一句,代码完成了。

<?php
$username = "root";
$password = "";
$hostname = "localhost"; 
$database = "domonet";
$tabela = "AtuadorAnalogico";
$valor1 = "AA_id";
$valor2 = "AA_nome";
$valor3 = "AA_valor";

//connection to the database

$dbhandle = mysql_connect($hostname, $username, $password) 
  or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";


//select a database to work with

$selected = mysql_select_db($database,$dbhandle) 
  or die("Could not select examples");
  echo "Connected ao banco<br>";

//get data from db

$sql = mysql_query("SELECT * FROM $tabela");
$count=mysql_num_rows($sql);

//start a table

echo '<form name="form1" method="post" action="">
<table width="292" border="0" cellspacing="1" cellpadding="0">';

//start header of table

echo '<tr>
<th>&nbsp;</th>
<th>Name</th>
<th>Email</th>
</tr>';

//loop through all results

while($r=mysql_fetch_object($sql)){

//print out table contents and add id into an array and email into an array

echo '<tr>
<td><input type="hidden" name="id[]" value='.$r->$valor1.' readonly></td>
<td>'.$r->$valor2.'</td>
<td><input name="valores[]" type="text" id="valores" value="'.$r->$valor3.'"></td>
</tr>';
}

//submit button


echo' <tr>
<td colspan="3" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>';

// if form has been submitted, process it

if(isset($_POST["Submit"]))
{
       // get data from form

     $valores = $_POST['valores'];

       // loop through all array items

   foreach($_POST['id'] as $value)
       {
       // minus value by 1 since arrays start at 0 (not anymore)

               $item = $value;
                //update table

       $sql1 = mysql_query("UPDATE $tabela SET $valor3='$valores[$item]' WHERE     $valor1='$value'") or die(mysql_error());

   }

// redirect user

$_SESSION['success'] = 'Updated';
header("location:index.php");
};

每个人都有一个美好的夜晚!

1 个答案:

答案 0 :(得分:0)

按以下方式更改电子邮件的表单元素..

<input name="email['.$r->$valor1.']" type="text" id="price" value="'.$r->$valor3.'">

你可以摆脱“id []”元素

然后循环,

foreach($_POST['email'] as $id=>$value)

$sql1 = mysql_query('UPDATE '.$tabela.' SET '.$valor3.'="'.$value.'" WHERE '.$valor1.'="'.$id.'" LIMIT 1') or die(mysql_error());

确保在点击提交前在电子邮件字段中输入内容。