1 个复选框,其中 2 个来自 sql 的值通过选择

发布于 2024-11-08 06:04:10 字数 1402 浏览 0 评论 0原文

大家好 现在已经两周了,我正在寻找问题的答案,但没有成功。希望有人可以帮助我,

我有一个从数据库调用的复选框选项

<form style="margin-top:-30px" method="POST" action="extradata.php">
<?php
$sql = "SELECT ext_id,ext_price,ext_name,ext_description FROM tbl_extra ORDER by ext_id ASC";
$result = mysql_query($sql) or trigger_error(mysql_error(),E_USER_ERROR);
$number = mysql_num_rows($result);
$i = 0;
while ($number > $i) {
$NA= mysql_result($result,$i,"ext_name");
$PR= mysql_result($result,$i,"ext_price");

print "<input type='checkbox' name='extra[]' value='$NA'></td>";
$i++;
}
?>

我想做的是将值传递给 extradata.php 并带有 2 个值
1.$NA,
2.$PR
选中该框后,将 $NA 的值插入到 ex_tra 中,将 $PR 的值插入到 ex_price

<代码>extradata.php

<?php
require_once 'library/config.php';

$id=$_POST['pd_id'];
$ssid=$_POST['ct_session_id'];
$total=$_POST['tot'];
$name=$_POST['basedes'];
$qty=$_POST['ct_qty'];
$extra_array = $_POST['extra'];
if ( $extra_array > "0" ) {
foreach ($extra_array as $one_extra) {
$source .= $one_extra.", "; }
$extra = substr($source, 0, -2);
} else {}
$result=mysql_query("INSERT INTO tbl_cart (pd_id, ct_qty, ct_session_id, ex_tra, ex_tra2, ex_price, ct_date) VALUES ('$id','$qty','$ssid','$extra','$name','$total', NOW())");

?>

致以最诚挚的问候

Hi all
Now is 2 week that i searching for an answer to my problem and no luck. hope somone can help me on this

i have a chackbox option that i call from the database

<form style="margin-top:-30px" method="POST" action="extradata.php">
<?php
$sql = "SELECT ext_id,ext_price,ext_name,ext_description FROM tbl_extra ORDER by ext_id ASC";
$result = mysql_query($sql) or trigger_error(mysql_error(),E_USER_ERROR);
$number = mysql_num_rows($result);
$i = 0;
while ($number > $i) {
$NA= mysql_result($result,$i,"ext_name");
$PR= mysql_result($result,$i,"ext_price");

print "<input type='checkbox' name='extra[]' value='$NA'></td>";
$i++;
}
?>

What i trying to do is to pass the value to extradata.php with 2 value
1.$NA,
2.$PR
when selected box then insert to the database the value of $NA to ex_tra and $PR to ex_price

the extradata.php

<?php
require_once 'library/config.php';

$id=$_POST['pd_id'];
$ssid=$_POST['ct_session_id'];
$total=$_POST['tot'];
$name=$_POST['basedes'];
$qty=$_POST['ct_qty'];
$extra_array = $_POST['extra'];
if ( $extra_array > "0" ) {
foreach ($extra_array as $one_extra) {
$source .= $one_extra.", "; }
$extra = substr($source, 0, -2);
} else {}
$result=mysql_query("INSERT INTO tbl_cart (pd_id, ct_qty, ct_session_id, ex_tra, ex_tra2, ex_price, ct_date) VALUES ('$id','$qty','$ssid','$extra','$name','$total', NOW())");

?>

Best regard to all

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

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

发布评论

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

评论(1

是伱的 2024-11-15 06:04:10

在一个变量中发送两个值必然会变得混乱。我只需发送 id 并在 extradata.php 中再次从数据库获取相应的值。

如果您确实想发送多个值,我会为复选框使用固定索引(不是 extra[] 而是 extra[SOME_NUMBER] 并在其旁边添加一个隐藏字段( extra_pr[SOME_NUMBER]?) 来传递第二个值,

请注意,您必须使用固定索引,因为未选中的复选框不会被发布,但所有隐藏字段都会被发布。

Sending two values in one variable is bound to get messy. I would just send the id and get the corresponding values from the database again in extradata.php.

If you really want to send multiple values, I would use fixed indices for the checkboxes (not extra[] but extra[SOME_NUMBER] and add a hidden field next to it (extra_pr[SOME_NUMBER]?) to pass the second value.

Note that you have to use fixed indices as unchecked checkboxes don´t get posted but all hidden fields will get posted.

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