質問

I have the following problem.I have an HTML form with multiple rows. Every row has 4 columns (ID,NAME,LASTNAME,EMAIL). I cannot figure out in my code which is the specific problem that is returned to me:

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

I am trying to insert with a single submit query multiple updates. I think I am close to the solution but I am stuck because I am not expert in programming languages. Any help; it would be appreciated.

HERE IS MY CODE :

    <?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());
        }   
 }
?>
役に立ちましたか?

解決

You have a lot of problems with your code. You are doing the query wrong, and it looks like you are using some of the variables incorrectly. Also, you do not need or die(mysql_error()) after defining your $query variable. Try this:

<?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());
    }   
}

?>

And change your table in the while loop to this:

<?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; ?>
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top