你如何编码PHP来检查清空,如果没有,然后运行SQL查询?(How do you code php to check for empties and if not then run the sql query?)

这是我的,它不工作。 我需要检查Your_Location.php中的字段是否为空。 如果是,则抛出错误。 如果不; 按照以下方式运行查询。 如果我输入// if(!mysqli_query($ conn,$ sqlinsert))它会工作

<?php //session_start(); include 'dbConfig.php'; include 'Your_Location.php'; $childfirst = $_POST['element_1']; $childlast = $_POST['element_2']; $childdobyear = $_POST['element_3_3']; $childdobmon = $_POST['element_3_1']; $childdobday = $_POST['element_3_2']; $childbaptize = $_POST['element_4']; $childrelationship = $_POST['inputrelation']; $childdob = "$childdobyear-$childdobmon-$childdobday"; $sqlinsert="INSERT INTO memchild (ID, FirstName, LastName, DOB, Baptize, Relationship) VALUES ('$getid2','$childfirst','$childlast','$childdob','$childbaptize','$childrelationship')"; //Build arrays of fields $required = array('element_1', 'element_2', 'element_3_3', 'element_3_2', 'element_3_1', 'elelment_4', 'inputrelation'); //Loop to check for empties $error = false; foreach($required as $fields) { if(empty($_POST[$fields])){ $error = true; } } if($error){ Sleep(3) ?> <script> document.getElementById('li_9').innerHTML = '* $childfirst Make sure the fields are not empty.'; </script> <?php exit(); }Else{ mysqli_query($conn,$sqlinsert) ?> <script> document.getElementById('li_9').innerHTML = '$childfirst $childlast has been added.'; </script> <?php sleep(3); echo "<meta http-equiv='refresh' content='0'>"; } ?>

This is what I have and it's not working. I need to check if the fields in Your_Location.php is empty. If it is, throw an error. If not; run the query as follow. It would work if I throw in //if (!mysqli_query($conn,$sqlinsert))

<?php //session_start(); include 'dbConfig.php'; include 'Your_Location.php'; $childfirst = $_POST['element_1']; $childlast = $_POST['element_2']; $childdobyear = $_POST['element_3_3']; $childdobmon = $_POST['element_3_1']; $childdobday = $_POST['element_3_2']; $childbaptize = $_POST['element_4']; $childrelationship = $_POST['inputrelation']; $childdob = "$childdobyear-$childdobmon-$childdobday"; $sqlinsert="INSERT INTO memchild (ID, FirstName, LastName, DOB, Baptize, Relationship) VALUES ('$getid2','$childfirst','$childlast','$childdob','$childbaptize','$childrelationship')"; //Build arrays of fields $required = array('element_1', 'element_2', 'element_3_3', 'element_3_2', 'element_3_1', 'elelment_4', 'inputrelation'); //Loop to check for empties $error = false; foreach($required as $fields) { if(empty($_POST[$fields])){ $error = true; } } if($error){ Sleep(3) ?> <script> document.getElementById('li_9').innerHTML = '* $childfirst Make sure the fields are not empty.'; </script> <?php exit(); }Else{ mysqli_query($conn,$sqlinsert) ?> <script> document.getElementById('li_9').innerHTML = '$childfirst $childlast has been added.'; </script> <?php sleep(3); echo "<meta http-equiv='refresh' content='0'>"; } ?>

最满意答案

首先尝试检查键是否实际存在。

$childfirst = isset($_POST['element_1'])? $_POST['element_1'] : null;

其次,你可以检查什么字段没有填写:

$errorMessage = ''; foreach($required as $key) { if(empty($_POST[$key])){ $error = true; // break; // Uncomment if you want to exit the loop if one field is not set // $errorMessage .= $key . ' is not filled in'; // Uncomment if you want to add message for every missing key } }

对于你的脚本,你试图将PHP值与javascript结合起来,你需要实际回显或打印这样的值:

<script> document.getElementById('li_9').innerHTML += "* <?php echo $childfirst;?> Make sure the fields are not empty."; </script>

还有第二个

<script> document.getElementById('li_9').innerHTML += "<?php echo $childFirst . ' ' . $childLast;?> has been added."; </script>

First try to check if the keys actually exist.

$childfirst = isset($_POST['element_1'])? $_POST['element_1'] : null;

secondly, you can check what field is not filled in:

$errorMessage = ''; foreach($required as $key) { if(empty($_POST[$key])){ $error = true; // break; // Uncomment if you want to exit the loop if one field is not set // $errorMessage .= $key . ' is not filled in'; // Uncomment if you want to add message for every missing key } }

for your scripts, you are trying to combine PHP value with javascript, you need to actually echo or print the value like this:

<script> document.getElementById('li_9').innerHTML += "* <?php echo $childfirst;?> Make sure the fields are not empty."; </script>

and also for the second one

<script> document.getElementById('li_9').innerHTML += "<?php echo $childFirst . ' ' . $childLast;?> has been added."; </script>

更多推荐