我知道这里被问了一百万次,但是我没有得到它的生命。我在php页面中有一个复选框,代码如下:
CODE UPDATED BELOW
这是一个简单的开/关开关。它应该将checked或not的值发送到php表单onff-switch.php,以便表单可以使用以下代码发布到MySql表:[UPDATED]
CODE UPDATED BELOW
第一个问题是,如果我使用$ con,我将通过终端收到错误。它告诉我有语法错误,即使我已经用w3schools.com进行了双重检查。
除此之外,当选中该框时,该值应为ON,如果未选中,则值为OFF,并且应该在数据库表中为UPDATEd。
更新:我已将所有元素移动到一个页面,如下所示,但仍然没有发布到数据库:
<?php
$con=mysqli_connect("localhost","USER","PASS","DB");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if (isset($_POST['formSubmit']))
{
// escape variables for security
$scanningvalue = mysqli_real_escape_string($_POST['onoffswitch']);
$sqlon="INSERT INTO configuration (scanning) VALUES ('$scanningvalue')";
$result=mysqli_query($con, $sqlon);
if($scanningvalue=='') $scanningvalue='off';
if($result)
{
echo "The db operation done (1 record added) and switch value ".$scanningvalue;
}
else
{
echo "The db operation error and switch value ".$scanningvalue;
}
}
mysqli_close($con);
?>
<form action="" method="post">
<div class="onoffswitch">
<input type="checkbox"
name="onoffswitch"
class="onoffswitch-checkbox"
id="myonoffswitch"
value="on"
checked
>
<label class="onoffswitch-label" for="myonoffswitch">
<div class="onoffswitch-inner"></div>
<div class="onoffswitch-switch"></div>
</label>
</div>
</form>
答案 0 :(得分:0)
改变这个 -
$scanningonoff = mysqli_real_escape_string($_POST['onoffswitch']);
$sqlon="INSERT INTO configuration (scanning) VALUES ('$scanningon')";
错别字 -
1) $con=mysqli_connect("localhost","config","password","mysql_db"));
^ extra bracket
2) if (isset($_POST['onoffswitch'])) {
{ ^ extra bracket
mysqli_query($con,$sqlon);
}
else {
mysqli_query($con,$sqloff);
}
答案 1 :(得分:0)
对于Db Connection事项,你最后会放置双括号。删除它
$con=mysqli_connect("localhost","config","password","mysql_db");
您可以通过名称而不是通过其值来调用元素,因此它将是&#39; onoffswitch&#39;使用$ _POST调用
检索数据时$scanningonoff = mysqli_real_escape_string($_POST['onoffswitch']);
答案 2 :(得分:0)
试试这个
<?php
$con=mysqli_connect("localhost","config","password","mysql_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if (isset($_POST['formSubmit']))
{
// escape variables for security
$scanningvalue = mysqli_real_escape_string($_POST['onoffswitch']);
$sqlon="INSERT INTO configuration (scanning) VALUES ('$scanningvalue')";
$result=mysqli_query($con,$sqlon);
if($scanningvalue=='') $scanningvalue='off';
if($result)
{
echo "The db operation done (1 record added) and switch value $scanningvalue";
}
else
{
echo "The db operation error and switch value $scanningvalue";
}
}
mysqli_close($con);
?>