简体   繁体   English

PHP Mysql通过函数将数据插入数据库

[英]PHP Mysql insert data into a database froma function

I would want to capture $status and insert it in a database 'smsdb'. 我想捕获$ status并将其插入数据库'smsdb'中。 However, I'm unable to do so and wish someone would guide me. 但是,我无法这样做,希望有人会引导我。 This starting code is part of wall function and thats where the status is got from after calling the function. 起始代码是wall函数的一部分,多数民众赞成在调用函数后从中获取状态。 In the grabdetails function where I get the other details into the db, $status is inaccessible. 在我将其他详细信息输入数据库的grabdetails函数中,$ status无法访问。 Would someone lead me please... // code 有人可以带领我吗... //代码

 $name = $resultarr['name'];
 $amount = $resultarr['amount'];
 $transaction_id = $resultarr['trans_id'];
 $date = $resultarr['time_paid'];

//message template
$message = "Dear $name we have received $amount from you. MPESA transaction Id $transaction_id on $date.";

$mobilenumber = $resultarr['msisdn']; // get mobile number from array
$message_sent = $message;

$serviceArguments = array(
        "mobilenumber" => $mobilenumber,
        "message" => $message_sent
);

$client = new SoapClient("http://59.38.606.10:8080/smsengine/smsws?WSDL");

$result = $client->process($serviceArguments);

grabdetails($message_sent, $mobilenumber);

return $result;

} 
 //I call the function wall() to send sms         

 wall();
$perm = wall();
$status = $perm->return; //outputing the status
  // Here I want to capture the $status variable and put it in a db below
echo "$status";


function grabdetails($messagee, $mobno)
{

$message_sent = $messagee;
$mobilenumber = $mobno;


$servername = "localhost";
$username = "root";
$password = "";
$dbname = "smsdb";

// Create connection


// Check connection

 $sql = "INSERT INTO smsdb (sms_text, receiver_number, time_sent, status)
   VALUES
     ('$message_sent', '$mobilenumber', NOW(), '$status' )";
 $conn->query($sql);

Any one? 任何人?

The $status variable is out of scope . $status变量out of scope

Add the line: 添加行:

global $status;

To your function. 为了您的功能。

http://php.net/manual/en/language.variables.scope.php http://php.net/manual/zh/language.variables.scope.php

EDIT: 编辑:

When you call grabdetails within the wall() function, the $status variable is not yet set. wall()函数中调用grabdetails ,尚未设置$status变量。 Maybe pass the status as a parameter in the grabdetails function from within the wall() function. 也许可以从wall()函数内部将状态作为参数传递给grabdetails函数。

eg grabdetails($message_sent, $mobilenumber, $result); 例如grabdetails($message_sent, $mobilenumber, $result);

Also change the function declaration to grabdetails($messagee, $mobno, $status); 还要将函数声明更改为grabdetails($messagee, $mobno, $status);

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

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