更新多个字段时更新不推送到数据库

时间:2013-07-30 21:20:11

标签: php html mysql database mysqli

这与我的上一个程序相同,但我使用了更多的表和信息,但它是相同的想法,但是当我尝试在这里运行时,数据不会进入数据库,而是重新加载旧信息我试图发送的信息。

<?php
$page = "login";
$title = "LatinSoft - Login";

include "header.php";
include "config.php";


if (isset($_POST['submit']) && $_POST['submit'] == 'Update') {

$updateQuery = ("UPDATE `users` SET email = '$_POST[email]', f_name = '$_POST[f_name]', 
                 l_name = '$_POST[l_name]', m_name = '$_POST[m_name]', phone = '$_POST[phone]', 
                 address = '$_POST[address]', city = '$_POST[city]', state = '$_POST[state]', 
                 zip = '$_POST[zip]', rights = '$_POST[rights]', comp_name = '$_POST[comp_name]', 
                 comp_tel = '$_POST[comp_tel]', comp_add = '$_POST[comp_add]', 
                 comp_city = '$_POST[comp_city]', comp_state = '$_POST[comp_state]', 
               WHERE id = '$_POST[id]'");
//$link defined in config.php
mysqli_query($link, $updateQuery);


};

$query = ("SELECT `ID`, `email`, `f_name`, `l_name`, `m_name`, `phone`, `address`, `city`, `state`, `zip`, `rights`, `comp_name`, `comp_tel`, `comp_add`, `comp_city`, `comp_state` FROM `users`");
$result = mysqli_query($link, $query);

echo "<table width=10%  border=0 cellpadding=0 cellspacing=1>

<tr>
<th>Email</th> 
<th>Firstname</th>
<th>Lastname</th>
<th>Middle Name</th>
<th>Phone</th>
<th>Address</th>
<th>City</th>
<th>State</th>
<th>Zip</th>
<th>Rights</th>
<th>Company Name</th>
<th>Company Telephone</th>
<th>Company Address</th>
<th>Company City</th>
<th>Company State</th>
</tr>";

while($row = mysqli_fetch_array($result)) {
?>

<form method="post" action="updateuser.php">
<tr>
<td><input type="text" name="email" value="<?php echo  $row['email']; ?>"></td>
<td><input type="text" name="f_name" value="<?php echo $row['f_name']; ?>" ></td>
<td><input type="text" name="l_name" value="<?php echo $row['l_name']; ?>" ></td>
<td><input type="text" name="m_name" value="<?php echo $row['m_name']; ?>"></td>
<td><input type="text" name="phone" value="<?php echo $row['phone']; ?>" ></td>
<td><input type="text" name="address" value="<?php echo $row['address']; ?>" ></td>
<td><input type="text" name="city" value="<?php echo $row['city']; ?>" ></td>
<td><input type="text" name="state" value="<?php echo $row['state']; ?>" ></td>
<td><input type="text" name="zip" value="<?php echo $row['zip']; ?>" ></td>
<td><input type="text" name="rights" value="<?php echo $row['rights']; ?>" ></td>
<td><input type="text" name="comp_name" value="<?php echo $row['comp_name']; ?>" ></td>
<td><input type="text" name="comp_tel" value="<?php echo $row['comp_tel']; ?>" ></td>
<td><input type="text" name="comp_add" value="<?php echo $row['comp_add']; ?>" ></td>
<td><input type="text" name="comp_city" value="<?php echo $row['comp_city']; ?>" ></td>
<td><input type="text" name="comp_state" value="<?php echo $row['comp_state']; ?>" ></td>
<td><input type="hidden" name="id" value="<?php  echo $row['id'];  ?>"></td>
<td><input type="submit" name="submit" value="Update" ></td>
</tr>
</form>

<?php
}
include "footer.php";
?>

2 个答案:

答案 0 :(得分:0)

查看您的更新查询。

删除WHERE

之前的,
$updateQuery = ("UPDATE `users` SET email = '$_POST[email]', f_name = '$_POST[f_name]', 
             l_name = '$_POST[l_name]', m_name = '$_POST[m_name]', phone = '$_POST[phone]', 
             address = '$_POST[address]', city = '$_POST[city]', state = '$_POST[state]', 
             zip = '$_POST[zip]', rights = '$_POST[rights]', comp_name = '$_POST[comp_name]', 
             comp_tel = '$_POST[comp_tel]', comp_add = '$_POST[comp_add]', 
             comp_city = '$_POST[comp_city]', comp_state = '$_POST[comp_state]'
           WHERE id = '$_POST[id]'");

答案 1 :(得分:0)

您在这里遇到,

的错误
.... comp_state = '$_POST[comp_state]', WHERE ....

删除,并重试

.... comp_state = '$_POST[comp_state]' WHERE ....

那是错误但不要直接放入$ _POST [xxxx],因为SQL注入等......

像这样:

    $email = mysqli_real_escape_string($link, trim($_POST['email']));
    $f_name = mysqli_real_escape_string($link, trim($_POST['f_name']));

    $updateQuery = ("UPDATE `users` SET email = '$email', f_name = '$f_name'.... // and so on...