我有点坚持这一点。仅当单击该组复选框时,我才尝试插入项目组。我已经测试了没有复选框的 SQL 插入,并且 SQL 可以完美地工作并插入数据。但是当我添加复选框和 if/else 语句时,它开始给我带来麻烦。
例如: -
如果我选择第 1 组而不是第 2 组 - SQL 仅在插入第 1 组数据时正常工作 -
如果我选择第 2 组而不是第 1 组 - 插入仍然插入第 1 组数据 -
如果我选择第 1 组和第 2 组- SQL 插入两行第 1 组数据 -
如果我不选择任何组,则会收到“警告:为 /home 中的 foreach() 提供的参数无效...” - 我稍后会对其进行整理。
我已经标记了“第 1 组”和“第 2 组”。如果单击复选框,我需要代码仅插入组,并忽略未单击的复选框组。任何帮助都会很棒。谢谢。
我一直在使用以下代码进行测试:
形式:
<form id="Form" method="post" action="/random.php" name="Form">
<input id="itemCurrency" type="hidden" value="2" name="itemCurrency"></input>
<div class="row">
<div class="col-lg-4">
<div class="itemHolder">
<img class="itemImg thumbnail" src="http://images.com/i/images.jpg"></img>
<div class="itemName">swim Shorts Wi...</div><
<div class="itemPrice">$33.33</div>
<div class="itemHref"><a target="_blank" href="http://random.com/product.aspx?id=40"> Visit Item Page </a></div>
</div>
</div>
//GROUP 1
<input id="itemCheckBox[]" type="checkbox" value="checked" name="itemCheckBox[]"></input>
<input id="itemName[]" type="hidden" value="some random text" name="itemName[]"></input>
<input id="itemHref[]" type="hidden" value="http://random.com/product.aspx?id=2140" name="itemHref[]"></input>
<input id="itemImg[]" type="hidden" value="http://images.com/i/image1s.jpg" name="itemImg[]"></input>
<input id="itemPrice[]" type="hidden" value="$37.33" name="itemPrice[]"></input>
//END GROUP 1
<div class="col-lg-4">
<div class="itemHolder">
<img class="itemImg thumbnail" src="http://images.com/i/image_xl.jpg"></img>
<div class="itemName">Bomber Jacket With Fur</div>
<div class="itemPrice"> $88.88</div>
<div class="itemHref"><a target="_blank" href="http://random.com/Bomber-Jacket-With-fur/..">Visit Item Page</a></div>
</div>
</div>
//GROUP 2
<input id="itemCheckBox[]" type="checkbox" value="checked" name="itemCheckBox[]"></input>
<input id="itemName[]" type="hidden" value="Bomber Jacket With Fur" name="itemName[]"></input>
<input id="itemHref[]" type="hidden" value="http://random.com/Bomber-Jacket-With-fur/.." name="itemHref[]"></input>
<input id="itemImg[]" type="hidden" value="http://images.com/i/image_xl.jpg" name="itemImg[]"></input>
<input id="itemPrice[]" type="hidden" value="$88.88" name="itemPrice[]"></input>
//END GROUP 2
<input id="site" type="hidden" value="1" name="site"></input>
<input id="submit_form" type="submit" value="yes" name="submit_form"></input>
</form>
PHP
if($submitForm == "yes" && $site == "1") {
foreach($_POST['itemCheckBox'] as $key => $val)
{
$fields[] = array(
'itemCheckBox'=>$_POST['itemCheckBox'] [$key],
'itemName' =>$_POST['itemName'] [$key],
'itemHref' =>$_POST['itemHref'] [$key],
'itemImg' =>$_POST['itemImg'] [$key],
'itemPrice' =>$_POST['itemPrice'][$key] );
//Remove non numaric characters from price(leave commas).
$itemPrice = preg_replace('/[^0-9,.]/s', '', $itemPrice);
//Data fields not in form
$userId = "username";
$userIp = $_SERVER['REMOTE_ADDR'];
$itemType = "ADD LATER";
$itemOutFit = "ADD LATER";
$site = "1";
$itemSale = "1";
//Only insert if checkbox is clicked --NOT WORKING, ????
if (isset($_POST['itemCheckBox'])){
echo "CHECKED";
echo "<br>";
/*** INSERT data ***/
$sql = "INSERT INTO items (userId,itemHref,itemImg,itemPrice,itemName,userIp,itemSite,itemType,itemCurrency,itemOutFit,itemSale) VALUES (:userId,:itemHref,:itemImg,:itemPrice,:itemName,:userIp,:itemSite,:itemType,:itemCurrency,:itemOutFit,:itemSale)";
$q = $conn->prepare($sql);
$q->execute(array(':userId'=>$userId,
':itemHref'=>$itemHref[$key],
':itemImg'=>$itemImg[$key],
':itemPrice'=>$itemPrice[$key],
':itemName'=>$itemName[$key],
':userIp'=>$userIp,
':itemSite'=>$itemSite,
':itemType'=>$itemType,
':itemCurrency'=>$itemCurrency,
':itemOutFit'=>$itemOutFit,
':itemSale'=>$itemSale));
} else {
echo "NOT CHECKED";
echo "<br>";
}
/*
echo $conn->errorCode();
echo "<br>"; ('SET CHARACTER SET utf8')
echo $conn->errorInfo();
echo "<br>";
//die(print_r($q->errorInfo(), true));
*/
}
更新试图将 php 剥离为基本代码,但仍然没有运气。做同样的事情....任何人都知道我该如何解决这个问题。
foreach($_POST['itemCheckBox'] as $key => $val)
{
$i = 0;
$itemCheckBox = $_POST['itemCheckBox'];
$itemName = $_POST['itemName'];
$itemHref = $_POST['itemHref'];
$itemImg = $_POST['itemImg'];
$itemPrice = $_POST['itemPrice'];
echo $i;
echo "<br>";
$i = $i +1;
if (isset($_POST['itemCheckBox'])){
echo "<br>";
echo "CHECKED";
echo "<br>";
echo "<br>";
echo $itemCheckBox[$i];
echo "<br>";
echo $itemName[$i];
echo "<br>";
echo $itemHref[$i];
echo "<br>";
echo $itemImg[$i];
echo "<br>";
echo $itemPrice[$i];
} else {
echo "NOT CHECKED";
echo "<br>";
$i = $i +1;
}
}