简体   繁体   English

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

[英]How can I pass the data to database?

In this form I am trying to redirect the user to the given url. 我正在尝试以这种形式将用户重定向到给定的URL。 There I can only use get method to pass parameters. 在那里,我只能使用get方法来传递参数。 But my form data are not save in the database when I use the get method. 但是当我使用get方法时,表单数据没有保存在数据库中。 I want to save the data at the same time in the database. 我想同时将数据保存在数据库中。

Here is my code: 这是我的代码:

<?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>

Here is my db connection: 这是我的数据库连接:

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

As you are sending requests to an external link and also want to process the same request in your own server, then either you have to use AJAX to send two requests or you have to use curl to send one request to external link. 当您将请求发送到外部链接并且还想在自己的服务器中处理相同的请求时,则要么必须使用AJAX发送两个请求,要么必须使用curl将一个请求发送到外部链接。 Hope the following may help. 希望以下内容能有所帮助。

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

Check above button code, I changed it to type button as I prefer you to post the form data using your myFunction() and by AJAX. 检查上面的按钮代码,我将其更改为type button,因为我希望您使用myFunction()和AJAX发布表单数据。 Now below is the possible code for myFunction() . 现在下面是myFunction()的可能代码。 [Note: You must have some code already in myFunction(), which I dont know, you just keep them if needed and also add the following code to it] In case ajax form posting you need not to use method or action in your form tag. [注意:您必须在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
    }
    });
}

In the above case the storedatatodb.php will be like follows 在上述情况下,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());
?>

ANOTHER method is to use PHP curl . 另一种方法是使用PHP curl。

In this case you may use any method of GET or POST in form tag and in action , use the url of the php file processing your data to insert into db. 在这种情况下,您可以在表单标签中使用任何GET或POST方法,并且在操作中,使用php文件的url处理您的数据以插入db。 Assume the file is storedatatodb.php . 假设文件为storedatatodb.php。 The code will be like follows 代码如下

<?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);
?>

If you are submitting data to same page then change from 如果您要向同一页面提交数据,则从

<form action="http://api.ipayy.com/v001/c/oc/dopayment" method="get" >

to

<form action="">

and also change from 并从

<?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());
}

?>

to

<?php

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

if(isset($_POST['btn-signup']))
{
    $mobile = mysql_real_escape_string($_GET['Mnumber']);
    $email = mysql_real_escape_string($_GET['email']);
    $fname = mysql_real_escape_string($_GET['fname']);
    $address = mysql_real_escape_string($_GET['address']);
    $sitename = mysql_real_escape_string($_GET['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());
}

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM