试图用 html 形式编辑一个 sql 行。我知道使用 mysqli 时我很容易受到 sql 注入的影响。但我对此相当陌生。我的目标是用一个 html 按钮调用一个 php 函数来更新行的值,然后返回到列表完成的页面。我了解到 ajax 是最好的方法,但无法开始工作。
编辑.php
<?php
try
{
$con = new PDO('mysql:host=localhost;dbname=SCMMM; charset=utf8', 'root', 'root');
}
catch(Exception $e)
{
die('Error : '.$e->getMessage());
}
if($_SERVER["REQUEST_METHOD"] === "POST") {
$id = $_GET['id'];
$nomeF = $_POST['nomeF'];
$nomeL = $_POST['nomeL'];
$Prof = $_POST['Profissao'];
$morada = $_POST['morada'];
$cod = $_POST['cod'];
$num = $_POST['num'];
$mail = $_POST['mail'];
$ordb = $_POST['ordb'];
$orde = $_POST['orde'];
$dataI = $_POST['dataI'];
$dataF = $_POST['dataF'];
$notas1 = $_POST['notas1'];
$notas2 = $_POST['notas2'];
$sql = "UPDATE Colaboradores SET (NomeF, NomeL, Profissao, Morada, CodPostal, Telemovel, mail, precoh, precohmais, dataI, dataF, notas1, notas2)
VALUES (:nomeF, :nomeL, :Prof, :morada, :cod, :num, :mail, :ordb, :orde, :dataI, :dataF, :notas1, :notas2) WHERE ColabID = $id";
// get row number and insert into the table**
$stmt = $con->prepare($sql);
$stmt->bindValue('nomeF', $nomeF, PDO::PARAM_STR);
$stmt->bindValue('nomeL', $nomeL, PDO::PARAM_STR);
$stmt->bindValue('Prof', $Prof, PDO::PARAM_STR);
$stmt->bindValue('morada', $morada, PDO::PARAM_STR);
$stmt->bindValue('cod', $cod, PDO::PARAM_STR);
$stmt->bindValue('num', $num, PDO::PARAM_INT);
$stmt->bindValue('mail', $mail, PDO::PARAM_STR);
$stmt->bindValue('ordb', $ordb, PDO::PARAM_INT);
$stmt->bindValue('orde', $orde, PDO::PARAM_INT);
$stmt->bindValue('dataI', $dataI, PDO::PARAM_STR);
$stmt->bindValue('dataF', $dataF, PDO::PARAM_STR);
$stmt->bindValue('notas1', $notas1, PDO::PARAM_STR);
$stmt->bindValue('notas2', $notas2, PDO::PARAM_STR);
$stmt->execute();
}
?>
编辑Colab.php
<?php
try
{
$con = new PDO('mysql:host=localhost;dbname=SCMMM; charset=utf8', 'root', 'root');
}
catch(Exception $e)
{
die('Error : '.$e->getMessage());
}
include("edit.php");
$id = $_GET['id'];
$query = "SELECT * FROM Colaboradores WHERE ColabID = $id";
$stmt = $con->prepare($query);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_BOTH);
?>
同页中的部分html表单editColab.php
<form id="Novocolab" method="post">
<div class="S">
<label for="notas2"><b>Notas</b></label>
<input type="text" placeholder="Introduzir Notas" name="notas2" value="<?php echo $row['notas2']?>">
</div>
<div class="S">
<button class="button" type="submit" value="submit">Concluído</button>
<button class="button" type="button" onclick="Reset()">Reset</button>
</div>
</div>
</form>
JS 运行在客户端浏览器上,PHP 运行在服务器端。当页面加载时,服务器首先处理页面上的任何 PHP 代码,然后将页面发送到浏览器,浏览器将解析 html/css 并执行设置为在页面完成加载时运行的任何 JS。
表单的目标属性需要设置为您向我们展示的 PHP 脚本。这意味着当浏览器提交表单时,它会将表单数据发送到该 PHP 脚本,该脚本将能够读取 $_POST 数据并处理您的更新。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句