获取数据库更新的用户ID - PHP / MySQL

时间:2015-08-25 14:52:39

标签: php html mysql database

有一件事我不明白。我有一个PHP表单来更新我的数据库条目。问题是,我不知道如何做到这一点,而不仅仅是更新特定的ID。让我解释一下我的问题:

我有一个用户表。当我点击编辑用户时,我将被重定向到edit.php?id = 3(例如,如果我想编辑ID为3的用户)。现在,在编辑页面上,我进行了更改,然后单击“更新”按钮。如果我这样做,我的数据库中的所有条目都会更新。在我看来,问题是我必须在我的查询中输入类似“WHERE id ='$ user_id'”的东西,但我对PHP很新,所以我不知道变量$ user_id应该是什么样的,以便它正常工作。

如果我没有输入WHERE语句,则会更新所有条目。如果我输入WHERE id ='$ user_id',则不会更新任何条目。让我告诉你我的代码:

<?php
session_start();
// Create connection credentials
$db_host = 'localhost';
$db_name = 'DBNAME';
$db_user = 'DBUSER';
$db_pass = 'DBPASS';

// Create mysqli object
$connect = new mysqli ($db_host, $db_user, $db_pass, $db_name);

// Error Handler
if ($connect->connect_error) {
    printf ("Connection failed: %s\n", $connect->connect_error);
    exit();
}   


// Check if form is submitted
if (isset ($_POST['updatelehrer'])) {
    $user_id = $_POST['user_id']; //Updated due to a comment 
    $update_firstname_teacher = mysqli_real_escape_string ($connect, $_POST['lehrer_vorname'] );
    $update_lastname_teacher = mysqli_real_escape_string ($connect, $_POST['lehrer_nachname'] );
    $update_street_teacher = mysqli_real_escape_string ($connect, $_POST['lehrer_strasse'] );
    $update_plz_teacher = mysqli_real_escape_string ($connect, $_POST['lehrer_plz'] );
    $update_city_teacher = mysqli_real_escape_string ($connect, $_POST['lehrer_ort'] );
    $update_phonenumber_teacher = mysqli_real_escape_string ($connect, $_POST['lehrer_telefon'] );
    $update_bank_teacher = mysqli_real_escape_string ($connect, $_POST['lehrer_bank'] );
    $update_inhaber_teacher = mysqli_real_escape_string ($connect, $_POST['lehrer_inhaber'] );
    $update_iban_teacher = mysqli_real_escape_string ($connect, $_POST['lehrer_iban'] );
    $update_bic_teacher = mysqli_real_escape_string ($connect, $_POST['lehrer_bic'] );

}


$sql = mysqli_query ($connect, "UPDATE `teacher` SET 
     firstname='".$update_firstname_teacher."', lastname='".$update_lastname_teacher."', street='".$update_street_teacher."', plz='".$update_plz_teacher."', city='".$update_city_teacher."', phonenumber='".$update_phonenumber_teacher."', bankaccount='".$update_bank_teacher."', bankowner='".$update_inhaber_teacher."', iban='".$update_iban_teacher."', bic='".$update_bic_teacher."'  WHERE id = '$user_id'");


if (mysqli_affected_rows($connect) == 0) //<--
{
    die('Could not update data: ' . mysqli_error($connect));
} else {
   header("Location: admin.php");// send to profile overview page
   exit;
}
mysqli_close($connect); 

?>

有人可以告诉我我能做什么,以便只更新具有特定ID的特定用户?我非常感谢大家的帮助。

编辑: 我更新了上面的代码并插入了$ _POST ['user_id']。这是我表格的一部分:

<form method="POST" action="update_profile_teacher.php" class="form-horizontal form-label-left">

<div class="form-group">
 <label class="control-label col-md-3 col-sm-3 col-xs-12">ID (versteckt):</label>
  <div class="col-md-9 col-sm-9 col-xs-12">
   <input type="hidden" name="user_id" class="form-control" value="<?php echo $_SESSION['data']['id']; ?>">
  </div>
</div>

<div class="form-group">
 <label class="control-label col-md-3 col-sm-3 col-xs-12">Passwort:</label>
  <div class="col-md-9 col-sm-9 col-xs-12">
     <input type="password" name="lehrer_passwort" class="form-control" value="passwordonetwo">
  </div>
 </div>

<div class="form-group">
 <label class="control-label col-md-3 col-sm-3 col-xs-12">E-Mail:</label>
   <div class="col-md-9 col-sm-9 col-xs-12">
    <input type="email" class="form-control" name="lehrer_email" value="<?php echo $_SESSION['data']['email']; ?>" placeholder="E-Mail Adresse">
   </div>
 </div>
</form>

编辑2: 以下是我在编辑页面上获取$ user_id的代码

<?php
$user_id = $_GET['id'];
$get_user = "SELECT * FROM teacher WHERE id = '$user_id'";
$result_get_user = mysqli_query($connect, $get_user);
$_SESSION['data'] = mysqli_fetch_assoc($result_get_user);
?>

2 个答案:

答案 0 :(得分:1)

这只是示范,因为我不知道你的html标记或它的创建。

向表单添加隐藏的输入字段:

<input type="hidden" name="userId" value="<?php echo $userId; ?>">

在您的php代码中,您可以在表单提交后访问userId 不要忘记逃避您的数据!

$userId = $_POST['userId'];

答案 1 :(得分:-1)

我在SQL语句中看到一个错误,尝试更改此行:

$sql = mysqli_query ($connect, "UPDATE `teacher` SET 
     firstname='".$update_firstname_teacher."', lastname='".$update_lastname_teacher."', street='".$update_street_teacher."', plz='".$update_plz_teacher."', city='".$update_city_teacher."', phonenumber='".$update_phonenumber_teacher."', bankaccount='".$update_bank_teacher."', bankowner='".$update_inhaber_teacher."', iban='".$update_iban_teacher."', bic='".$update_bic_teacher."'  WHERE id = '$user_id'");

进入这个:

$sql = mysqli_query ($connect, "UPDATE `teacher` SET 
     firstname='".$update_firstname_teacher."', lastname='".$update_lastname_teacher."', street='".$update_street_teacher."', plz='".$update_plz_teacher."', city='".$update_city_teacher."', phonenumber='".$update_phonenumber_teacher."', bankaccount='".$update_bank_teacher."', bankowner='".$update_inhaber_teacher."', iban='".$update_iban_teacher."', bic='".$update_bic_teacher."'  WHERE id = '".$user_id."'");

你应该在WHERE部分看到这两者之间的区别。