使用php

时间:2019-12-02 05:54:39

标签: php html

我正在开发一个网页,当用户单击带有HTML和PHP的“提交”按钮时,该表单将从表单中获取用户的输入并将其添加到下表中。现在该程序可以工作了,但是当用户单击“提交”按钮时,该列表就被清除了,我该如何使用它了,因此使用PHP在下面添加到表中的上一个项目仍然是我的代码。

<?php

$failure = False;

$conn = mysqli_connect('localhost','uobasi','blind123','mealprep');
    if(!$conn){
        echo 'Connection error: '.msqli_connect_error();
        $failure = True;
    }
$user_email = 'obasiu17@students.ecu.edu';

if(!$failure) {
    if(isset($_POST['submit'])) {
        $recipe = htmlspecialchars($_POST['task']);
        $sql = "insert into weeklyrecipe(username, recipe) values ('$user_email', '$recipe')";

        mysqli_query($conn, $sql);

    }
}


?>


<html>
<head>
    <title> Weekly Recipes </title>
    <link rel="stylesheet" type="text/css" href="weeklyCalendarRepstyles.css">

</head>
<body>

  <div class="heading">
    <h2>Weekly Recipes</h2>
  </div>

  <form id="recipe-form" method="post">
    <input type="text" name="task" id="task" class="task_input">
    <select id="day" name="day">
      <option value="0">Sunday</option>
      <option value="1">Monday</option>
      <option value="2">Tuesday</option>
      <option value="3">Wednesday</option>
      <option value="4">Thursday</option>
      <option value="5">Friday</option>
      <option value="6">Saturday</option>
    </select>
    <button type="submit" class="task_btn" name="submit">Add Recipe</button>

  </form>

  <div>
    <table id="t01">
      <tr>
        <th>Sunday</th>
        <th>Monday</th>
        <th>Tuesday</th>
        <th>Wednesday</th>
        <th>Thursday</th>
        <th>Friday</th>
        <th>Saturday</th>
      </tr>
      <tr>
        <td><?php if(isset($_POST['day']) && $_POST['day']==0){echo $_POST['task'];;}else{ echo "-";} ?></td>
        <td><?php if(isset($_POST['day']) && $_POST['day']==1){echo $_POST['task'];;}else{ echo "-";} ?></td>
        <td><?php if(isset($_POST['day']) && $_POST['day']==2){echo $_POST['task'];;}else{ echo "-";} ?></td>
        <td><?php if(isset($_POST['day']) && $_POST['day']==3){echo $_POST['task'];;}else{ echo "-";} ?></td>
        <td><?php if(isset($_POST['day']) && $_POST['day']==4){echo $_POST['task'];;}else{ echo "-";} ?></td>
        <td><?php if(isset($_POST['day']) && $_POST['day']==5){echo $_POST['task'];;}else{ echo "-";} ?></td>
        <td><?php if(isset($_POST['day']) && $_POST['day']==6){echo $_POST['task'];;}else{ echo "-";} ?></td>
      </tr>
    </table>
  </div>


</body>


</html>

2 个答案:

答案 0 :(得分:2)

使用 PHP会话,您可以获得上一个记录。

