如何在单击按钮时将数据插入数据库?
Posted
技术标签:
【中文标题】如何在单击按钮时将数据插入数据库?【英文标题】:How to insert data in to database on button click? 【发布时间】:2015-03-30 11:28:53 【问题描述】:我有一个模式,它读取如下代码之类的值。当我单击模式中的提交按钮时,是否可以将 $row3['first_name'] 等值插入数据库?我对此进行了研究,但我没有看到太多关于它的信息,因为它们大多使用文本字段。
<a href="#" class="btn btn-success hidden" id="request" data-toggle="modal" name="req" data-target="#basicModal">Request from Lender</a>
</br></br>
<form action="" method="post">
<div class="modal fade" id="basicModal" tabindex="-1" role="dialog" aria-labelledby="basicModal" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">x</button>
<h4 class="modal-title" id="myModalLabel"><?php
print $row3['title'] . ' from ' . $row3['first_name'] . ' ' . $row3['last_name'];
?></h4>
</div>
<div class="modal-body">
<p>Are you sure you want to borrow <b> 1 x <?php
print $row3['title'];
?></b> from <b> <?php
print $row3['first_name'];
?> <?php
print $row3['last_name'];
?></b> for <b id="total"></b> cogs?
</br></br> You will be required to collect and return this item to <?php
print $row3['houseno'];
?> <?php
print $row3['address1'];
?> <?php
print $row3['postcode'];
?>.
</br></br> <?php
print $_GET[$from];
?> Your cogs will not be transferred to <?php
print $row3['first_name'];
?> until you have confirmed you wish to proceed following the lenders decision!</p>
</div>
<div class="modal-footer">
<button type="submit" value="submit" class="btn btn-success">Register</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</form>
【问题讨论】:
【参考方案1】:您需要调用一个方法,将要插入到数据库中的值传递给。
例子:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error)
die("Connection failed: " . $conn->connect_error);
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')";
if ($conn->query($sql) === TRUE)
echo "New record created successfully";
else
echo "Error: " . $sql . "<br>" . $conn->error;
$conn->close();
?>
【讨论】:
我该怎么做呢? :S 谢谢 - 我最终还是这样做了... if(isset($_POST['button1'])) $q = mysql_query("INSERT INTO table (keys) VALUES (' $values')") 或死(mysql_error()); if($q) echo "成功"; else 回声“错误”;以上是关于如何在单击按钮时将数据插入数据库?的主要内容,如果未能解决你的问题,请参考以下文章