向 mysql 插入多行不起作用?我使用的是 php 5,这就是它不起作用的原因吗?

发布于 2024-12-03 00:08:20 字数 2174 浏览 0 评论 0原文

我在下面有一个完成的脚本,该脚本位于 insert_dimensions.php 文件中。虚线下方是网页中的另一个脚本。这些应该可以将一堆不同的数字放入 mysql 表的不同行上。如下所示,第一次运行脚本时,它会向 mysql 添加一行,但会在该行中放置一个零。第二次运行脚本时,它将应在第 1 行中的内容放入第 2 行中。即使它应该添加多于 1 行,但它只添加了 1 行。脚本运行的其余时间都是一样的。每次只添加 1 行,并且输入之前应该输入的值。

我已经尝试过很多 foreach 的事情。我不知道这些应该放在哪个页面上,因为包含它们的帖子(来自谷歌搜索)通常不会说将代码放在哪个页面上。我也尝试过将 [ ] 和其他类型的带或不带 $i 的括号放在addedWeight 的右侧。没有任何效果。我没有尝试过使用有时会看到的变量 $key,但我想知道这是否可以以某种方式解决问题。在它进入的 mysql 列中,addedWeight 的类型为 INT。我尝试将其更改为 varchar 类型,但仍然不起作用。

<?php
$con = mysql_connect("localhost","blah","blah");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("blah", $con);
$sql="INSERT INTO dimensions (weight_ounces, page_link, session_id)
VALUES
('$_POST[addedWeight]','$_POST[PageLink]','$_POST[SessionID]')";
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
mysql_close($con);
?>



--------------------------------------------------------------------------------------
<form action="insert_dimensions.php" method="post">
<?php
print "<input type='hidden' name='SessionID' value='" . session_id() . "'>";
$current_file_name = basename($_SERVER['REQUEST_URI'], ".php");
print "<input type='hidden' name='PageLink' value='" . $current_file_name . "'>";
mysql_connect("localhost", "blah", "blah") or die(mysql_error()); 
mysql_select_db("blah") or die(mysql_error()); 
$data = mysql_query('SELECT weight_ounces FROM dimensions_list WHERE session_id = "' . session_id() . '"'); 
$params = array();
while ($row = mysql_fetch_assoc($data)) {     
$params[] = $row['weight_ounces']; 
}
$combinations=getCombinations($params);
function getCombinations($array)
{
    $length=sizeof($array);
    $combocount=pow(2,$length);
for ($i=1; $i<$combocount; $i++)
    {
$binary = str_pad(decbin($i), $length, "0", STR_PAD_LEFT);
        $combination='';
        for($j=0;$j<$length;$j++)
        {
            if($binary[$j]=="1")
                $combination+=$array[$j];
        }
        $combinationsarray[]=$combination;
$varw = $combination;
print "<input type='text' name='addedWeight' value='{$varw}'>";
    }
    return $combinationsarray;
} 

I have a finished script below that goes in insert_dimensions.php file. Below the dotted line there is another script that goes in a webpage. These should work to put a bunch of different numbers into a mysql table on different rows. As it is below, the first time the script is run it adds a row to mysql, but it puts a zero in the row. The 2nd time the script is run, it puts what should have gone in row 1 into row 2. And it only adds 1 row even though it should be adding more than 1 row. The rest of the times the script is run it is the same thing. It's only adding 1 row each time, and it's entering the value that it should have entered the time before.

I have tried many foreach things. I don't know which page those are supposed to go on because the posts that contain them (from googling) don't usually say on which page to put the codes. I have tried putting [ ] and other types of brackets with and without $i to the right of addedWeight too. Nothing has worked. I have not tried using the variable $key which I see sometimes but I was wondering if that might fix the problem somehow. In the mysql column it goes into, addedWeight is type INT. I tried changing it to type varchar but it still didn't work.

<?php
$con = mysql_connect("localhost","blah","blah");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("blah", $con);
$sql="INSERT INTO dimensions (weight_ounces, page_link, session_id)
VALUES
('$_POST[addedWeight]','$_POST[PageLink]','$_POST[SessionID]')";
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
mysql_close($con);
?>



