如何在PHP中添加从数组填充的表行

问题描述 投票:0回答:5

我试图从应用程序中提取一些信息,然后根据这个数组创建表行,我在这段代码的中间丢失,我需要帮助。

提交代码是:

<?php
session_start(); // NEVER forget this!
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("webauth", $con);
$myusername = $_SESSION['myusername'];
//$result = mysql_query("SELECT * FROM user_pwd");
$sql5 = ("SELECT dept from `user_pwd` WHERE  name = '$myusername'");
$result5=mysql_query($sql5);
$row5=mysql_fetch_array($result5);
// echo "<br />";

mysql_close($con);
$url3 = 'http://localhost:1090/WebEmpLoad.hal';
$myvars3 = 'department='.$row5['dept'];
$ch3 = curl_init( $url3 );
curl_setopt( $ch3, CURLOPT_POST, 1);
curl_setopt( $ch3, CURLOPT_POSTFIELDS, $myvars3);
curl_setopt( $ch3, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch3, CURLOPT_HEADER, 0);
curl_setopt( $ch3, CURLOPT_RETURNTRANSFER, 1);
$response3 = curl_exec( $ch3 );
//echo $response3;
$arr = (explode(',',$response3,100));
$Num = sizeof($arr);
$rows = ($Num - 1)/2;
echo $rows;
?>

而我正在尝试根据$arr的值创建raw。

检查代码和脚本here

javascript php mysql rows
5个回答
1
投票

可以通过一些方法来改进脚本。既然你的问题很广泛,我会指出其中的一些问题。

第一..

$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

可简单缩短

$con = mysql_connect("localhost","root","") or die('Could not connect: ' . mysql_error());

其次,您忘记在mysql_query()中添加资源参数。虽然这个参数是可选的,但是通过你的变量名称($sql15),我假设你可能有多个连接。

$result5=mysql_query($sql5, $con);

另外,我不确定你在这做什么..

$sql5 = ("SELECT dept from `user_pwd` WHERE  name = '$myusername'");

你的语法可能对我所知道的完全有效,但我以前从未见过如此定义的字符串,并且快速的Google搜索没有向我显示任何内容..所以你可能想要改变它只是..

$sql5 = "SELECT dept from `user_pwd` WHERE  name = '$myusername'";

接下来,在调用数据之前关闭连接。您可能希望将mysql_close()向下移动到$myvars3 = 'department='.$row5['dept'];之后的一条线

哦,你正在使用mysql_fetch_array(),然后将其称为关联数组($row5['dept'])。为了安全起见,要么将$row5=mysql_fetch_array($result5);更改为$row5=mysql_fetch_assoc($result5);,要么像这样添加关联数组结果类型.$row5=mysql_fetch_array($result5, MYSQL_ASSOC);

最后,我们将解决您的数据上传问题。

我不知道你是如何解决这个问题的,但你基本上是把一个网页打成一个数组,只要有一个逗号并尝试将数组上传到一个表中。除非您确切知道该页面上出现了多少逗号,并且您知道该数字与表格中的列数匹配,否则这是一个非常糟糕的主意。我不知道你想要完成什么,但我向你保证,有更好的方法。

并回答你的实际问题..插入行的SQL语法是“INSERT INTO table(column1,column2)VALUES('value1','value2')”;

最后注意..您正在使用旧的API,切换到PDO。看到大红色警告here

所以,把它们放在一起......

session_start(); // NEVER forget this!
$con = mysql_connect("localhost","root","") or die('Could not connect: ' . mysql_error());
mysql_select_db("webauth", $con);
$myusername = $_SESSION['myusername'];
//$result = mysql_query("SELECT * FROM user_pwd");
$sql5 = "SELECT dept from `user_pwd` WHERE  name = '$myusername'";
$result5=mysql_query($sql5, $con);
$row5=mysql_fetch_assoc($result5);
// echo "<br />";

$url3 = 'http://localhost:1090/WebEmpLoad.hal';
$myvars3 = 'department='.$row5['dept'];
mysql_close($con);
$ch3 = curl_init( $url3 );
curl_setopt( $ch3, CURLOPT_POST, 1);
curl_setopt( $ch3, CURLOPT_POSTFIELDS, $myvars3);
curl_setopt( $ch3, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch3, CURLOPT_HEADER, 0);
curl_setopt( $ch3, CURLOPT_RETURNTRANSFER, 1);
$response3 = curl_exec( $ch3 );
//echo $response3;
$arr = (explode(',',$response3,100));
$Num = sizeof($arr);
$rows = ($Num - 1)/2;
echo $rows;

0
投票
AS use have array of data.
user the code where your want to print the rows from this array
**<table>**
<?php 
foreach ($arr as $rows)
{
echo "<tr><td>".$rows."</td></tr>";
}
?>
**</table>**

0
投票

好的,我看到你在做什么。

试试吧。

echo "<table>";
$col = "left";
foreach($rows as $row){
    if($col == "left"){
        echo "<tr><td>$row</td>";
        $col = "right";
    }else{
        echo "<td>$row</td></tr>";
        $col = "left";
    }
}
echo "</table>";

0
投票

我现在找出了你的jsFiddle。有三种方法可以像你一样做,取决于你需要做什么。

第一种方法是创建一个完全准备好行和列的html表,但是隐藏所有行,并且只显示要使用jQuerys .show()和.hide()显示的行,具体取决于下拉列表的值。按下添加按钮。 <tr id='row373' style='display:none'><td>..</td>..</tr>

第二种方法是使用隐藏的html元素发送表的所有数据,如包含lis的div,并使用数据属性<li id='row373' data-key='value'>来装饰所有li,jQuery可以使用.data('key')函数读取是每列数据的名称。

第三种也许是最好的方法是使用ajax请求将单行或多行加载为.html或.json数据。这需要您编写一个特殊的.php脚本,该脚本仅从数据库中读取数据并以您希望使用jQuery访问的格式将其吐出。该脚本可以由jQuerys .ajax()。get()或.post()函数调用。


0
投票

@Tuna Fish:我使用了代码的逻辑。伙计们,感谢所有的帮助,但我设法找到了解决问题的好方法。你可以检查以下code

© www.soinside.com 2019 - 2024. All rights reserved.