<?php
    $failure = False;
    session_start();
    $conn = mysqli_connect('localhost','root','Passw0rd','mealprep');
    if(!$conn){
        echo 'Connection error: '.mysqli_connect_error();
        $failure = True;
    }
    $user_email = 'obasiu17@students.ecu.edu';
    if(!$failure) {
        if(isset($_POST['submit'])) {
          if(isset($_SESSION['weekly_recipes']) && isset($_POST['day']) && $_POST['day']!=''){
            // check already recipes or not
            $is_recipes_available=false;
            $weekly_recipes=array();
          foreach ($_SESSION['weekly_recipes'] as $k => $data) {
                    if($_POST['day']==$data['day']){
                 $is_recipes_available=true;
                 $data['task']=$_POST['task'];
                   }    
                   array_push($weekly_recipes, $data);
              }
            if($is_recipes_available==false){
                $a=array();
                $a[0]['day']=$_POST['day'];
                $a[0]['task']=$_POST['task'];
                $_SESSION['weekly_recipes']=array_merge($_SESSION['weekly_recipes'], $a);
            }
            else{
                 $_SESSION['weekly_recipes']=$weekly_recipes;
            }
            }
        else{
            if(isset($_POST['day']) && $_POST['day']!=''){
                $a=array();
                $a[0]['day']=$_POST['day'];
                $a[0]['task']=$_POST['task'];
                $_SESSION['weekly_recipes']=$a;
            }
          }
          $recipe = htmlspecialchars($_POST['task']);
          unset($_POST['day']);
          unset($_POST['task']);
          $sql = "insert into weeklyrecipe(username, recipe) values ('$user_email', '$recipe')";
          mysqli_query($conn, $sql);
        }
    }
    ?>
    <html>
    <head>
        <title> Weekly Recipes </title>
        <link rel="stylesheet" type="text/css" href="weeklyCalendarRepstyles.css">

    </head>
    <body>

      <div class="heading">
        <h2>Weekly Recipes</h2>
      </div>

      <form id="recipe-form" method="post">
        <input type="text" name="task" id="task" class="task_input">
        <select id="day" name="day">
          <option value="0">Sunday</option>
          <option value="1">Monday</option>
          <option value="2">Tuesday</option>
          <option value="3">Wednesday</option>
          <option value="4">Thursday</option>
          <option value="5">Friday</option>
          <option value="6">Saturday</option>
        </select>
        <button type="submit" class="task_btn" name="submit">Add Recipe</button>

      </form>

      <div>
        <table id="t01">
          <tr>
            <th>Sunday</th>
            <th>Monday</th>
            <th>Tuesday</th>
            <th>Wednesday</th>
            <th>Thursday</th>
            <th>Friday</th>
            <th>Saturday</th>
          </tr>
          <?php if(isset($_SESSION['weekly_recipes']) && count($_SESSION['weekly_recipes'])>0){
          foreach ($_SESSION['weekly_recipes'] as $k => $data) {?>
            <tr>
              <td><?php if($data['day']==0){echo $data['task'];}else{ echo "-";} ?></td>
              <td><?php if($data['day']==1){echo $data['task'];}else{ echo "-";} ?></td>
              <td><?php if($data['day']==2){echo $data['task'];}else{ echo "-";} ?></td>
              <td><?php if($data['day']==3){echo $data['task'];}else{ echo "-";} ?></td>
              <td><?php if($data['day']==4){echo $data['task'];}else{ echo "-";} ?></td>
              <td><?php if($data['day']==5){echo $data['task'];}else{ echo "-";} ?></td>
              <td><?php if($data['day']==6){echo $data['task'];}else{ echo "-";} ?></td>
            </tr>
          <?php }
        }
        else{?>
        <td><tr colspan="7">No Record Found</tr></td>
        <?php }?>     
        </table>
      </div>
    </body>
    </html>

答案 1 :(得分:-1)

添加函数+ mysqli_real_escape_string

function insert_weeklyrecipe($weeklyrecipe) {

    $sql = "INSERT INTO weeklyrecipe ";
    $sql .= "(username, recipe) ";
    $sql .= "VALUES (";
    $sql .= "'" . db_escape($conn, $weeklyrecipe['username']) . "',";
    $sql .= "'" . db_escape($conn, $weeklyrecipe['recipe']) . "'";
    $sql .= ")";  

     $result = mysqli_query($conn, $sql);
    // For INSERT statements, $result is true/false
    if($result) {
      return true;
    } else {
      // INSERT failed
      echo mysqli_error($conn);
      exit;
    }
  }


function db_escape($conn, $string) {
    return mysqli_real_escape_string($conn, $string);
  }