如何在PHP中使用post方法捕获表中每个用户的特定主ID?
问题描述:
我对编程完全陌生。基本上我正在研究PHP和MySQL的crud操作。如何在PHP中使用post方法捕获表中每个用户的特定主ID?
下面是我从数据库中检索数据的代码。请注意,我使用隐藏输入字段来捕获每个用户的主键(id
)。
<div class="table-responsive">
<table id="datatable-buttons" class="table table-striped jambo_table bulk_action">
<thead>
<tr class="headings">
<th class="column-title">Client Name</th>
<th class="column-title">PAN </th>
<th class="column-title">GST Registration Type </th>
<th class="column-title no-link last"><span class="nobr">Add</span>
</th>
</tr>
</thead>
<?php
$sql = mysqli_query($conn,'select * from customer_master');
while ($row = mysqli_fetch_array($sql))
{?>
<tbody>
<tr class="even pointer">
<td class=" "><?php echo $row['companyName'];?></td>
<td class=" "><?php echo $row['pan'];?></td>
<td class=" "><?php echo $row['gstTaxType'];?></td>
<td><input type="hidden" name="pid" value="<?php echo $row['customerId'];?>"></a> | <a type="" class="" data-toggle="modal" data-target="#edit_customer_profile"><i class="fa fa-pencil" aria-hidden="true"></i></a></td>
</tr>
</tbody>
<?php }?>
</table>
</div>
当点击data-target="#edit_customer_profile"
秒模式将 只对特定customerId
开放数据。
为此,我在第二个模态中使用了这个查询。
<?php $sql = mysqli_query($conn,'select * from customer_master');
while ($row = mysqli_fetch_array($sql)) {?>
因此,请帮助我检索选定用户的唯一数据。
答
您必须在SELECT语句中使用WHERE子句。一定要使用prepared语句来避免SQL注入。
这里从[PHP-文档]一个例子(http://php.net/manual/de/mysqli.prepare.php)改变你的需求:
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
/* create a prepared statement */
if ($stmt = $mysqli->prepare("SELECT * FROM customer_master WHERE customerId=?")) {
/* bind parameters for markers */
$stmt->bind_param("i", $_POST['customerId']);
/* execute query */
$stmt->execute();
/* bind result variables */
$stmt->bind_result($customerId);
$stmt->bind_result($companyName);
$stmt->bind_result($pan);
$stmt->bind_result($gstTaxType);
/* fetch value */
$stmt->fetch();
/* close statement */
$stmt->close();
}
/* close connection */
$mysqli->close();
?>
<div class="table-responsive">
<table id="datatable-buttons" class="table table-striped jambo_table bulk_action">
<thead>
<tr class="headings">
<th class="column-title">Client Name</th>
<th class="column-title">PAN </th>
<th class="column-title">GST Registration Type </th>
<th class="column-title no-link last"><span class="nobr">Add</span></th>
</tr>
</thead>
<tbody>
<tr class="even pointer">
<td class=" "><?php echo $companyName;?></td>
<td class=" "><?php echo $pan;?></td>
<td class=" "><?php echo $gstTaxType;?></td>
<td><input type="hidden" name="pid" value="<?php echo $customerId;?>"></a> | <a type="" class="" data-toggle="modal" data-target="#edit_customer_profile"><i class="fa fa-pencil" aria-hidden="true"></i></a></td>
</tr>
</tbody>
</table>
</div>
'从customer_master选择*,其中客户ID = $ _ POST [' 客户ID ']' 是你寻找类似这个 ? –
对不起,我只是在寻找这个。 –
它工作吗? @amit –