Ошибка при удалении — только переменные могут быть переданы по ссылке PHP / MYSQLI

Я отправляю данные формы и удаляю их перед вставкой, а не проверяю, нужно ли делать обновление. Однако я получаю следующую ошибку:

Неустранимая ошибка: необученная ошибка: только переменные могут быть переданы по ссылке в /public_html/fitbit2/admin/admin_assignments.php:98 Трассировка стека: # 0 {main} добавлено в /public_html/fitbit2/admin/admin_assignments.php в строке 98

Это строка 98:

$stmtdel->bind_param( "ss", $Assignment, $Section[$i], $Semester, $Year );

Вот еще код:

if ( $_POST ) {

//assign date variables
$PreStartDate = $_POST[ 'PreStartDate' ];
$PreEndDate = $_POST[ 'PreEndDate' ];

$PostStartDate = $_POST[ 'PostStartDate' ];
$PostEndDate = $_POST[ 'PostEndDate' ];

$semesterarr = array('Fall','Spring','Summer1','Summer2','Winter','May');
if (in_array($_POST['Semester'], $semesterarr)) {
$Semester = clean($_POST['Semester']);
}


if (filter_var($_POST['Year'], FILTER_VALIDATE_INT) !== FALSE) {
$Year = clean((int)$_POST['Year']);
}

//convert time format to prepare for mysql insert
$PreStartTime = clean( $_POST[ 'PreStartTime' ] );
$PreStartTime = date( "H:i:s", strtotime( "$PreStartTime" ) );

$PreEndTime = clean( $_POST[ 'PreEndTime' ] );
$PreEndTime = date( "H:i:s", strtotime( "$PreEndTime" ) );

$PostStartTime = clean( $_POST[ 'PostStartTime' ] );
$PostStartTime = date( "H:i:s", strtotime( "$PostStartTime" ) );

$PostEndTime = clean( $_POST[ 'PostEndTime' ] );
$PostEndTime = date( "H:i:s", strtotime( "$PostEndTime" ) );

//create datetime for mysql insertion
$PreStartDate = $PreStartDate . ' ' . $PreStartTime;
$PreEndDate = $PreEndDate . ' ' . $PreEndTime;

$PostStartDate = $PostStartDate . ' ' . $PostStartTime;
$PostEndDate = $PostEndDate . ' ' . $PostEndTime;

$PointsPossible = filter_var( $_POST[ 'PointsPossible' ], FILTER_SANITIZE_NUMBER_INT );
$Assignment = filter_var( $_POST[ 'Assignment' ], FILTER_SANITIZE_STRING );


$Section = $_POST[ 'SectionNumber' ];

if ( empty( $Section ) ) {
echo( "You didn't select any sections. Please click the back button in your browser to restart the process." );
exit;
} else {

//DELETE before inserting
//$stmtsel = $connection->prepare( "SELECT AssignmentID FROM Assignments WHERE Assignment=? AND SectionNumber=?" );

$stmtdel = $connection->prepare( "DELETE FROM Assignments WHERE Assignment=? AND SectionNumber=? AND Semester=? AND Year=?" );

$stmt = $connection->prepare( "INSERT INTO Assignments SET PreStartDate=?, PreEndDate=?, PostStartDate=?, PostEndDate=?, Assignment=?, PointsPossible=?, SectionNumber=?, Semester=?, Year=?" );

$N = count( $Section );
$success = '';

for ( $i = 0; $i < $N; $i++ ) {

$stmtdel = $connection->prepare("DELETE FROM Assignments WHERE Assignment=? AND SectionNumber=? AND Semester=? AND Year=?");
// 4 parameters, $sectionNumber does not need to be defined at this stage
$stmtdel->bind_param('ssss', $Assignment, $sectionNumber, $Semester, $Year);

$stmt = $connection->prepare("INSERT INTO Assignments SET PreStartDate=?, PreEndDate=?, PostStartDate=?, PostEndDate=?, Assignment=?, PointsPossible=?, SectionNumber=?, Semester=?, Year=?");
// 9 parameters
$stmt->bind_param('sssssssss', $PreStartDate, $PreEndDate, $PostStartDate, $PostEndDate, $Assignment, $PointsPossible, $sectionNumber, $Semester, $Year);

foreach ($Section as $sectionNumber) {
// $sectionNumber becomes defined here
$stmtdel->execute();
$stmt->execute();
}

$result = $stmt->get_result();
if ( $result->num_rows === 0 ) {
BodyHeader( "No Data", '', '' );
?>
<div class="alert alert-danger" role="alert">
There was a problem. None of the sections were updated.
</div>
<?php
BodyFooter();
exit;
}
$success .=   $Section[$i] . " ";

}
}
}

?>


<?php //show sync message at top of page
if(isset($success)){?>
<div class="alert alert-success alert-dismissable fade show" id="flash-msg" role="alert">
You successfully updated the following sections: <?=$success; ?>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<?php } ?>

