不能作为未插入数据库的数据回显到网站

时间:2016-03-15 03:16:49

标签: php mysql curl sdn

我需要解决朋友完成的工作。似乎没有错误,因为我可以毫无问题地启动网站。从下拉列表中选择一个选项并单击“添加”时,它不会向SQL插入数据,也不会回显到网站。我想解决的问题只是将数据插入数据库并在网站上显示。数据库和表没有错误,因为我尝试多次重做它而没有运气。

我不确定代码有什么问题。

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form") && ($_POST['InputSwitch'] != "") && ($_POST['InputFromPort'] != "") && ($_POST['InputToPort'] != "")) {

//Start of Add Flow
$flow = '{"flow":{"cookie":"0x2032195","priority": 30000, "idle_timeout": 0, "hard_timeout": 0,"match":[{"in_port":'.$_POST["InputFromPort"].'}],"actions":[{"output":'.$_POST["InputToPort"].'}]}}';

$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POSTFIELDS, $flow);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_URL,"https://127.0.0.1:8443/sdn/v2.0/of/datapaths/".$_POST['InputSwitch']."/flows");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

$result3 = curl_exec ($curl);
curl_close ($curl);
//End of Add Flow

//Start of Add Flow
$flow2 = '{"flow":{"cookie":"0x2032195","priority": 30000, "idle_timeout": 0, "hard_timeout": 0,"match":[{"in_port":'.$_POST["InputToPort"].'}],"actions":[{"output":'.$_POST["InputFromPort"].'}]}}';

$curl2 = curl_init();
curl_setopt($curl2, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl2, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl2, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl2, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl2, CURLOPT_POSTFIELDS, $flow2);
curl_setopt($curl2, CURLOPT_HEADER, true);
curl_setopt($curl2, CURLOPT_URL,"https://127.0.0.1:8443/sdn/v2.0/of/datapaths/".$_POST['InputSwitch']."/flows");
curl_setopt($curl2, CURLOPT_RETURNTRANSFER, 1);

$result4 = curl_exec ($curl2);
curl_close ($curl2);
//End of Add Flow

$query = "SELECT * FROM port_switching";
$result = mysql_query($query);
$i=0;
while($row = mysql_fetch_array($result))
{
    $table[$i]=$row['switching_id'];
    $table[$i+1]=$row['switch'];
    $table[$i+2]=$row['port_in'];
    $table[$i+3]=$row['port_out'];
    $i+=4;
}
if((array_search($_POST['InputFromPort'], $table)) != false)
{
    $insertSQL = sprintf("UPDATE port_switching SET port_in=%s WHERE port_in=%s",
        GetSQLValueString($_POST['InputFromPort'], "text"),
        GetSQLValueString($_POST['InputFromPort'], "text"));
      mysql_select_db($database_localhost, $localhost);
    $Result1 = mysql_query($insertSQL, $localhost) or die(mysql_error());
}


$insertSQL = sprintf("INSERT INTO port_switching (switch, port_in, port_out) VALUES (%s, %s, %s)",
                   GetSQLValueString($_POST['InputSwitch'], "text"),
                   GetSQLValueString($_POST['InputFromPort'], "text"),
                   GetSQLValueString($_POST['InputToPort'], "text"));

  $insertSQL2 = sprintf("INSERT INTO port_switching (switch, port_in, port_out) VALUES (%s, %s, %s)",
                   GetSQLValueString($_POST['InputSwitch'], "text"),
                   GetSQLValueString($_POST['InputToPort'], "text"),
                   GetSQLValueString($_POST['InputFromPort'], "text"));

  mysql_select_db($database_localhost, $localhost);
  $Result1 = mysql_query($insertSQL, $localhost) or die(mysql_error());
 $Result2 = mysql_query($insertSQL2, $localhost) or die(mysql_error());

尝试在$ Result2之后将echo / print放在此处,并且它返回解析错误(意外的t_echo / print)。他说不要再从这里添加代码,因为它完成了。

2 个答案:

答案 0 :(得分:1)

http://php.net/manual/en/function.mysql-query.php

引用:混合mysql_query(字符串$ query [,resource $ link_identifier = NULL])

是$ localhost的link_identifier?我猜它不会......

如果没有,只需删除该arg并使用仅需要1个arg(查询)的mysql_query()。与select_db()函数相同。假设您只有1个mysql连接(link_identifier),默认情况下它将被使用。

答案 1 :(得分:0)

我认为您可以使用单个isset()作为提交按钮,而不是使用带有帖子值的多个isset()。一旦添加数据,您是否检查数据库以验证是否已添加值。因为有时虽然脚本没有t显示任何错误,可以避免插入值。并尝试通过将此添加到脚本来启用显示错误。

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);