警告:implode()[function.implode]:传入的参数无效 第7行的C:\ xampp \ htdocs \ tempahperalatan \ Page2.php
警告:mysqli_error()期望参数1为mysqli,null给定 在第23行的C:\ xampp \ htdocs \ tempahperalatan \ Page2.php
我正在做一个表单来插入来自多个复选框的数据,我提交的数据被完美插入,但是一旦我打开页面,上面两个错误就出现了(page2.php)任何人都可以告诉我在我的错过了什么编码或问题在哪里?先感谢您。
以下参考是我的PHP编码:
<?php
// Make a MySQL Connection
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("tempahperalatan") or die(mysql_error());
$checkBox = implode(',', $_POST['item']);
$microphones = $_POST['microphones'];
$amplifiers = $_POST['amplifiers'];
$loudspeakers = $_POST['loudspeakers'];
$mixers = $_POST['mixers'];
$catatan = $_POST['catatan'];
if(isset($_POST['submit']))
{
$query="INSERT INTO pasystems (item, microphones, amplifiers, loudspeakers, mixers, catatan) VALUES ('" . $checkBox . "', '$microphones', '$amplifiers', '$loudspeakers', '$mixers', '$catatan')";
mysql_query($query) or die (mysql_error() );
echo "<script type='text/javascript'>alert('Submitted successfully!')</script>";
}
else{
echo "<script type='text/javascript'>alert('Failed!')</script>" . $sql . "<br>" . mysqli_error($conn);
}
?>
以下是我的表格:
<form action="page2.php" method="POST">
<div class="form-group row text-left">
<label for="example-date-input" class="col-2 col-form-label">Nama Peralatan: </label>
<div class="col-10">
<div class="form-group">
<div class="form-row">
<div class="col-md-2">
<div class="form-check text-left">
<label class="form-check-label">
<input class="form-check-input" name="item[]" type="checkbox" value="Microphones">
Microphones
</label>
</div>
</div>
<div class="">
<input class="form-control" type="number" value="0" name="microphones" id="example-number-input">
</div>
</div>
</div>
<div class="form-group">
<div class="form-row">
<div class="col-md-2">
<div class="form-check text-left">
<label class="form-check-label">
<input class="form-check-input" name="item[]" type="checkbox" value="Amplifiers">
Amplifiers
</label>
</div>
</div>
<div class="">
<input class="form-control" type="number" value="0" name="amplifiers" id="example-number-input">
</div>
</div>
</div>
<div class="form-group">
<div class="form-row">
<div class="col-md-2">
<div class="form-check text-left">
<label class="form-check-label">
<input class="form-check-input" name="item[]" type="checkbox" value="Loudspeakers">
Loudspeakers
</label>
</div>
</div>
<div class="">
<input class="form-control" type="number" value="0" name="loudspeakers" id="example-number-input">
</div>
</div>
</div>
<div class="form-group">
<div class="form-row">
<div class="col-md-2">
<div class="form-check text-left">
<label class="form-check-label">
<input class="form-check-input" name="item[]" type="checkbox" value="Mixers">
Mixers
</label>
</div>
</div>
<div class="">
<input class="form-control" type="number" value="0" name="mixers" id="example-number-input">
</div>
</div>
</div>
</div>
</div>
<div class="form-group row text-left">
<label for="exampleTextarea" class="col-2 col-form-label">Catatan: </label>
<div class="col-10">
<textarea class="form-control" name="catatan" id="exampleTextarea" rows="3"></textarea>
</div>
</div>
<center><button type="submit" name="submit" class="btn btn-info">Submit</button></center>
</form>
答案 0 :(得分:5)
发生第一个错误是因为在尝试使用$_POST['item']
之前没有检查是否存在if
。您需要在$_POST
块中移动这些行,或者更好的是,检查$checkBox = implode(',', $_POST['item']);
$microphones = $_POST['microphones'];
$amplifiers = $_POST['amplifiers'];
$loudspeakers = $_POST['loudspeakers'];
$mixers = $_POST['mixers'];
$catatan = $_POST['catatan'];
中要使用的每个元素:
mysql_...
第二个错误是因为您将mysqli_...
函数与mysql_*
函数混合在一起。 You should not be using mysql_...
functions at all。 public string Name => "bob";
函数已过时,deprecated和不安全 - 它们已完全从现代版本的PHP(7.0及更高版本)中删除。请改用MySQLi
或PDO
。