--------------------------------------------------------------------------------------
<form action="insert_dimensions.php" method="post">
<?php
print "<input type='hidden' name='SessionID' value='" . session_id() . "'>";
$current_file_name = basename($_SERVER['REQUEST_URI'], ".php");
print "<input type='hidden' name='PageLink' value='" . $current_file_name . "'>";
mysql_connect("localhost", "blah", "blah") or die(mysql_error()); 
mysql_select_db("blah") or die(mysql_error()); 
$data = mysql_query('SELECT weight_ounces FROM dimensions_list WHERE session_id = "' . session_id() . '"'); 
$params = array();
while ($row = mysql_fetch_assoc($data)) {     
$params[] = $row['weight_ounces']; 
}
$combinations=getCombinations($params);
function getCombinations($array)
{
    $length=sizeof($array);
    $combocount=pow(2,$length);
for ($i=1; $i<$combocount; $i++)
    {
$binary = str_pad(decbin($i), $length, "0", STR_PAD_LEFT);
        $combination='';
        for($j=0;$j<$length;$j++)
        {
            if($binary[$j]=="1")
                $combination+=$array[$j];
        }
        $combinationsarray[]=$combination;
$varw = $combination;
print "<input type='text' name='addedWeight' value='{$varw}'>";
    }
    return $combinationsarray;
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

落在眉间の轻吻 2024-12-10 00:08:20

你的代码看起来很奇怪。一些可能有帮助的评论:

1 - 这行有一个问题:

print "<input type='text' name='addedWeight' value='{$varw}'>";

您可能应该使用

print <input type='text' name='addedWeight[]' value='{$varw}'>";

现在的方式,您将仅将最后一个addedWeight传递给正在插入数据库的php。您需要有一个数组并传递该数组。

2 - 您可能需要在第一个脚本中使用一个循环(执行插入的脚本)。一旦你将addedWeight 作为数组传递,你就可以做类似的事情,

<?php
$con = mysql_connect("localhost","blah","blah");
if (!$con)
{
  die('Could not connect: ' . mysql_error());
}
mysql_select_db("blah", $con);

$addedWeight = $_POST[addedWeight]; <- load array passed into local variable
$counter = sizeof($addedWeight); <- count the number of items in the array

for ($i = 0; $i < $counter; $i++) <- here is the loop that will insert all the elements into the database.
{
  $sql="INSERT INTO dimensions (weight_ounces, page_link, session_id)
  VALUES
  (addedWeight[$i],'$_POST[PageLink]','$_POST[SessionID]')";

  if (!mysql_query($sql,$con))
  {
    die('Error: ' . mysql_error());
  }
}
mysql_close($con);
?>

我希望这有意义并帮助你解决问题。如果没有,请重新发帖。

最后一条评论 - 我会将函数移到表单之外。我喜欢将所有函数保存在单独的文件中或脚本的顶部或底部。如果您决定这样做,我在上面第 1 项中讨论的行将显示如下内容:

print "<input type='text' name=addedWeight[] value='$combinations'>";

这当然是个人偏好。

祝你好运!

Your code seems strange. A few comments that might help:

1 - this line has a problem:

print "<input type='text' name='addedWeight' value='{$varw}'>";

You should probably use

print <input type='text' name='addedWeight[]' value='{$varw}'>";

The way you have it now, you will pass only the last addedWeight to the php that is inserting in the database. You need to have an array and pass the array.

2 - you probably need a loop in your first script (the one that does the insert). Once you pass the addedWeight as an array, you can so something like

<?php
$con = mysql_connect("localhost","blah","blah");
if (!$con)
{
  die('Could not connect: ' . mysql_error());
}
mysql_select_db("blah", $con);

$addedWeight = $_POST[addedWeight]; <- load array passed into local variable
$counter = sizeof($addedWeight); <- count the number of items in the array

for ($i = 0; $i < $counter; $i++) <- here is the loop that will insert all the elements into the database.
{
  $sql="INSERT INTO dimensions (weight_ounces, page_link, session_id)
  VALUES
  (addedWeight[$i],'$_POST[PageLink]','$_POST[SessionID]')";

  if (!mysql_query($sql,$con))
  {
    die('Error: ' . mysql_error());
  }
}
mysql_close($con);
?>

I hope this makes sense and helps you resolve the issue. If not, please post again.

And one last comment - I would move the function outside the form. I like to keep all my functions in a separate file or at the top or bottom of the script. If you decide to do that the line I discussed above in my item 1 would read something like:

print "<input type='text' name=addedWeight[] value='$combinations'>";

This is a personal preference of course.

Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文