我正在尝试通过 php 将 html 表单连接到 mysql 数据库。这是我的 php 脚本代码:
include_once('db_connect.php');
if(isset($_REQUEST['submit'])) {
$lokotitle=$_POST['lokotitle'];
$description=$_POST['description'];
$category=$_POST['category'];
$showyourname=$_POST['showyourname'];
$yourname=$_POST['yourname'];
$lat=$_POST['lat'];
$lng=$_POST['lng'];
// Will add form validation here
if ($errorMessage != "" ) {
echo "<p class='message'>" .$errorMessage. "</p>" ;
}
else{
//Inserting record in table using INSERT query
$insqDbtb="INSERT INTO `new_loko`.`web_form`
(`lokotitle`, `description`, `category`, `showyourname`, `yourname`, `lat`, `lng`) VALUES ('$lokotitle', '$description', '$category', '$showyourname', '$yourname', '$lat', '$lng')";
mysqli_query($link,$insqDbtb) or die(mysqli_error($link));
?>
<script type="text/javascript">window.location = "submit_success.php";</script>
<?php
}
}
我的 include_once('db_connect.php') 工作正常,但 php 只是运行这部分,检查连接是否正常工作,然后停止,而不实际将数据上传到数据库。我无法弄清楚代码有什么问题。另外,我之前就可以使用此功能,但不知道为什么现在不再使用了。
预先感谢您提供急需的帮助。
最佳答案
将您的 $errorMessage
声明为空,然后根据验证更改值。
<?php
include_once('db_connect.php');
if(isset($_REQUEST['submit'])) {
$lokotitle=$_POST['lokotitle'];
$description=$_POST['description'];
$category=$_POST['category'];
$showyourname=$_POST['showyourname'];
$yourname=$_POST['yourname'];
$lat=$_POST['lat'];
$lng=$_POST['lng'];
$errorMessage="";
/* some validation
if(validation)
$errorMessage="some message";
-------------------
-------------------
-------------------
// Will add form validation here
*/
if ($errorMessage != "" ) {
echo "<p class='message'>" .$errorMessage. "</p>" ;
}
else{
//Inserting record in table using INSERT query
$insqDbtb="INSERT INTO `new_loko`.`web_form`
(`lokotitle`, `description`, `category`, `showyourname`, `yourname`, `lat`, `lng`) VALUES ('$lokotitle', '$description', '$category', '$showyourname', '$yourname', '$lat', '$lng')";
mysqli_query($link,$insqDbtb) or die(mysqli_error($link));
?>
<script type="text/javascript">window.location = "submit_success.php";</script>
<?php
}
}
?>
关于php - 连接php到mysql数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38582865/