dongyongmin5711 2018-02-24 10:51 采纳率: 100%
浏览 36
已采纳

PHP:在OOP上使用POST并存储在MySql中

Ok im kinda new to using oop on php, So this what im tryna do. getting the value of the textbox putting them in an array then storing them in database but the problem is it doesnt store the data

<?php
require ('employmentfunc.php');
?>
<html>
<head>
</head>
<body>
<?php 
$arr = array(isset ($_POST['company']), isset ($_POST['position']));

if (isset($_POST['ok']))
{
    $a = new empfunc();
    $a->SaveA($arr);
}
?>
<form name="emp" action="employmentform.php" method="POST">

<TD><input type="text" name="company">
<td><input type="text" name="position">
<input type="submit" class="button1" name="ok">
</form>
</body>
</html>



<?php
class empfunc{

function saveA($array){
    $sqlconnect = mysqli_connect("localhost", "root","","employment");
    $sqlquery = "INSERT INTO info(`Company`, `Position`) VALUES ('$array[0]', '$array[1]')";
    mysqli_query ($sqlconnect, $sqlquery);
}
}
?>
  • 写回答

1条回答 默认 最新

  • dro44817 2018-02-24 11:14
    关注

    Your problem is the way you are building the $arr variable. You are storing boolean results from the isset function calls instead of storing the data itself. I would rewrite that part of the code using a few more lines to make it more readable. Since you are storing the result of isset your values stored in the database will be either 0 or 1. Instead, either do not use the isset function or else change them to a ternary operator to store some default data if they are not set:

    $arr = array(
        isset ($_POST['company']) ? $_POST['company'] : 'No Company', 
        isset ($_POST['position']) ? $_POST['position'] : 'No Position'
    );
    

    or if you just want to skip the isset validation:

    $arr = array($_POST['company'], $_POST['position']);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?