dpgbh20688 2016-01-07 23:45 采纳率: 0%
浏览 32
已采纳

使用$ _POST从Select语句设置文本框值?

I'm currently trying to create a update type of script where a admin can click a update button, the text that's already in the database is shown in the textboxes & the admin can then replace the text if it's incorrect.

I currently have a error called "Undefined index". This is when i attempt to set a value to my text boxes from my select statement.

This is my current progress,

<?php
require 'configure.php';
if(isset($_GET['id']))
{
$id=$_GET['id'];
if(isset($_POST['Edit']))
{
    $sql = "UPDATE `pet` SET `pettype` = :petType WHERE id = :id";
    $statement = $pdo->prepare($sql);
    $id = $_GET['id'];
    $petType = $_POST['petType'];
    $statement->bindValue(':id', $id);
    $statement->bindValue(':petType', $petType);
    $update = $statement->execute();
if($sql)
{
header('location:index.php');
}
}
$jobID = $_GET['id'];
$stmt = $pdo->query('SELECT * FROM pet WHERE petType = "' . $jobID . '"');
$result = $stmt->fetch(PDO::FETCH_ASSOC);
}

?>

This is my update code, as well as my populate statement at the bottom.

<form method="post" action="">
Name:<input type="text" name="petID" value="<?php echo $_POST['petID'] ?>" /><br />
Age:<input type="text" name="petType" value="<?php echo $_POST['petType'] ?>" /><br /><br />
<br />
<input type="submit" name="submit" value="update" />
</form>

This is my Form which i'm using to edit the data.

I've tried

value="<?php echo ($pdo['petType']) ?>"
value="<?php echo $stmt['petType'] ?>"

Perhaps someone with a little more knoeledge would be able to help. Thank you

  • 写回答

1条回答 默认 最新

  • duangang1991 2016-01-07 23:55
    关注
    value="<?php echo $result['petType'] ?>"
    

    But a problem with your code is that you're just inserting the $jobID into the query without sanitizing it. This could lead to sql injection.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Acrn IVSHMEM doorbell问题
  • ¥15 yolov5中的val测试集训练时数量变小问题
  • ¥15 MPLS/VPN实验中MPLS的配置问题
  • ¥15 materialstudio氢键计算问题
  • ¥15 已知隐函数其中一个变量的,求另外一个变量
  • ¥15 echarts图表制作
  • ¥15 halcon根据玻璃面板纹路取区域
  • ¥15 HFSS设计小型化180度耦合器
  • ¥15 使用CInternetSession,CHttpFile读取网页文件时有些电脑上会卡住怎么办?
  • ¥15 水下机器人的半物理仿真研究