简体   繁体   English

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

[英]Setting Textbox Value from a Select Statement with $_POST?

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. 这是当我尝试从我的select语句中为我的文本框设置值时。

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 谢谢

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. 但是您的代码存在一个问题,就是您只是将$ jobID插入查询中而没有对其进行清理。 This could lead to sql injection. 这可能导致sql注入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM