从选择选项字段中获取ID并在所选用户中更新它

时间:2018-04-06 14:33:39

标签: php html mysql

我在创建程序时遇到了一些问题, 我有一个选择字段,显示表用户的所有用户,我希望当我选择一个用户并按链接我希望它将用户userid从表属性更新为puserid

php代码:

//connect with the database
$con = mysql_connect("localhost","root","");
$db = mysql_select_db("ocm2inf_ocm",$con);
$get=mysql_query("SELECT * FROM users WHERE PortalId = '14'");
$option = '';

//create rows of the users
while($row = mysql_fetch_assoc($get))
{
  $option .= '<option value = "'.$row['UserId'].'">'.$row['firstName']. str_repeat('&nbsp;', 1). $row['UserId'].'</option>';
}

//update the row in the database after pressing the button
if(isset($_POST['linkownersandproperties']))
{
  $UserId = $_POST['UserIdOwner'];

  $sql = "UPDATE properties SET puserId='$UserId' WHERE propertyId=$id";
}

html代码:

<form class="create section" action="" method="post">
  <select class="form-control" name="UserIdOwner">
    <?php echo $option; ?>
  </select>
  <br />
  <button style="width:110px; margin-left:10px;" type="submit" name="linkownersandproperties" value="Submit" class="btn btn-primary btn-lg submit-button1" required="required">Link</button>
  </form>

提前致谢:)

1 个答案:

答案 0 :(得分:0)

更改此规则
$sql = "UPDATE properties SET puserId='$UserId' WHERE propertyId=$id";

遵守这条规则

$sql = mysql_query("UPDATE properties SET puserId='$UserId' WHERE propertyId='$id'");

将其放在$sql下的if语句

$query = mysql_query($sql, $db);
if (!$sql) {
    echo "Failed";
} else {
    echo "Executed";
}