Php 如何从HTML表单向MySQL DB插入条目
所以,我有一个表单,页面中有一些字段。例如,auth.php。此表单字段中的数据是通过调用某个php函数接收的,该函数从MySQL数据库中提供此数据。守则:Php 如何从HTML表单向MySQL DB插入条目,php,mysql,Php,Mysql,所以,我有一个表单,页面中有一些字段。例如,auth.php。此表单字段中的数据是通过调用某个php函数接收的,该函数从MySQL数据库中提供此数据。守则: <?php include 'functions.php'; $result=array(); $result = GetEntries(); $json = json_encode($result); ?> 通过此代码在字段中插入的数据: <script type="text/jav
<?php
include 'functions.php';
$result=array();
$result = GetEntries();
$json = json_encode($result);
?>
通过此代码在字段中插入的数据:
<script type="text/javascript">
function nextFunc(){
var name2 = <?php echo $json;?>;
document.getElementById("rname").value = name2[currententry]['Name'];
}
</script>
函数nextFunc(){
var name2=;
document.getElementById(“rname”).value=name2[currententry]['Name'];
}
但是如何实现在MySQL数据库中插入一些条目的机制呢。例如,用户在我的表单上按下ADD按钮,用他自己的数据填充字段“Name”,然后按下SAVE按钮——我想将此用户数据直接保存在我的MySQL数据库中
请帮忙 要实现这一点,您需要遵循以下几个步骤:
- 函数名以小写字母开头
- 类名以大写字母开头
- 不要在只包含php代码的php文件中使用
?>
- 不需要缩进所有代码李>
这是基本用法。一旦理解了该原理,就可以将其扩展到ajax。创建一个ajax函数,将表单数据提交到
submit.php
文件。概括来说,您可以将该数据发布到服务器(可能是标准表单发布,可能是ajax请求等),服务器端代码将读取发布的值并对数据库执行插入操作。有许多教程介绍了这些概念和许多示例。你尝试了什么?你被困在哪里?我发现了这个例子-。是的,它是有效的,但是这个例子使用了POST方法,但是如果我不想使用标准格式,我想使用按钮容量的自己的图像呢。所以-我想知道如何通过使用:construction来实现它“以按钮容量拥有图像”到底是什么意思?任何要插入数据库的数据都需要发布到服务器。通常,您可以选择表单post或AJAX。无论哪种方式,您所要求的内容对于堆栈溢出来说都太宽了。我们可以帮助解决特定的问题,但是网上已经有很多介绍性的教程了。@AntonRomanov不,无论如何都要避免那个教程。它使用过时的mysql\u查询
,不使用准备好的语句来确保数据编码正确。在任何用户提供的数据都指定有?
或:name
指示符的情况下,这些操作非常简单,稍后将根据您使用的是哪个指示符,使用绑定参数或执行
填充指示符。
<form action="submit.php" method="post">
<label>
Name <input type="text" name="name" />
</label>
<input type="submit" value="Save" />
</form>
<?php
$name = strip_tags($_POST['name']);
// connect to database
$con = new mysqli('localhost', 'db_username', 'db_password', 'db_name');
if ($con->connect_errno) {
printf("Failed to connect to mysql: %s", $con->connect_error);
}
// prepare the query
$sql = sprintf("INSERT INTO my_table SET name = '%s'", $name);
// insert into database
$query = $con->query($sql) or die($con->error);
// view ID of last inserted row in the database
print_r('Last inserted ID: '.$con->insert_id);
include 'functions.php';
$result=array(); // this line should not be here
$result = GetEntries(); // is overwritten by this one
$json = json_encode($result);