具有动态分配名称的文本区域会抛出我的代码 [英] Textareas with dynamically-assigned name throws my code
本文介绍了具有动态分配名称的文本区域会抛出我的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有许多 textareas,每个都有一个唯一的指定名称(例如 name="adcode$ID").当我尝试将这些名称传递给下面的代码时,由于动态部分,它不起作用.
I have a number of textareas, each with a unique assigned name (name="adcode$ID", for example). When I try to pass those names to the code below, it doesn't work because of the dynamic part.
if (isset($_POST['editadapp'])) { // Edit AD
$newadcode = mysql_real_escape_string($_POST['.adcode$ID.']);
$doedit = "UPDATE ads SET adcode = '".$newadcode."') WHERE ads_ID=$ID" or die(mysql_error());
$updatead = mysql_query($doedit) or die(mysql_error());
header("Location: " . $_SERVER['PHP_SELF']);
我该如何解决这个问题?
How can I resolve this?
推荐答案
这个错误太多了,令人害怕.
There is so much wrong with this that it's frightening.
首先
$doedit = "UPDATE ads SET adcode = '".$newadcode."') WHERE ads_ID=$ID" or die(mysql_error());
那个代码片段在很多层面上都是错误的.
That code snippet is wrong on many levels.
- sql 语法错误
- 该 sql 使用来自用户输入的字符串进行格式化(请参阅此处一个>
- 或 die() 不应在这里使用,您正在创建一个字符串
理想情况下,您应该使用以下代码:
Ideally you should have code like:
$dbh = new PDO('connectionstring to connect to your database');
$sql = 'update ads set adcode = ? where ads_id = ?';
$sth = $dbh->prepare($sql);
$sth->execute(array($_POST['adcode' . $ID], $ID));
其他主题:
这篇关于具有动态分配名称的文本区域会抛出我的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文