我正在为一个客户端开发一个简单的项目,但是我无法在mysql中编辑和更新数据。这是我从编辑提交表单传递的代码:
updateinfo.php
<?php
$con = mysqli_connect("localhost", "user", "password", "db");
if (mysqli_connect_errno()){
echo "Failed to connect to MySql: " . mysqli_connect_error();
}
$id = $row['id'];
$fname = $row['firstName'];
$sql = "UPDATE `db`.`client_information` SET `firstName` = '$fname' WHERE `client_information`.`id` = '$id'";
mysqli_query($con, $sql);
echo "Updated!";
mysqli_close($con);
?>
clientedit.php
<?php
$con = mysqli_connect("localhost", "user", "password", "db");
if (mysqli_connect_errno()){
echo "Failed to connect to MySql: " . mysql_connect_error();
}
$id=$_GET['id'];
$result = mysqli_query($con, "SELECT * FROM `client_information` WHERE id='$id'");
while ($row = mysqli_fetch_array($result)){
$id = $row['id'];
$fname = $row['firstName'];
}
mysqli_close($con);
?>
<?php include 'header.php';?>
<form role="form" action="updateinfo.php" method="post">
<div class="col-md-4">
<fieldset disabled>
<div class="form-group">
<label for="id">ID</label>
<input type="text" value="<?php echo $id;?>" class="form-control" id="id">
</div>
</fieldset>
<div class="form-group">
<label for="fname">First Name</label>
<input type="text" value="<?php echo $fname;?>" class="form-control" id="fname">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</div>
</form>
<?php include 'footer.php';?>
提前感谢您的帮助!
更新
更改变量后,当我通过表单(clientedit.php)传递信息时,updateinfo.php会使用新信息更新表。但是,它只有在我说:&#34; SELECT * FROM client_information
WHERE id
=&#39; 31&#39;时才有效。如果我将其更改为:&#34; SELECT * FROM client_information
WHERE id
=&#39; $ id&#39;&#34;,则它会完全失败。我知道我不是动态的#34;将文件连接到文件。
答案 0 :(得分:1)
向下滚动并开始阅读新答案的位置。 如果您的PHP代码在同一页面中,则需要更改表单操作,如此
如果没有像你拥有的那样离开它。
<form role="form" action="" method="post">
你需要为这两个输入命名,如
<input type="text" value="<?php echo $id;?>" class="form-control" id="id" name="id">
<input type="text" value="<?php echo $fname;?>" class="form-control" id="fname" name="fname">
并且您在表单中使用post方法,因此您的$ id应该是这样的
$id=$_POST['id'];
为这样的第二场比赛做同样的事情
$fname=$_POST['fname'];
新答案
我们昨天唯一错过的。是你正在使用禁用字段集。输入费用。删除这个。您的查询代码和查询应该有效。请参阅下面的代码。并且不要忘记改变连接信息,因为我的不同于你的。
因为我假设您不希望用户更改其ID。只需在输入中使用隐藏属性。不要禁用
更新一个
<?php
//you may have to change the value in connection variable.
$con = mysqli_connect("localhost", "root", "", "db");
if (mysqli_connect_errno()){
echo "Failed to connect to MySql: " . mysqli_connect_error();
}
$id = $_POST['id'];
$fname = $_POST['fname'];
$sql = "UPDATE `db`.`client_information` SET `firstName` = '$fname' WHERE `client_information`.`id` = '$id'";
mysqli_query($con, $sql) or mysqli_error($con);
echo "Updated!";
mysqli_close($con);
?>
点击
<?php
//again you might have to change connection value in the below line
$con = mysqli_connect("localhost", "root", "", "db");
if (mysqli_connect_errno()){
echo "Failed to connect to MySql: " . mysql_connect_error();
}
$id=$_GET['id'];
$result = mysqli_query($con, "SELECT * FROM `client_information` WHERE id='$id'");
while ($row = mysqli_fetch_array($result)){
$id = $row['id'];
$fname = $row['firstName'];
}
mysqli_close($con);
?>
<?php include 'header.php';?>
<form role="form" action="updateinfo.php" method="post">
<div class="col-md-4">
<fieldset >
<div class="form-group">
<label for="id">ID</label>
<input type="text" value="<?php echo $id;?>" class="form-control" id="id" name="id">
</div>
</fieldset>
<div class="form-group">
<label for="fname">First Name</label>
<input type="text" value="<?php echo $fname;?>" class="form-control" id="fname" name="fname">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</div>
</form>
<?php include 'footer.php';?>
答案 1 :(得分:1)
尝试一下,看看这是否是您正在寻找的,它是有效的。我出于某种原因无法让你工作。
它将首先查询您的数据库,然后您可以立即编辑您喜欢的数据库。
N.B。:只是更新现有数据而不是添加或删除。
我修改了我在网络上找到的现有脚本,该脚本有一些缺失的部分,以使其正常工作。
这些列已用于以下内容:
HTML / PHP / SQL (update.php)
<?php
$DB_HOST = "xxx";
$DB_NAME = "xxx";
$DB_USER = "xxx";
$DB_PASS = "xxx";
$con = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if($con->connect_errno > 0) {
die('Connection failed [' . $con->connect_error . ']');
}
$sql="SELECT * FROM `your_table`";
$result=mysqli_query($con,$sql);
// Count table rows
$count=mysqli_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=mysqli_fetch_array($result)){
?>
<tr>
<td align="center">
<?php $id[]=$rows['id']; ?><?php echo $rows['id']; ?>
</td>
<td align="center">
<input name="name[]" type="text" id="name" value="<?php echo $rows['firstName']; ?>">
</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="Submit" value="Submit"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php
if(isset($_POST['Submit'])){
for($i=0;$i<$count;$i++){
// these variables were missing from the tutorial. Not having those made entries blank when doing the UPDATE.
$email = $_POST['email'];
$name = $_POST['name'];
$lastname = $_POST['lastname'];
$sql1="UPDATE `your_table` SET firstName='".mysqli_real_escape_string($con, stripslashes($name[$i]))."', lastName='".mysqli_real_escape_string($con, stripslashes($lastname[$i]))."', email='".mysqli_real_escape_string($con, stripslashes($email[$i]))."' WHERE id='".mysqli_real_escape_string($con, stripslashes($id[$i]))."'";
// Old unsecure method. Do not use mysqli_real_escape_string and/or stripslashes as predefined variables, it won't work
// $sql1="UPDATE `your_table` SET firstName='$name[$i]', lastName='$lastname[$i]', email='$email[$i]' WHERE id='$id[$i]'";
$result1=mysqli_query($con,$sql1);
}
}
if($result1){
header("location: update.php");
}
mysqli_close($con);
?>