dtnpf35197 2017-11-10 05:40
浏览 72

PHP更新表单,仅当使用PDO在该特定字段中存在输入时才更新数据库信息

I am currently working on a form that uses PHP and SQL to update information in a database. It is functioning properly and updating the information but the issue is... is that it updates everything, including fields that I didn't even put any input in which means it will only update a particular row in the database and leave the others blanks... I need it to just change information from a field with an actual input and leave it if there is no input.

Here is the PHP and SQL code:

try {

$deleteRecId = $_GET['id'];
$update_event_name = $_POST['updateName'];
$update_event_location = $_POST['updateLocation'];
$update_event_date = $_POST['updateDate'];

include 'connect.php';

if(isset($_POST["submit"])) {

// new data
$sql = "UPDATE events SET event_name='$update_event_name', 
event_location='$update_event_location', event_date='$update_event_date' 
WHERE event_id=$deleteRecId";

// Prepare statement
$stmt = $conn->prepare($sql);

// execute the query
$stmt->execute();

// echo a message to say the UPDATE succeeded
echo $stmt->rowCount() . " records UPDATED successfully";
}
}

catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}

and here if the form:

<form class="update-form" action="<?php echo $_PHP_SELF ?>" method="post">

  <p id="input-headers">Event Name</p>

  <p id="update-input-field-wrapper">
    <input type="text" name="updateName" value="">
  </p>

  <p id="input-headers">Event Location</p>

  <p id="update-input-field-wrapper">
    <input type="text" name="updateLocation" value="">
  </p>

  <p id="input-headers">Event Date</p>

  <p id="update-input-field-wrapper">
    <input type="text" name="updateDate" value="" placeholder="01/01/2000">
  </p>

  <input type="submit" name="submit" value="Submit" id="updateBtn">

</form>

So to sum up I need this application to only update information of a field with an actual input and if the form field has no input I need that database info to remain the same. I appreciate any help with this as I am pretty new to these concepts... thanks!

  • 写回答

2条回答 默认 最新

  • duanjuebiao6730 2017-11-10 05:44
    关注

    Using your current code structure, you can do this.

    1. Use SQL to select * from event ID. Populate your update_event_xxx with the parameters.

    2. If $_POST[xx] is blank, ignore. Else, update_event_xx = $_POST[xx]

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大