PHP数组未正确发布-在检查中显示,但在转储中没有显示

时间:2018-09-22 15:57:09

标签: php

问题:

我的网站页面已损坏。它从另一个页面接收到一个POST消息,并发送了一系列的复选框,直到以前我在代码中进行了一些调整,现在都坏了。

如果我检查了发送到Google Chrome浏览器页面中的POST数组:

campName: Correct
notes: Some Notes
userid: 47
userid: 45
userid: 43
userid: 44

这是正确的,并且与上一页中选中的复选框有关,但是如果我var_dump($_POST['userid']),我只会得到一个值(列表的底部)。

我的操作在页面上也被破坏,并显示一条错误消息:

Warning: Invalid argument supplied for foreach()$_POST['userid']有关,即使它看起来非常有效。

我已经盯着这个看了好几个小时,无法解决,出现在多个浏览器和机器上。

“ campName”和“ notes”顺利通过,似乎只有userid数组引起了问题。

接受帖子的页面:

<?php
include_once 'header.php';
var_dump($_POST);

?>

<?php
if (!isset ($_POST['campName'])){
    header ('Location: /error.php/?err=You+must+enter+details+to+create+a+campaign');
}else{
$sql = "SELECT name FROM campaigns WHERE name=?";
mysqli_stmt_prepare($stmt,$sql);
mysqli_stmt_bind_param($stmt, "s", $_POST['campName']);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$num=mysqli_num_rows($result);
if ($num!=0){

$jumbo = "<center>There is already a campaign with the name: <br></center> ";
} else{
    $jumbo = "Create: ";
}
}
?>


<div class="container" id="first-screen"> <!-- Main Container for Page-->
<div class="jumbotron">
<h1 class = "jumbotron-header">  <i> <center> <?php echo $jumbo . $_POST['campName']; ?> </strong></i></h1>
</div> <!-- Close Jumbotron-->
<p>You'll be creating this campaign with the following notes which will be available to all site members:</p>
<br>
<p class="font-weight-light"><? echo $_POST['notes']; ?> 
<P>  The following members will be assigned as committee members,  these can be changed as required in the admin panel.  Committee members will be alerted by SMS that the campaign has been 
created and that they have been assigned as committee members.</p>


<?php 

   $sql = "SELECT user_first, user_last FROM users WHERE user_uid =?";
   mysqli_stmt_prepare($stmt, $sql);
   mysqli_stmt_bind_param($stmt, "s", $_SESSION['username']);
   mysqli_stmt_execute($stmt);
   $result = mysqli_stmt_get_result($stmt);
   $data= mysqli_fetch_assoc ($result);
   echo '<strong>' . $data['user_first'] . ' ' . $data['user_last'] . ' ' . ' </strong> <br>';


foreach ($_POST['userid'] as $userid){

    $sql = "SELECT user_first, user_last FROM users WHERE user_id =?";
    mysqli_stmt_prepare($stmt, $sql);
    mysqli_stmt_bind_param($stmt, "s", $userid);
    mysqli_stmt_execute($stmt);
    $result = mysqli_stmt_get_result($stmt);
    $data= mysqli_fetch_assoc ($result);
    echo $data['user_first'] . ' ' . $data['user_last'] . ' ' . '<br>';

   }

?>
<div>
<a href="/createcampaign.php" class="btn btn-primary" role="button">Restart</a>
<?php 

if($num==0){echo <<<EOT
    <form action="campcreate.php" method="post"
<button type="submit" name="create" class="btn btn-success" id="create">Finish</button>
</form>
EOT;
}
?>
</div> <!-- Button Div-->
</div> <!--Close main container-->


<?php
//Double check record will not be overwritten or double submission:
$sql="SELECT * FROM campaigns WHERE name=?";
mysqli_stmt_prepare($stmt, $sql);
mysqli_stmt_bind_param($stmt, "s", $_POST['campName']);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$rows = mysqli_num_rows($result);
if($rows!=0){
     die;
}else{
    //Create a new record in the master campaigns list//
$sql="INSERT INTO campaigns (name, created) VALUES (?,?)";
mysqli_stmt_prepare($stmt, $sql);
mysqli_stmt_bind_param($stmt, "ss", $_POST['campName'], $_SESSION['username']);
mysqli_stmt_execute($stmt);
$id = mysqli_insert_id($conn);

$tablename = "camp_" . $id;
echo $tablename;
//Create a new table for the campaign

$sql = "CREATE TABLE $tablename
    (
    id INT NOT NULL PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    notes VARCHAR(3000) NOT NULL
    )
    ";
    mysqli_stmt_prepare($stmt, $sql);
    mysqli_stmt_execute($stmt);

// Insert data in to the newly created campaign table
$sql="INSERT into $tablename (id, name, notes) VALUES (?,?,?)";
mysqli_stmt_prepare($stmt, $sql);
mysqli_stmt_bind_param($stmt, "iss", $id, $_POST['campName'], $_POST['notes']);
mysqli_stmt_execute($stmt);

//Change Tablename

$tablename = "com_" . $id;
echo $tablename;

//Create Committee Table

$sql = "CREATE TABLE $tablename
    (
    id int NOT NULL PRIMARY KEY AUTO_INCREMENT,
    members VARCHAR(255) NOT NULL
    )
    ";
    mysqli_stmt_prepare($stmt, $sql);
    mysqli_stmt_execute($stmt);

    //Insert Committee Memebers

    foreach ($_POST['userid'] as $userid){
        $sql="INSERT into $tablename (members) VALUES (?)";
        mysqli_stmt_prepare($stmt, $sql);
        mysqli_stmt_bind_param($stmt, "s", $userid);
        mysqli_stmt_execute($stmt);
    }
    //Insert extra line for originator to committee list

    $sql="INSERT into $tablename (members) VALUES (?)";
    mysqli_stmt_prepare($stmt, $sql);
    mysqli_stmt_bind_param($stmt, "s", $_SESSION['id']);
    mysqli_stmt_execute($stmt);
    }

