【问题标题】:Can't echo to website as data not inserting to database由于数据未插入数据库,因此无法回显到网站
【发布时间】:2016-03-15 03:16:48
【问题描述】:

我需要对朋友完成的工作进行故障排除。似乎没有错误,因为我可以毫无问题地启动网站。当从下拉列表中选择一个选项并单击添加时,它不会将数据插入 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)。他说不要再从这里添加代码,因为它已经完成了。

【问题讨论】:

    标签: php mysql curl sdn


    【解决方案1】:

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

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

    $localhost 是链接标识符吗?估计不是..

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

    【讨论】:

    • 这是我的 $localhost(来自其他 php 脚本)$localhost = mysql_pconnect($hostname_localhost, $username_localhost, $password_localhost)
    • @IntelX7 mysql_select_db($database_localhost, $localhost) or die('Unable to select db, error: ' . mysql_error()); mysql_query($insertSQL, $localhost) or die('Unable to INSERT, error: ' . mysql_error()); echo '<br />Can you see this?'; 如果没有看到任何消息,请尝试“查看页面源”,可能没有显示。
    • 当我“查看页面源代码”时,它只显示html代码
    • @IntelX7 您向 CodeCanyon 提到的错误:听起来表格是空的,所以“SELECT *...”返回一个空集..这意味着 while() 主体永远不会执行,并且留下 $table 未初始化..这会导致该错误。您应该添加调试消息以进行验证。最简单的修复(不检查返回的空集)可能是更改以下行(添加 i>0 检查)if(($i > 0) && (array_search(... ..由于您没有看到错误消息,这意味着插入正在工作,或者该代码没有运行。添加调试 echos() 以验证脚本是否正在运行结束。
    • 正如您当前的代码一样,一旦表中有数据,并且您去更新一行,您还将插入一个具有相同数据的新行..您需要添加一个 else{ } 子句到 if(($i > 0) && (array_search($_POST['InputFromPort'], $table) != false)) ,或其他控制程序流/逻辑的方式。 (?) 只是为了调试,使用 $i > 0 检查看看会发生什么,但最终你应该正确检查返回的空集并相应地更改流控制。
    【解决方案2】:

    我认为您可以将单个 isset() 用于提交按钮而不是多个 isset() 与 post 值。添加数据后,您是否检查数据库以验证是否添加了值。因为有时虽然脚本没有t 显示任何错误,可以避免插入值。并尝试通过将其添加到脚本来启用显示错误。

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

    【讨论】:

    • 我确实检查了数据库。没有值
    • OK。也就是说你的数据插入数据库也没有工作。你启用php报错和检查了吗?
    • 有 2 个错误。 PHP 通知:未定义变量:第 134 行 /opt/lampp/htdocs/AddRemove.php 中的表和 PHP 警告:array_search() 期望参数 2 为数组,在 /opt/lampp/htdocs/AddRemove.php 中给出 null 134. 过了一会儿($row = mysql_fetch_array($result))
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-11
    • 2014-04-27
    相关资源
    最近更新 更多