是否可以将选定的动态选择列表内容添加到新表中?

时间:2017-03-26 06:18:44

标签: php html mysqli

我有一个名为 'ecc' 的数据库。

数据库有一个名为 'task' 的表格。

表格任务有三个字段,名为 '分配给' '主题''日期' 。< / p>

使用适当的日期数据类型。

'assignto' 字段必须满足所选内容的选定内容。

'subject' 只是一个文本字段。

'date' 字段应包含 'yyyy / mm / dd' 格式化中的日期。

选择列表的内容摘自字段 '客户端' 来自同一 'ecc' 数据库的 'ProjectManager'

我的问题是以下代码: - 没有将选定列表的选定内容插入到 '分配给' 字段中的表'task'中 'subject' 'date'

如果(isset($ _ post('')) ,它会向我显示与 相关的警告。

如果我不使用 if(isset($ _ post('')) ,它会向我显示未声明变量的警告。

警告是: -

1)注意:未定义的索引:第53行的C:\ wamp \ www \ select.php中的$ mysqli

2)注意:未定义的索引:第57行的C:\ wamp \ www \ select.php中的主题

3)注意:未定义的索引:第58行的C:\ wamp \ www \ select.php中的日期

<?php
// Connect db
$mysqli = new mysqli('localhost','root','','ecc');

// check if error
if ($mysqli->connect_error) {
    die('Can not connect to DB error : ('. $mysqli->connect_errno .') '. 
$mysqli->connect_error);
}
echo "Select client";

// MySqli Select Query
$results = $mysqli->query("SELECT ProjectManager FROM client");

echo '<select name="project_manager">';
echo '<option value="">-select project manager-</option>';
while($row = $results->fetch_assoc()) {
    echo '<option 
value="'.$row['ProjectManager'].'">'.$row['ProjectManager'].'</option>';
}  
echo '</select>';

// Frees memory

$results->free();

?>



<html>
<body>
<form action = "select.php" method ="POST">


  Subject:<br>

  <input type="text" name="subject"><br>

  <br>

    <input type = "date" name = "date"><br>

<input type="submit" value="submit">  


</form>
</body>
</html>



<?php

$assignedto = $_POST['$mysqli'];



    $subject = $_POST['subject'];
    $date = $_POST['date'];




$sql="INSERT INTO task (assignedto,
                        subject,
                        date)
                        VALUES('$assignedto','$subject',
                        '$date')";


if(mysqli_query($mysqli,$sql))
{
    echo 'record added';
}

else
{
    echo 'record not added';
}
//header("refresh:02;url=index.html");
?>

3 个答案:

答案 0 :(得分:1)

我改变了你的查询使用预处理语句也使用了mysqli real_escape字符串..这有点帮助你防止sql注入..你应该总是使用它。

    <html>
    <body>
    <form action = "select.php" method ="POST">

    <?php
    error_reporting(-1);
    $mysqli = new mysqli('localhost','root','','ecc');
    if ($mysqli->connect_error) {
        die('Can not connect to DB error : ('. $mysqli->connect_errno .') '.$mysqli->connect_error);
    }
    echo "Select client";
    if($results = $mysqli->query("SELECT ProjectManager FROM client"))
    {?>
    <select name="project_manager">
    <option value="">-select project manager-</option>
    <?php

    while($row = $results->fetch_assoc()) {
    ?><option value="<?php echo $row['ProjectManager'] ?>"><?php echo $row['ProjectManager'] ?></option>
    <?php
    }  
    ?>
    </select>
    <?php $results->free(); }?>
      Subject:<br>

      <input type="text" name="subject"><br>

      <br>

        <input type = "date" name = "date"><br>

    <input type="submit" value="submit">  


    </form>
    </body>
    </html>



    <?php
    if(  isset($_POST['project_manager']) && isset($_POST['subject'])  && isset($_POST['date']) ){
        print_r($_POST);
        $assignedto = $mysqli->real_escape_string($_POST['project_manager']);
        $subject = $mysqli->real_escape_string( $_POST['subject']);
        $date =  $mysqli->real_escape_string($_POST['date']);


        if($stmt = $mysqli->prepare("INSERT INTO task (assignedto,subject,date) VALUES(?,?,?)")){
            $stmt->bind_param('sss',$assignedto,$subject,$date);
            $stmt->execute();
            $rows_affected = $stmt->affected_rows;
            if($rows_affected >0){
               echo "inserted successfully";    
            }elseif($rows_affected === -1){
               echo 'there is an error inserting';    
            }elseif($rows_affected === NULL){
               echo "invalid argument supplied";    
            }
        }    
    }

答案 1 :(得分:0)

在形式上,你只有两个值,一个是日期,另一个是主题,你没有像mysqli这样的东西..

 $assignedto = $_POST['$mysqli']; 

显示警告,因为没有$ mysqli名称。

$subject = $_POST['subject'];
$date = $_POST['date']; 

也会显示错误,因为它只会在您提交表单时发布..所以你必须把它放在里面

if(isset($_POST['date'] && isset($_POST['subject'] && isset($_POST['assingedto'])

以下一行

$sql="INSERT INTO task (assignedto,
                    subject,
                    date)
                    VALUES('$assignedto','$subject',
                    '$date')";
即使您提交的表单只有两个值,

也会出错。你没有分配给...的价值。

答案 2 :(得分:0)

<?php
// Connect db
$mysqli = new mysqli('localhost','root','','ecc');

// check if error
if ($mysqli->connect_error) {
    die('Can not connect to DB error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}
echo "Select client";

// MySqli Select Query
$results = $mysqli->query("SELECT ProjectManager FROM client");

echo '<form action = "select.php" method = "post"><select name="assignedto"></form>';
echo '<option value="">-select project manager-</option>';

while($row = $results->fetch_assoc()) {
    echo '<option value="'.$row['ProjectManager'].'">'.$row['ProjectManager'].'</option>';
}  
echo '</select>';

// Frees memory

$results->free();

?>



<html>
<body>
<form action = "select.php" method ="POST">

<br>
  Subject:<br>

  <input type="text" name="subject"><br>

  <br>
  Date:
    <input type = "date" name = "date" value = "yyyy/mm/dd"><br>

<input type="submit" value="submit">  


</form>
</body>
</html>



<?php

    $assignedto = $_POST['assignedto'];
    if(isset($_POST['date']) && isset($_POST['subject']) && isset($_POST['assingedto'])){


    $subject = $_POST['subject'];
    $date = $_POST['date'];




$sql="INSERT INTO task (assignedto,
                        subject,
                        date)
                        VALUES('$assignedto','$subject',
                        '$date')";


if(mysqli_query($mysqli,$sql))
{
    echo 'record added';
}

else
{
    echo 'record not added';
}
//header("refresh:02;url=index.html");
    }
?>
@v Sugumar我和所有的变化都看了一下 但它仍然显示错误

注意:未定义的索引:在第54行的C:\ wamp \ www \ select.php中分配给