php - 使用 HTML 表格形式进行多行更新查询

标签 php mysql html-table multirow

我遇到以下问题。我有一个包含多行的 HTML 表单。每行有 4 列(ID、NAME、LASTNAME、EMAIL)。我无法在我的代码中找出返回给我的具体问题:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(name,lastname,email) VALUES ('.('Billly','Blueton1','bb5@phpeasystep.com'),('J' at line 1

我正在尝试使用单个提交查询插入多个更新。我认为我已经接近解决方案,但我陷入困境,因为我不是编程语言专家。任何帮助;我们将不胜感激。

这是我的代码:

    <?php

   $con = mysql_connect("localhost","root","");
   mysql_select_db("test");



$sql="SELECT * FROM test_mysql";
$result=mysql_query($sql);

// Count table rows
$count=mysql_num_rows($result);

?>

<table width="500" border="0" cellspacing="1" cellpadding="0">
<form name="form1" method="post" action="" >
<tr>
<td>
<table width="500" border="0" cellspacing="1" cellpadding="0">

<tr>
<td align="center"><strong>Id</strong></td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Lastname</strong></td>
<td align="center"><strong>Email</strong></td>
</tr>

<?php
while($rows=mysql_fetch_array($result)){
?>

<tr>
<td align="center">
<? $id[]=$rows['id']; ?><?php echo $rows['id'];?>
</td>
<td align="center">
<input name="name[]" type="text" id="name"  value='<?php echo $rows['name']; ?>'>
</td>
<td align="center"> 
<input name="lastname[]" type="text" id="lastname" value='<?php echo $rows['lastname']; ?>'>
</td>
<td align="center">
<input name="email[]" type="text" id="email" value='<?php echo $rows['email']; ?>'>
</td>
</tr>

<?php
}
?>

<tr>
<td colspan="4" align="center"><input type="submit" name="submit1" value="ΕΝΗΜΕΡΩΣΗ"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>

<?php

 if(isset($_POST['name'])){
       foreach($_POST['name'] as $row=>$Name)
       {
           $id = intval($rows['id']);   
           $name = mysql_real_escape_string($Name);
           $lastname=mysql_real_escape_string($_POST['lastname'][$row]);
           $email = mysql_real_escape_string($_POST['email'][$row]);

           $row_data[]="('$name','$lastname','$email')"; 
            $implodeArray = implode(",", $row_data);

       }
    if(!empty($row_data)){
           $query = "UPDATE test_mysql (name,lastname,email)  VALUES ('.$implodeArray.') WHERE id='$id'" or die(mysql_error());
           $result1 = mysql_query($query)or die(mysql_error());
        }   
 }
?>

最佳答案

您的代码存在很多问题。您执行的查询错误,并且看起来您错误地使用了某些变量。此外,在定义 $query 变量后,您不需要 or die(mysql_error()) 。试试这个:

<?php

if (isset($_POST['name'])) {
    foreach ($_POST['name'] as $i => $name) {

        $id = intval($_POST['id'][$i]);
        $name = mysql_real_escape_string($name);
        $lastname = mysql_real_escape_string($_POST['lastname'][$i]);
        $email = mysql_real_escape_string($_POST['email'][$i]);

        mysql_query("UPDATE test_mysql SET name='$name', lastname='$lastname', email='$email' WHERE id=$id") or die(mysql_error());
    }   
}

?>

并将 while 循环中的表更改为:

<?php while ($row = mysql_fetch_array($result)) : ?>

<tr>
    <td align="center">
        <?php
            $id[] = $rows['id'];
            echo $rows['id'];
        ?>
        <input type="hidden" name="id[]" value="<?php echo $rows['id']; ?>" />
    </td>
    <td align="center">
        <input name="name[]" type="text" id="name"  value="<?php echo $rows['name']; ?>" />
    </td>
    <td align="center"> 
        <input name="lastname[]" type="text" id="lastname" value="<?php echo $rows['lastname']; ?>" />
    </td>
    <td align="center">
        <input name="email[]" type="text" id="email" value="<?php echo $rows['email']; ?> /">
    </td>
</tr>

<?php endwhile; ?>

关于php - 使用 HTML 表格形式进行多行更新查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19538936/

相关文章:

php - Paypal 动态按钮和与 IPN 的链接

php - 在哪里存储 docker-php-ext 文件?

php - Smarty MYSQL 数组关联 ID

java - 将参数传递给 JDBC PreparedStatement

mysql - 使用 linq 从 DataTable 获取 bool 值

css - 小型 HTML 表格问题

html - 并排放置两个表 TR

php - 我如何使用元数据 API 使用 PHP 将自定义字段放在标准 SalesForce 对象上

php - 下拉框第一行空白值

javascript - 表格标题不会滚动到页面顶部之外?