【发布时间】:2018-02-15 17:04:07
【问题描述】:
我正在尝试创建一个简单的表单,它将我的 HTML 表单接收到的给定数据插入到名为“供应商”的 SQL 表中,但是我正在努力使用它的功能。
我想将 7 个文本字段添加到我的供应商表中,它们的名称如下:
- 供应商名称
- addressL1(第 1 行)
- 地址L2
- 邮政编码
- 电子邮件
- 电话
- 说明
可以在下面找到此表单的 HTML:
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<form action="" method="post">
<ul class="form-style-1">
<li>
<label style="color:#4D4D4D;" >Vendor Name <span class="required">*
</span></label>
<center> <input type="text" name="vendorName" class="field-long"
required="required" placeholder="Vendor Name" /> </center>
</li>
<li>
<label style="color:#4D4D4D;">Vendor Address <span class="required">*
</span></label>
<center> <input type="text" name="addressL1" required="required"
class="field-long" placeholder="Address Line 1" /> </center>
</br>
<center> <input type="text" name="addressL2" required="required"
class="field-long" placeholder="Address Line 2" /> </center>
</br>
<center> <input type="text" name="postcode" required="required"
class="field-short" placeholder="Postcode" /> </center>
</li>
<li>
<label style="color:#4D4D4D;">Vendor Contact Details <span
class="required">*</span></label>
<center> <input type="text" name="email" required="required"
class="field-long" placeholder="Email Address" /> </center>
</br>
<center> <input type="text" name="telephone" required="required"
class="field-long" placeholder="Phone Number" /> </center>
</select>
</li>
<li>
<label style="color:#4D4D4D;">Vendor Description </label>
<center> <textarea name="description" id="field5" class="field-long
field-textarea" placeholder="Description"></textarea> </center>
</li>
<li>
<center> <input type="submit" class="AddButton" value="POST"></input>
</center>
</li>
</ul>
</form>
</body>
</html>
而我使用的PHP是:
<?php
date_default_timezone_set('Europe/London');
$server = "";
$connectionInfo = array( "Database"=>"");
$conn = sqlsrv_connect($server,$connectionInfo);
if (!$conn)
{
die("Connection failed");
}
$_SERVER['REQUEST_METHOD'];
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$VendorName = $_POST['vendorName'];
$AddressLine1 = $_POST['addressL1'];
$AddressLine2 = $_POST['addressL2'];
$Postcode = $_POST['postcode'];
$VendorEmail = $_POST['email'];
$VendorNumber = $_POST['telephone'];
$VendorDes = $_POST['description'];
$time = time();
$timestamp = date("Y-m-d H:i:s", $time);
$describeQuery = ("INSERT INTO Vendors (VendorName, VendorAL1,
VendorAL2, VendorPost, VendorEmail, VendorNumber, VendorDes,
Added)
VALUES ('".$VendorName."', '".$AddressLine1."',
'".$AddressLine2."', '".$Postcode."',
'".$VendorEmail."', '".$VendorNumber."',
'".$VendorDes."', '".$timestamp."')");
$results = sqlsrv_query($conn, $describeQuery);
if(sqlsrv_query($conn, $describeQuery))
{
$alert = "Vendor Successfully Added";
echo "<script type='text/javascript'>alert('$alert');
</script>";
}
else
{
echo 'Information not inserted';
}
}
sqlsrv_close($conn);
?>
每次我提交表单时,它都会直接进入“未插入信息”ELSE 语句,并且不会将数据导入我的数据库。
出于预防原因,我删除了我的服务器名称和数据库名称,但是我可以向您保证它们是正确的,因为我曾参与过以前的项目并使用了相同的连接方法。
对此的任何帮助将不胜感激,如果有任何格式错误,请提前道歉,我不是堆栈溢出的狂热用户。
【问题讨论】:
-
警告:这有一些严重的SQL injection bugs,因为在查询中使用了用户数据。尽可能使用准备好的语句。当使用low-level SQL server driver 时,这些操作非常简单,其中任何用户提供的数据都使用
?或:name指示符指定,稍后使用bind_param或execute填充,具体取决于您使用的是哪个。 切勿将$_POST、$_GET或任何用户数据直接放在您的查询中。 -
考虑使用development framework 来解决此类问题。有了这些你可以遵循的模式,将你的代码组织成适当的模型、视图和控制器上下文。您在这里看到的是一堆混乱的问题,HTML、PHP 和 SQL 都混杂在一起。框架有多种形式,从像 Fat-Free Framework 这样的非常精简到像 Laravel 这样的功能非常齐全,以及介于两者之间的许多地方。
-
这不是我打算发布或准备在现实世界中使用的项目。在较低级别的 PHP 教程网站上有许多类似于我的在线示例,这就是为什么我来这里是为了指导我哪里出错了,而不是如何改进我的编程风格......
-
我想告诉你的是停止使用“低级 PHP 教程网站”,因为它们中的大多数在促进不良习惯方面都是纯粹的毒药,而且通常只充斥着过时的东西垃圾。一个好的框架会不断发展和改进。 YouTube 教程(如果有的话)在过去十年中已经大幅下降。如果你想真正学习 PHP 而不仅仅是乱七八糟的代码,请使用框架。