//Close Function



?>

帖子来自:

<?php
include 'header.php';
?>

<div class="container" id="first-screen"> <!-- Main Container for Page-->
<div class="jumbotron">
<h1 class = "jumbotron-header"> Manage Campaigns </h1>
</div> <!-- Close Jumbotron-->
<p>Create, modify or delete campaigns from the project.  This page is only available to site <strong>administrators</strong></p>
<p>Campaigns fall in to two groups,  either main campaigns or sub-campaigns.  For example PimJam 2019 could be a main campaign with fun-raiser events added as sub-campaigns.</p>
<p>Campaigns bind all site data together so it is VITAL that these are correctly ordered and maintained, deleting a campaign can disastiousy damage data on the website and should only be performed under recomendation 
of <strong>shitpostwizard</strong>.
<div>
<button type="button" class="btn btn-primary" id="create">Create a new Campaign</button>
<button type="button" class="btn btn-primary">Create a new Sub-campaign</button>
<button type="button" class="btn btn-danger">Delete a Campaign</button>
</div> <!-- Button Div-->
</div> <!--Close main container-->




<div class="container collapse" id="create-screen"> <!-- Main Container for Page-->
<div class="jumbotron">
<h1 class = "jumbotron-header"> Create a new campaign </h1>
</div> <!-- Close Jumbotron-->
<p>Enter the details below to create a new campaign.  Please ensure it is a new campaign which is required, not a new sub-campaign.</p>
<div> <!--Form Div-->
<form action="campcreate.php" class="form" method="post">
<label for "campName">Campaign Name:</label>
<input type="text" id="campName" class="form-control" name="campName" placeholder="Campaign Name" required minlength="6" autofocus>
<label for "notes">Description:</label>
<textarea class="form-control" name="notes" rows="5" id="notes" placeholder="Add any useful notes or descriptions here"></textarea>
<h3>Select committee memmbers:</h3>
<p>Committee members can be changed as required,  all members selected as committee members will be able to view documents, meetings etc. connected to the campaign.</p>
<p>Assigning a committee member does <strong>not</strong> grant them any different admin privillages on the site.</p>
<?php getnames(); ?>

<div>
<button type="submit" class="btn btn-success float-right" id="create">Create</button>
<button type="button" class="btn btn-primary float-right" id="restart">Back</button>
</div> <!--Form Div--->
</form>

</div> <!-- Button Div-->
</div> <!--Close main container-->


<script>
    $("#restart").click(function(){
    $("#create-screen").hide();
    $('#first-screen').fadeIn(200);
});
</script>




<script>
    $("#create").click(function(){
    $("#first-screen").hide();
    $('#create-screen').fadeIn(200);
});
</script>



?>   

在functions.php中生成的复选框:

function getnames(){
    global $conn;
$sqlga = "SELECT user_id, user_first, user_last FROM users WHERE (usr_acrive= 1) AND (perm!=0) ORDER BY user_last ASC";
$stmt = mysqli_stmt_init($conn);    
mysqli_stmt_prepare($stmt, $sqlga);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
while ($row = mysqli_fetch_assoc($result)) {
    if ($row['user_id']!=$_SESSION['user_id']){
        $checked="";
    }else{
        $checked="checked disabled";
    }
        $i++;
    echo '<input type="checkbox"'.$checked.' value="'. $row['user_id'] .'" name="userid"'. $i. '> '.$row['user_first'] . ' '. $row['user_last'] . '<br>';
   }
} 

有任何诊断指标吗?

1 个答案:

答案 0 :(得分:1)

您需要更改

echo '<input type="checkbox"'.$checked.' value="'. $row['user_id'] .'" name="userid"'. $i. '> '.$row['user_first'] . ' '. $row['user_last'] . '<br>';

收件人:

echo '<input type="checkbox"'.$checked.' value="'. $row['user_id'] .'" name="userid[]"'. $i. '> '.$row['user_first'] . ' '. $row['user_last'] . '<br>';

尝试提交所有具有相同name的值的数组时。您应该使用数组运算符[]来确保值在提交时不会相互覆盖。

我认为应该可以解决您的问题。