gpt4 book ai didi

php - 如何将数据传递到数据库?

转载 作者:行者123 更新时间:2023-11-28 23:37:13 25 4
gpt4 key购买 nike

在这种形式中,我试图将用户重定向到给定的 url。在那里我只能使用get方法来传递参数。但是当我使用 get 方法时,我的表单数据并没有保存在数据库中。我想把数据同时存入数据库。

这是我的代码:

<?php

include_once 'CryptoUtils.php';
include_once 'dbconnect.php';

if(isset($_POST['btn-signup']))
{
$mobile = mysql_real_escape_string($_POST['Mnumber']);
$email = mysql_real_escape_string($_POST['email']);
$fname = mysql_real_escape_string($_POST['fname']);
$address = mysql_real_escape_string($_POST['address']);
$sitename = mysql_real_escape_string($_POST['sitename']);


$q = ("INSERT INTO template_users(Mnumber,email,fname,address,sitename) VALUES('$mobile','$email','$fname','$address','$sitename')");
mysql_query ($q) or die("Problem with the query: $q<br>" . mysql_error());
}

?>
<!DOCTYPE>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Ipayy </title>
<link rel="stylesheet" href="style.css" type="text/css"/>

</head>
<body>
<center>
<div id="login-form">
<form action="http://api.ipayy.com/v001/c/oc/dopayment" method="get" >

<table align="left" width="40%" border="0">
<h1 align="left">Create Your Web Site</h1></br></br>




<input type="hidden" name="gh" value="<?php echo $encrypted_string; ?>" />

<tr>
<td><input type="text" name="Mnumber" value="" placeholder="Your Mobile Number" required /></td>
</tr>

<tr>
<td><input type="email" name="email" value="" placeholder="Your Email" required /></td>
</tr>

<tr>
<td><input type="text" name="fname" value="" placeholder="Your First Name" required /></td>
</tr>

<tr>
<td><input type="text" name="address" value="" placeholder="Your Address" required /></td>
</tr>

<tr>
<td><input type="text" name="sitename" value="" placeholder="Your Site name" required /></td>
</tr>

<tr>
<td><button type="submit" name="btn-signup" onclick="myFunction()">Create My gomobi website</button></td>
</tr>

</table>
</form>

</div>
</center>
</body>
</html>

这是我的数据库连接:

<?php
if(!mysql_connect("localhost","root",""))
{
die('oops connection problem ! --> '.mysql_error());
}
if(!mysql_select_db("ipay"))
{
die('oops database selection problem ! --> '.mysql_error());
}
?>

最佳答案

当您向外部链接发送请求并且还想在您自己的服务器中处理相同的请求时,您要么必须使用 AJAX 发送两个请求,要么必须使用 curl 向外部链接发送一个请求。希望以下内容对您有所帮助。

<button type="button" name="btn-signup" onclick="myFunction()">Create My gomobi website</button>

检查上面的按钮代码,我将其更改为类型按钮,因为我希望您使用 myFunction() 和 AJAX 发布表单数据。下面是 myFunction() 的可能代码。 [注意:您必须在 myFunction() 中已经有一些代码,我不知道,您只需在需要时保留它们并向其中添加以下代码] 如果使用 ajax 表单发布,您不需要在表单中使用方法或操作标签。

function myFunction()
{
var gh = $('input[name=gh]').val();
var Mnumber = $('input[name=Mnumber]').val();
var email = $('input[name=email]').val();
var fname= $('input[name=fname]').val();
var address= $('input[name=address]').val();
var sitename= $('input[name=sitename]').val();
$.get("http://api.ipayy.com/v001/c/oc/dopayment",
{

gh : gh,
Mnumber : Mnumber,
email : email,
fname : fname,
address : address,
sitename : sitename
},
function(data,status){
//console.log(data);
if(status == 'success')
{
//another POST or GET ajax method can be initiated with same data
//but differant url (the url of your php code which is inserting data to database)Here is an example below
$.post("storedatatodb.php",
{
gh : gh,
Mnumber : Mnumber,
email : email,
fname : fname,
address : address,
sitename : sitename

},
function(data,status)
{
if(status == 'success')
{
// do something - redirect to some page
}
});
}
else
{
//show error message
// or do nothing
}
});
}

在上述情况下,storedatatodb.php 将如下所示

<?php
include_once 'dbconnect.php';
$mobile = mysql_real_escape_string($_POST['Mnumber']);
$email = mysql_real_escape_string($_POST['email']);
$fname = mysql_real_escape_string($_POST['fname']);
$address = mysql_real_escape_string($_POST['address']);
$sitename = mysql_real_escape_string($_POST['sitename']);


$q = ("INSERT INTO template_users(Mnumber,email,fname,address,sitename) VALUES('$mobile','$email','$fname','$address','$sitename')");
mysql_query ($q) or die("Problem with the query: $q<br>" . mysql_error());
?>

另一种方法是使用 PHP curl 。

在这种情况下,您可以在表单标记中使用 GET 或 POST 的任何方法,并在操作中使用处理数据的 php 文件的 url 插入到数据库中。假设文件是​​ storedatatodb.php 。代码如下

<?php
include_once 'dbconnect.php';
$gh = $_POST['gh'];
$mobile = mysql_real_escape_string($_POST['Mnumber']);
$email = mysql_real_escape_string($_POST['email']);
$fname = mysql_real_escape_string($_POST['fname']);
$address = mysql_real_escape_string($_POST['address']);
$sitename = mysql_real_escape_string($_POST['sitename']);

$q = ("INSERT INTO template_users(Mnumber,email,fname,address,sitename) VALUES('$mobile','$email','$fname','$address','$sitename')");
mysql_query ($q) or die("Problem with the query: $q<br>" . mysql_error());

//curl to send data to external URL
// In case of GET request
$geturl = "http://api.ipayy.com/v001/c/oc/dopayment?gh=".$gh."&mobile=".$mobile."&email=".$email."&fname=".$fname."&address=".$address."&sitename=".$sitename;
// Get cURL resource
$curl = curl_init();
// Set some options
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $geturl
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
//Print error if any
if(curl_errno($curl))
{
echo 'error:' . curl_error($curl);
}
// Close request to clear up some resources
curl_close($curl);
?>

关于php - 如何将数据传递到数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35454701/

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