gpt4 book ai didi

javascript - 使用 PHP、JQUERY 和 JAVASCRIPT 将数据插入 mysql 数据

转载 作者:行者123 更新时间:2023-11-29 04:18:40 27 4
gpt4 key购买 nike

我正在尝试使用使用 PHP、JQUERY 和 JAVASCRIPT 的表单将数据插入 MYSQL 数据库。目前我无法获取用户可以输入到“添加业务名称”字段中的数据,以插入到“justrated”的 MYSQL 数据库中。此外,屏幕上没有出现任何错误,所以我不知道如何修复它。

代码:

<!DOCTYPE html>
<html>
<head>
<title>test</title>
<meta charset="utf-8">
<title>jQuery UI Tabs - Default functionality</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>SB Admin 2 - Bootstrap Admin Theme</title>
<!-- Bootstrap Core CSS -->
<link href="bower_components/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
<!-- MetisMenu CSS -->
<link href="bower_components/metisMenu/dist/metisMenu.min.css" rel="stylesheet">
<!-- Custom CSS -->
<link href="dist/css/sb-admin-2.css" rel="stylesheet">
<!-- Custom Fonts -->
<link href="bower_components/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
</head>
<body>
<div class="navbar-default sidebar" role="navigation">
<!-- /.sidebar-collapse -->
</div>
<!-- /.navbar-static-side -->
</nav>
</div>
<!-- /.row -->
<div class="row">
<div class="col-lg-12">
<div class="panel panel-default">
<div class="panel-heading">
</div>
<div class="panel-body">
<div class="row">
<div id="page-wrapper">
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">Add Business</h1>
</div>
<!-- /.col-lg-12 -->
</div>
<!-- /.row -->
<div class="row">
<div class="col-lg-12">
<div class="panel panel-default">
<div class="panel-heading">
Please enter your business details
</div>
<div class="panel-body">
<div class="row">
<div class="col-lg-6">
<form role="form">
<!--<div class="form-group">
<label>Text Input</label>
<input class="form-control">
<p class="help-block">Example block-level help text here.</p>
</div>-->
<div class="form-group" name ="BusinessName">
<label>Business Name</label>
<input class="form-control" placeholder="Enter Business Name">
</div>
<button type="submit" name="submit" class="btn btn-default" style ="background-color:#238DE8;color:#FFFFFF";>Submit</button>
<button type="reset" name="reset" class="btn btn-default" style="background-color:#FF2720;color:#FFFFFF";>Reset</button>
</form>
</div>
<!-- /.col-lg-6 (nested) -->
<!-- /.col-lg-6 (nested) -->
</div>
<!-- /.row (nested) -->
</div>
<!-- /.panel-body -->
</div>
<!-- /.panel -->
</div>
<!-- /.col-lg-12 -->
</div>
<!-- /.row -->

</div>
<button type="submit" name="submit" class="btn btn-default" style ="background-color:#238DE8;color:#FFFFFF";>Submit</button>
<button type="reset" name="reset" class="btn btn-default" style="background-color:#FF2720;color:#FFFFFF";>Reset</button>
</form>
</div>

<?php
if (isset($_POST["submit"]))
{
//create connection
$conn = new mysqli("localhost", "root", "", "justrated");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO businesses (`BusinessName`)
VALUES ('".$_POST['BusinessName']."')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
?>
<!-- /#wrapper -->
<!-- jQuery -->
<script src="bower_components/jquery/dist/jquery.min.js"></script>
<!-- Bootstrap Core JavaScript -->
<script src="bower_components/bootstrap/dist/js/bootstrap.min.js"> </script>
<!-- Metis Menu Plugin JavaScript -->
<script src="bower_components/metisMenu/dist/metisMenu.min.js"></script>
<!-- Custom Theme JavaScript -->
<script src="dist/js/sb-admin-2.js"></script>
</body>
</html>

最佳答案

首先,您应该将表单设置为通过 POST 发送,因为它默认为 GET。

<form role="form" action="" method="post">

其次,给你的输入一个名字

<input class="form-control" placeholder="Enter Business Name" name="BusinessName">

第三,你应该escape your data在插入之前,或使用 prepared statements .你正在做的事情很危险。

最后,我不明白你在用 jQuery 和 JavaScript 做什么(jQuery 是一个 JavaScript 库,你知道......不是两个不同的东西)??

关于javascript - 使用 PHP、JQUERY 和 JAVASCRIPT 将数据插入 mysql 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32400906/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com