dpgbh20688 2016-01-07 23:45
浏览 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 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义
  • ¥15 嵌入式--定时器使用
  • ¥20 51单片机学习中的问题
  • ¥30 Windows Server 2016利用兩張網卡處理兩個不同網絡
  • ¥15 Python中knn问题
  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题