我有以下问题。我有一个包含多行的HTML表单。每行有4列(ID,NAME,LASTNAME,EMAIL)。我无法弄清楚我的代码中返回给我的具体问题:
您的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册,以便在'(名称,姓氏,电子邮件)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());
}
}
?>
答案 0 :(得分: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; ?>