如何从链接获取变量“message”并使其在数据库中更新?

时间:2017-06-11 06:32:04

标签: php postback

嘿所以我正在创建一个人们可以发送消息的网站,我想把它放在我的数据库中。但他们可以更新消息。这是我使用http://example.com/file.php?message=hi&referral=1的链接,但在访问时我收到此错误

  

0行更新,将其添加到储物柜。

,这是一个错误,因为没有更新行

<?php

define("MYSQL_HOST", "localhost");
define("MYSQL_PORT", "3306");
define("MYSQL_DB", "db");
define("MYSQL_TABLE", "table");
define("MYSQL_USER", "user");
define("MYSQL_PASS", "pass");
$mysqli = new mysqli(MYSQL_HOST, MYSQL_USER, MYSQL_PASS, MYSQL_DB);
if ($mysqli->connect_errno) 
{
  echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
$referral =            $_GET['refferal'];
$message =           $_GET['message'];

if (!($stmt = $mysqli->prepare("UPDATE ".MYSQL_DB.".".MYSQL_TABLE." SET message=(?) WHERE referral=(?) "))) 
{

  echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
$stmt->bind_param('ds', $message, $referral);
if (!$stmt->execute()) 
{
  echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}
else
{
  printf("%d Row updated, added ".$message." to locker ".$referral." .\n", mysqli_stmt_affected_rows($stmt));
}
?>

1 个答案:

答案 0 :(得分:0)

假设您有一个网址为www.google.com,并且您想要从url参数传递某些数据属性。对于此示例,链接类似于以下内容。

www.google.com?color=blue

您可以执行以下操作从网址中检索值。

// First lets define this variable so we do not get any undefined variable warnings in the error log.
$color = NULL; 

// second would be lets validate that this get parameter is set and not empty.
if ( !empty($_GET['color']) ) { 


 // We will then define the REQUEST to a variable for later calling.
    $color = $_REQUEST['color']; 
}

然后您可以简单地echo $color,它的值为blue

然后,您只需将$ color变量应用于数据库语句。

最后,您在refferal的请求中有一个拼写错误,其中应该是referral