我似乎在这里遗漏了一些东西。根据我所看到的情况,我在Google或StackOverflow上找到的每一篇帖子都显示我的代码是正确的。但是,每次我点击提交它只是清除页面并让我回到顶部。我已经尝试将它归结为基本字段(没有函数),但这并没有改变任何东西。
这是我的表格:
<form acction="/php/newcarcode.php" method="post">
<table style="width:575px">
<tr>
<th>Currently Own</th>
<td><input type="checkbox" name="own"></td>
<td></td>
</tr>
<tr>
<th style="width:175px">Year</th>
<td style="width:375px"><input type="text" name="year" style="width:340px"></td>
<td style="width:25px"></td>
</tr>
<tr>
<th>Make</th>
<td><select name="dropdown" style="width:344px"><?php make() ?></select></td>
<td></td>
</tr>
<tr>
<th>Model</th>
<td><select name="dropdown" style="width:344px"><?php model() ?></select></td>
<td></td>
</tr>
<tr>
<th>Trim</th>
<td><input type="text" name="trim" style="width:340px"></td>
<td></td>
</tr>
<tr>
<th>Purchased</th>
<td><input type="date" name="purchased" style="width:340px"></td>
<td></td>
</tr>
<tr>
<th>Engine</th>
<td><select name="engine" style="width:344px"><?php engine() ?></select></td>
<td><a href="newengine.php"><img src="/images/addnew.png" width="33px" height="25px"></a></td>
</tr>
<tr>
<th>Drivetrain</th>
<td><select name="drivetrain" style="width:344px"><?php drivetrain() ?></select></td>
<td></td>
</tr>
<tr>
<th>Transmission</th>
<td><select name="trans" style="width:344px"><?php trans() ?></select></td>
<td><a href="newengine.php"><img src="/images/addnew.png" width="33px" height="25px"></a></td>
</tr>
<tr>
<th>Driver</th>
<td><select name="driver" style="width:344px"><?php driver() ?></select></td>
<td><a href="newengine.php"><img src="/images/addnew.png" width="33px" height="25px"></a></td>
</tr>
<tr>
<th>Type</th>
<td><select name="dropdown" style="width:344px"><?php type() ?></select></td>
<td></td>
</tr>
<tr>
<th>Doors</th>
<td><input type="number" name="doors" style="width:340px"></td>
<td></td>
</tr>
<tr>
<th>Color</th>
<td><input type="text" name="color" style="width:340px"></td>
<td></td>
</tr>
<tr>
<th>Cost</th>
<td><input type="number" name="cost" style="width:340px"></td>
<td></td>
</tr>
<tr>
<th>Sale Price</th>
<td><input type="number" name="sale" style="width:340px"></td>
<td></td>
</tr>
<tr>
<th>Profit</th>
<td><input type="number" name="profit" style="width:340px"></td>
<td></td>
</tr>
<tr>
<th>Profile Pic</th>
<td><input type="text" name="profile" style="width:340px"></td>
<td></td>
</tr>
<tr>
<th>Photo Album</th>
<td><input type="text" name="album" style="width:340px"></td>
<td></td>
</tr>
<tr>
<th>Thumbnail Pic</th>
<td><input type="text" name="thumbnail" style="width:340px"></td>
<td></td>
</tr>
<tr>
<th style="height: 75px">Notes</th>
<td><textarea type="text" name="notes" style="width:340px" rows="4"></textarea></td>
<td></td>
</tr>
<tr>
<th style="height: 75px">Mods</th>
<td><textarea type="message" name="mods" style="width:340px" rows="4"></textarea></td>
<td></td>
</tr>
</table>
<br>
<input type="submit" value="Submit">
</form>
然后这是它正在调用的php页面:
<?php
$con = mysql_connect("server", "mycaradmin", "SuperSecretPassword") or die(mysql_error());
mysql_select_db("mycars") or die(mysql_error());
$year = $_POST[year];
$make = $_POST[make];
$model = $_POST[model];
$trim = $_POST[trim];
$engine = $_POST[engine];
$trans = $_POST[trans];
$doors = $_POST[doors];
$type = $_POST[type];
$color = $_POST[color];
$drivetrain = $_POST[drivetrain];
$driver = $_POST[driver];
$own = $_POST[own];
$purchase = $_POST[purchase];
$sale = $_POST[sale];
$profit = $_POST[profit];
$profile = $_POST[profile];
$notes = $_POST[notes];
$mods = $_POST[mods];
$album = $_POST[album];
$sql = " INSERT INTO mycars.vehicles (
VYear,
VMakeID,
VModelID,
VTrim,
VEngineID,
VTransID,
VNumDoors,
VTypeID,
VColor,
VDrivetrainID,
PeopleID,
VCurrentlyOwn,
VPurchasePrice,
VSalePrice,
VProfit,
VAttachments,
VNotes,
VModifications,
VAlbum,
VDateOfPurchase
)
VALUES (
'$year',
'$make',
'$model;',
'$trim',
'$engine',
'$trans',
'$doors',
'$type',
'$color',
'$drivetrain',
'$driver',
'$own',
'$purchase',
'$sale',
'$profit',
'$profile',
'$notes',
'$mods',
'$album',
)"
;
if (!mysql_query($con,$sql))
{
die('Error: ' . mysql_error($con));
}
echo "1 record added";
mysql_close($con);
&GT;
似乎不管我改变什么,我得到相同的结果。我很困惑。
答案 0 :(得分:5)
错字 -
<form acction="/php
^
答案 1 :(得分:2)
您的$_POST
变量没有引号:
$_POST[year];
应为$_POST['year'];
答案 2 :(得分:1)
你的SQL语句中的最后一个'$ album'后面还有一个额外的逗号
答案 3 :(得分:1)
此代码中存在一些问题:
你需要在php中的关联数组中用单引号或双引号括起键,如下所示:
$ year = $ _POST [“year”];
答案 4 :(得分:0)
我知道这是额外的话题,可能会得分......永远不要使用mysql库,而是使用mysqli或PDO。您也没有对SQL,JavaScript或HTML注入的输入进行验证。
您的插入语句中还有一个额外的分号和逗号。
考虑更改看起来像这样的行:
$year = $_POST[year];
对于看起来像这样的行:
$year = filter_var($_POST['year'], FILTER_SANITIZE_INT);
这将确保年份是整数。你可以/也应该检查它是发布到今天的第一辆车范围内的有效年份。 filter_var还可以使用正则表达式进行过滤,或者使用除FILTER_SANITIZE_INT之外的其他过滤器对已知格式的其他类型数据进行过滤。
您的数据库内容:
$db = new mysqli(server", "mycaradmin", "SuperSecretPassword") or die();
$stmnt = $db->prepare('INSERT INTO mycars.vehicles (
VYear,
VMakeID,
VModelID,
VTrim,
VEngineID,
VTransID,
VNumDoors,
VTypeID,
VColor,
VDrivetrainID,
PeopleID,
VCurrentlyOwn,
VPurchasePrice,
VSalePrice,
VProfit,
VAttachments,
VNotes,
VModifications,
VAlbum,
VDateOfPurchase
)
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)');
$stmnt->bindparam('ssssssssssssssssssss', '$year',
$make,
$model,
$trim,
$engine,
$trans,
$doors,
$type,
$color,
$drivetrain,
$driver,
$own,
$purchase,
$sale,
$profit,
$profile,
$notes,
$mods,
$album) or die($stmnt->error);
$stmnt->execute()or die($stmnt->error);
$stmnt->close();
$db->close();
这将通过在使用之前准备和编译语句来保护您的数据库免受SQL注入。您应该将来自用户的任何和所有输入视为潜在有害和危险,因此如果它来自用户,请在使用之前对其进行清理,并且不要将其未处理地传递到数据库查询中。
答案 5 :(得分:0)
参数化您的查询。这将阻止SQL注入。