0

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

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在 '(name,lastname,email) VALUES ('.('Billly','Blueton1','bb5@phpeasystep.com'),('J ' 在第 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());
        }   
 }
?>
4

1 回答 1

1

你的代码有很多问题。你做错了查询,看起来你不正确地使用了一些变量。此外,您不需要or die(mysql_error())在定义$query变量后。尝试这个:

<?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; ?>
于 2013-10-23T10:45:21.263 回答