我遇到了问题,我不知道自己做错了什么。我想将数据库中的值输出到表单中,我得到的只是一个空白页面。值应该在每个用户的表单内,因此我使用用户ID。这是我做的:
<?php
include 'inc/database.php';
$memberID = "SELECT id FROM admin";
$result_member_ID = mysqli_query ($connect, $memberID);
$get_admin = "SELECT * FROM admin WHERE id = '$result_member_ID'";
$result_get_admin = mysqli_query($connect, $get_admin);
$_SESSION['data'] = mysqli_fetch_assoc($result_get_admin);
?>
现在在我的表单中,字段看起来像这样:
<form method="post" action="edit_mitarbeiter.php" class="form-horizontal form-label-left">
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Benutzername: </label>
<div class="col-md-9 col-sm-9 col-xs-12">
<input type="text" name="username" class="form-control" placeholder="Geben Sie einen Benutzernamen an." value="<?php echo $_SESSION['data']['username']; ?>">
</div>
</div>
</form>
现在我得到的是一个空白页面,因为我的错误在这里:
$memberID = "SELECT id FROM admin";
$result_member_ID = mysqli_query ($connect, $memberID);
这个变量有什么问题?我想从用户那里获取ID,之后我选择id = $result_member_ID
的所有用户!
有人能告诉我我做错了吗?
编辑: 这是我目前获取记录的php代码。我收到一条错误消息,我将在下面发布。代码:
include 'inc/database.php';
$memberID = "SELECT id FROM admin";
$result_member_ID = mysqli_query ($connect, $memberID);
$get_admin = "SELECT * FROM admin WHERE id = '$result_member_ID'";
$result_get_admin = mysqli_query($connect, $get_admin);
while($dataarray = mysqli_fetch_assoc($result_get_admin)) //fetch multiple data
{
$_SESSION['data'] = $dataarray ['id'];
$_SESSION['name'] = $dataarray ['username'];
}
错误讯息: 可捕获的致命错误:第23行的/var/customers/webs/ccsolution/work/schulkantine/admin/edit_mitarbeiter.php中无法将类mysqli_result的对象转换为字符串
第23行:
$get_admin = "SELECT * FROM admin WHERE id = '$result_member_ID'";
编辑2: 现在它正在工作,正确的数据显示在输入字段内。现在的问题是,只要我点击提交按钮,我就会收到一条成功消息,但数据库中的数据没有改变。请查看我的代码现在的样子:
<?php
include 'inc/database.php';
$result_member_ID = $_GET['id']; //receive the id from page
$get_admin = "SELECT * FROM admin WHERE id = '$result_member_ID'";
$result_get_admin = mysqli_query($connect, $get_admin);
$_SESSION['data'] = mysqli_fetch_assoc($result_get_admin);
// Check if form is submitted
if (isset ($_POST['editmitarbeiter'])) {
$update_username = mysqli_real_escape_string ($connect, $_POST['username']);
$update_password = mysqli_real_escape_string ($connect, $_POST['password']);
$update_email = mysqli_real_escape_string ($connect, $_POST['email']);
$update_vorname = mysqli_real_escape_string ($connect, $_POST['vorname']);
$update_nachname = mysqli_real_escape_string ($connect, $_POST['nachname']);
$update_telefon = mysqli_real_escape_string ($connect, $_POST['telefon']);
$sql = mysqli_query ($connect, "UPDATE `admin` SET
username='".$update_username."', password='".$update_password."', email='".$update_email."', vorname='".$update_vorname."', nachname='".$update_nachname."', telefon='".$update_telefon."' WHERE id = $result_member_ID");
if (mysqli_affected_rows($connect) == 0) //<--
{
die('Could not update data: ' . mysqli_error($connect));
} else {
$msg_success= '<strong>Gratulation!</strong> Der Mitarbeiter wurde erfolgreich hinzugefügt. Zur <a href="mitarbeiter.php"><span style="color:#fff;">Übersicht aller Mitarbeiter >></span></a>';
}
}
?>
为什么更改未保存在数据库中的任何想法?
谢谢, 克里斯
答案 0 :(得分:2)
<?php
include 'inc/database.php';
$result_member_ID = $_GET['id']; //receive the id from page
$get_admin = "SELECT * FROM admin WHERE id = '$result_member_ID'";
$result_get_admin = mysqli_query($connect, $get_admin);
while($dataarray = mysqli_fetch_assoc($result_get_admin)) //fetch multiple data
{
$_SESSION['data'] = $dataarray ['id'];
$_SESSION['name'] = $dataarray ['username'];
}
接收多个数据时必须使用while循环。无法在一行中获取一组数据。
更新数据库时遇到的第二个问题失败,查询中的$result_member_ID
原因位于if (isset ($_POST['editmitarbeiter']))
内,因此未获得该值
所以解决方案是
添加这样的隐藏输入并将其放入表单
<input type="hidden" value="<?php echo $result_member_ID;?>" name="member_Id">
在表单与其他输入一起提交时发布
$member_Id = mysqli_real_escape_string ($connect, $_POST['member_Id']);
将查询更改为此
$sql = mysqli_query ($connect, "UPDATE `admin` SET
username='$update_username',
password='$update_password',
email='$update_email',
vorname='$update_vorname',
nachname='$update_nachname',
telefon='$update_telefon'
WHERE id = '$member_Id'");
答案 1 :(得分:1)
不确定PHP但是你检查了你的查询;根本没有意义。
$memberID = "SELECT id FROM admin";
$result_member_ID = mysqli_query ($connect, $memberID);
$get_admin = "SELECT * FROM admin WHERE id = '$result_member_ID'";
这应该在下面,您应该使用IN
运算符
SELECT * FROM admin WHERE id IN ('$result_member_ID'")
但这与说
相同SELECT * FROM admin WHERE id IN ( select id from admin)
(OR)就在下面,因为您选择了每条记录
SELECT * FROM admin