- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有人可以仔细检查一下我的代码,找出为什么我会得到这个:
Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
我知道这将是非常简单的事情,但我看不到它。
<body>
<?php
//connect to database//
$dbc = mysql_connect("localhost", "root", "***");
if (!$dbc)
die ('Could not connect: ' . mysql_error());
//select database//
$db_selected = mysql_select_db("tafe", $dbc );
if (!$db_selected)
die ('Could not connect: ' . mysql_error());
// initialise variables to store form control values
$Name = "";
$Address = "";
$Phone = "";
$Mobile = "";
$Email = "";
if($_SERVER['REQUEST_METHOD'] == "POST") // if form has been posted
{
// initialise variables to store posted values
$ContactID = $_POST["ContactID"];
$Name = $_POST["Name"];
$Address = $_POST["Address"];
$Phone = $_POST["Phone"];
$Mobile = $_POST["Mobile"];
$Email = $_POST["Email"];
//build sql insert statement
$qry = "UPDATE contacts SET Name = '" . $Name . "', Address = '" . $Address . "', Phone = '" . $Phone . "', Mobile = '" . $Mobile . "', Email = '" . $Email . "' WHERE ContactID =" . $ContactID;
// run insert statement against database
$rst = mysql_query($qry, $dbc);
if ($rst)
{
echo "<b><font color='green'>The contact has been updated.</font></b>";
echo "</br></br>";
echo "<a href=list-contacts.php>Continue</a>";
}
else
{
echo "<b><font color='red'>Error: ". mysql_error($dbc) . "</font></b>"; //alert if contact could not be added//
}
}
else // if form has not been posted
{
// build sql statement
$qry = "SELECT * FROM contacts WHERE ContactID = " . $_GET["ContactID"];
// run select statement
$rst = mysql_query($qry, $dbc);
if ($rst)
{
$row = mysql_fetch_assoc($rst); // fetch row and place column values into respective place holder variable
$Name = $row["Name"];
$Address = $row["Address"];
$Phone = $row["Phone"];
$Mobile = $row["Mobile"];
$Email = $row["Email"];
}
else // in case of an error
{
echo "<b><font color='red'>Error: ". mysql_error($dbc) . "</font></b>";
} // end of nested else statement ?>
<form name="editcontact" method="post" action="edit-contact.php">
<table border="1" cellpadding="2">
<caption> Caption 5</caption>
<!--Name Input-->
<tr>
<td><label for="Name">Name</label></td>
<td><input type="text" name="Name" value="<?php echo $Name ?>" size="30" maxlength="50" tabindex="1"/>
</td>
</tr>
<!-- Address Input-->
<tr>
<td><label for="Address">Address</label></td>
<td><textarea name="Address" cols="45" rows="5" tabindex="2"><?php echo $Address?></textarea></td>
</tr>
<!--Phone Input-->
<tr>
<td><label for="Phone">Phone</label></td>
<td><input type="text" name="Phone" value="<?php echo $Phone ?>" size="20" maxlength="20" tabindex="3" /> </td>
</tr>
<!--Mobile Input-->
<tr>
<td><label for="Mobile">Mobile</label></td>
<td><input type="text" name="Mobile" value="<?php echo $Mobile ?>" size="20" maxlength="20" tabindex="4" /> </td>
</tr>
<!--Email Input-->
<tr>
<td><label for="Email">Email</label></td>
<td><input type="text" name="Email" value="<?php echo $Email ?>" size="30" maxlength="50" tabindex="5" /></td>
</tr>
<!--Submit Button-->
<tr>
<td colspan="2" align="center"><input type="submit" name="Submit" value="Submit" tabindex="6"/>
</td>
</tr>
</table>
</form>
<?php
} // end of main else statement
mysql_free_result($rst); //free memory//
?>
</body>
</html>`
最佳答案
$_POST["ContactID"]
返回 null,这就是您收到该错误的原因。
将ContactID发送到服务器:
<input type="hidden" name="ContactID" value="<?php echo $_GET["ContactID"]; ?>" />
您的代码存在七个问题:
mysql_*
功能。它们已经过时了。使用mysqli_*
或PDO
.<b>
和<font>
标签。现在是 2014 年。使用 HTML5 和 CSS3。htmlspecialchars()
,否则用户将能够攻击您的网站 ( XSS )这段代码可以很好地工作:
<?php
try
{
$db = new PDO("mysql:dbname=tafe;host=localhost", "root", "***");
}
catch (PDOException $e)
{
die("Cannot connect to database.");
}
function post($name)
{
return isset($_POST[$name]) ? $_POST[$name] : "";
}
function html($x)
{
return htmlentities($x, ENT_QUOTES, "UTF-8");
}
if (post("id"))
{
$query = $db->prepare("UPDATE contacts SET Name = :name, Address = :address, Phone = :phone, Mobile = :mobile, Email = :email WHERE ContactID = :id");
$query->bindParam(":name", post("name"));
$query->bindParam(":address", post("address"));
$query->bindParam(":phone", post("phone"));
$query->bindParam(":mobile", post("mobile"));
$query->bindParam(":email", post("email"));
$query->bindParam(":id", post("id"));
if ($query->execute())
$message = '<span style="color: green; font-weight: bold;">The contact has been updated.</span><br /><a href="list-contacts.php">Continue</a>';
else
$message = '<span style="color: red; font-weight: bold;">There was an error.</span>';
}
elseif (isset($_GET["ContactID"]))
{
$query = $db->prepare("SELECT Name, Address, Phone, Mobile, Email FROM contacts WHERE ContactID = :id");
$query->bindParam(":id", $_GET["ContactID"]);
if ($query->execute())
{
if (!$query->rowCount())
$message = '<span style="color: red; font-weight: bold;">This contact does not exists.</span>';
else
{
$row = $query->fetch(PDO::FETCH_ASSOC);
foreach ($row as $k => $v)
$_POST[$k] = $v;
}
}
else
$message = '<span style="color: red; font-weight: bold;">There was an error.</span>';
?>
<!DOCTYPE html>
<html>
<head>
<title>Contact</title>
<meta charset="utf-8" />
</head>
<body>
<?php
if (isset($message))
echo "<p>".$message."</p>";
?>
<form action="edit-contact.php" method="post">
<label for="name">Name:</label><br />
<input type="text" name="name" id="name" value="<?php echo html(post("name")) ?>" /><br />
<label for="address">Address:</label><br />
<textarea name="address" id="address"><?php echo html(post("address")) ?></textarea><br />
<label for="phone">Phone:</label><br />
<input type="text" name="phone" id="phone" value="<?php echo html(post("phone")) ?>" /><br />
<label for="mobile">Mobile:</label><br />
<input type="text" name="mobile" id="mobile" value="<?php echo html(post("mobile")) ?>" /><br />
<label for="email">Email:</label><br />
<input type="text" name="email" id="email" value="<?php echo html(post("email")) ?>" /><br />
<input type="submit" name="submit" value="Submit" />
<input type="hidden" name="id" value="<?php echo isset($_GET["ContactId"]) ? intval($_GET["ContactId"]) : "0" ?>" />
</form>
</body>
</html>
关于php - 获取 "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21232199/
git count-objects -v 发出的这个警告是什么意思? Google 只给了我 git 本身的来源。 warning: no corresponding .pack: .git/obje
我要MOVE来自 itab1 的字段至 itab2基于它们的字段名称。我试过以下: CLEAR itab2. MOVE-CORRESPONDING itab1 TO itab2. 这是有效的,但只要
我要MOVE来自 itab1 的字段至 itab2基于它们的字段名称。我试过以下: CLEAR itab2. MOVE-CORRESPONDING itab1 TO itab2. 这是有效的,但只要
假设您将一个项目分解为一个 super 项目和一个子模块。它们都有稳定的(主)分支和开发分支。有没有办法设置您的子模块,以便 super 项目从其 dev 分支跟踪子模块的 dev 分支,并从其稳定分
我有一个列表,其中包含两种不同的商品,product-x 和 product-y。 当用户删除 product-x 时,我需要它的等效 product-y 也删除,反之亦然。 我已经尝试了几种不同的方
下面的示例显示了一个 box div 和一个 :after 内容,它应该是一个单独的 block 。 div.box { background-color: #FAA; width:
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 6 年前。 Improve
我使用的是 ABAP 7.4 SP13。 我想在下面的代码中使用“选项 2”,但是如何进行这种深度映射?这里的目标是有一个干净、简单、易于阅读的映射,避免循环。 TYPES: BEGIN OF
我试图充分了解 docker 的功能,以便对我安全地使用它产生合理的信心。对此的一个建议是始终使用 USER Dockerfile 中的语句。在试图理解这一点的影响时,我遇到了一些麻烦。 具体问题:
我在 Flutter Web 应用程序中使用命名路由进行导航。导航到所需的路由时,URL 会更新,但我无法通过 URL 栏直接导航到该路由。每次我尝试在 URL 中添加路径时,它都会将我带到“.../
我重命名了一个文件夹并更新了命名空间声明,但 ReSharper 6 声称命名空间应该反射(reflect)重命名之前的情况。文件位置数据存储在哪里? 最佳答案 检查并确保您的程序集名称与新的命名空间
我正在 Shopify 上创建带有折扣的草稿订单,其返回错误类似于“必须与根据值计算出的值相对应”。 我计算折扣如下: $amount 是订单总金额 (78.99),$rate (30) 是折扣百分比
我正在使用pyspark.ml.classification中的RandomForestClassifier 我在二进制类数据集上运行模型并显示概率。 我的列概率如下: +-----+--------
我正在尝试绘制以不同角度记录的传感器数据。 import pandas as pd import matplotlib.pyplot as plt #create dataframe, each
我的 ASP.NET 页面没有显示控件。它只是显示母版页错误 The page has one or more controls that do not correspond with 代替 关
问题: 我有一大堆日期,我需要按周排列它们。 问题:如何按日期所在的一年中的第几周对日期进行分组? 示例数据集 Date date = new SimpleDateFormat.parse("04/0
我想获取此命令的选项: my_cmd show --value true -D 为此,我需要通过选项两次(由于架构)。在第二遍期间,无法正确检索与其选项对应的参数。在此示例中,在第二次传递期间检索到的
本地代码 native .c #include #include #include jstring Java_com_lab5_oli_myapplication_MainActivity_he
我正在使用 Firebase Python AdminSDK 生成自定义 token ,Javascript 客户端使用该 token 登录 Firebase。当 JS 客户端尝试使用自定义 toke
在上面的 excel 表中,我需要一个公式来提取存在“1”的特定列名,然后在相应的单元格中输入该名称。示例 - 在上面的图像中,对应于 col“Tags”的每一行都包含相应单元格包含“1”的列名。 最
我是一名优秀的程序员,十分优秀!