<h1 class="h2">Setup Assignments</h1>
<p>&nbsp;</p>
<form action="admin_assignments.php" method="post">
<p style="font-weight: bold">Which section(s) would you like to apply this assignment to? </p>
<?php $stmt4 = $connection->prepare("SELECT DISTINCT SectionNumber FROM Courses WHERE SectionNumber != '' ORDER BY SectionNumber ASC
") or die($connection->error);
$stmt4->execute();
$result4 = $stmt4->get_result();

while ($row4 = $result4->fetch_assoc()):
?>
<input type="checkbox" name="SectionNumber[]" value="<?=$row4['SectionNumber'];?>" id="SectionNumber[]"><?=$row4['SectionNumber'];?>

<?php endwhile; ?>


<p>&nbsp;</p>


<div class="form-group has-float-label">

<select name="Assignment" class="form-control" required autofocus>
<option value="" selected>Select Assignment</option>

<?php $stmt4 = $connection->prepare("SELECT DISTINCT Assignment, AssignmentID FROM Assignments GROUP BY Assignment
") or die($connection->error);
$stmt4->execute();
$result4 = $stmt4->get_result();

while ($row4 = $result4->fetch_assoc()):
?>

<option value="<?=$row4['Assignment'];?>"><?=$row4['Assignment'];?></option>

<?php endwhile; ?>
</select>
</div>


<div class="form-group has-float-label">
<input type="text" name="PointsPossible" id="PointsPossible" value="" class="form-control" placeholder="Points Possible" required autofocus>
<label for="PointsPossible">Points Possible</label>
</div>

<div class="form-group has-float-label">

<select name="semester" id="semester" class="form-control">
<option selected="selected">--- Semester? ---</option>
<option value="summer1">Summer 1</option>
<option value="summer2">Summer 2</option>
<option value="fall">Fall</option>
<option value="spring">Spring</option>
<option value="winter">Winter Minimester</option>
<option value="may">May Minimester</option>
</select>
</div>

<div class="form-group has-float-label">
<select name="year" class="form-control">
<?php
for($i=date('Y'); $i < date('Y')+2; $i++){
echo "<option value=\"$i\">$i</option>";
}
?>
</select>
</div>
<p>&nbsp;</p>

<div class="form-group row">
<div class="col-sm-10">
<strong>Pre-Test</strong>
<p>Select the opening and closing dates for the Pre-Test:</p>
<p id="predates" class="input-group">
<input type="text" class="date start form-control" name="PreStartDate" placeholder="Start Date" required autofocus style="width: 150px" autocomplete="off"/>
<input type="text" class="time start form-control" name="PreStartTime" placeholder="Start Time" required autofocus style="width: 150px" autocomplete="off"/> &nbsp;&nbsp; to &nbsp;&nbsp;
<input type="text" class="date end form-control" name="PreEndDate" placeholder="End Date" required autofocus style="width: 150px" autocomplete="off"/>
<input type="text" class="time start form-control" name="PreEndTime" placeholder="End Time" required autofocus style="width: 150px" autocomplete="off"/>
</p>
</div>
</div>
<div class="form-group row">
<div class="col-sm-10">
<strong>Post-Test</strong>

<p>Select the opening and closing dates for the Post-Test:</p>
<p id="postdates" class="input-group">
<input type="text" class="date start form-control" name="PostStartDate" placeholder="Start Date" required autofocus style="width: 150px" autocomplete="off"/>
<input type="text" class="time start form-control" name="PostStartTime" placeholder="Start Time" required autofocus style="width: 150px" autocomplete="off"/> &nbsp;&nbsp; to &nbsp;&nbsp;
<input type="text" class="date end form-control" name="PostEndDate" placeholder="End Date" required autofocus style="width: 150px" autocomplete="off"/>
<input type="text" class="time start form-control" name="PostEndTime" placeholder="End Time" required autofocus style="width: 150px" autocomplete="off"/>
</p>
</div>
</div>
<input class="btn btn-lg btn-primary btn-block" type="submit" value="Save Changes">

</form>

Что мне нужно сделать, чтобы исправить эту ошибку?

Спасибо,

Тим

0

Решение

Проблема здесь в том, что mysqli_stmt::bind_param принимает переменные ссылки. $Section[$i] не квалифицируется как значение, полученное из массива.

При выполнении операторов в цикле гораздо эффективнее сначала подготовить и связать операторы и выполнять их только внутри цикла.

При связывании, поскольку используются ссылки на переменные, сами переменные не нужно определять на этом этапе.

Имея это в виду, вы должны попробовать это

$stmtdel = $connection->prepare("DELETE FROM Assignments WHERE Assignment=? AND SectionNumber=? AND Semester=? AND Year=?");
// 4 parameters, $sectionNumber does not need to be defined at this stage
$stmtdel->bind_param('ssss', $Assignment, $sectionNumber, $Semester, $Year);

$stmt = $connection->prepare("INSERT INTO Assignments SET PreStartDate=?, PreEndDate=?, PostStartDate=?, PostEndDate=?, Assignment=?, PointsPossible=?, SectionNumber=?, Semester=?, Year=?");
// 9 parameters
$stmt->bind_param('sssssssss', $PreStartDate, $PreEndDate, $PostStartDate, $PostEndDate, $Assignment, $PointsPossible, $sectionNumber, $Semester, $Year);

foreach ($Section as $sectionNumber) {
// $sectionNumber becomes defined here
$stmtdel->execute();
$stmt->execute();
}
1

Другие решения

Когда вы используете функцию bind_param, вы можете передавать переменные только как аргументы, а не как значение. Обратите внимание, в вашем коде это $Section[$i] это не переменная, а значение из доступа к массиву. Чтобы исправить это, вам нужно назначить переменную $SectionValue = $Section[$i] и передать эту переменную в bind_param.

1

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector