2021年1月4日星期一

PHP MySQL, Can't figure out how to create this program

I have to create this program for school, and I can not figure out what to do. I asked the teacher and got zero direction on where to go next. (basically got the "google it" response) The assignment is this: Create a PHP class named "User" with the following private fields:

name, birthdate in yyyy/mm/dd format, age, and department. In the class, include getter and setter methods to get and set the values of those variables.

Author a data entry webform using HTML text input boxes and a submit button. When the user clicks on the submit button, a "User" class is instantiated and the new User object's fields are populated. The HTML input boxes correspond to the field in the "User" class. Add an "Insert" button that allows the user to insert the form data to a MySQL table. Add a "Display" button the displays the contents of your MySQL table on the web form when the end-user clicks on the button.

This is what I have thus far: PHP:

<?PHP     if ($_POST) {    //create a connection  $conn = mysqli_connect('localhost', 'xxxxxx_hidden', 'xxxxxxxxxx_hidden');  if (! $conn) {      die("Connection failed" . mysqli_connect_error());  }  else {      mysqli_select_db($conn, 'junkdb');  }  class USER {  private $userName;  private $userBirthday;  private $userAge;  private $userDepart;    function setter($userName,$userBirth,$userAge,$userDepart) {      $this->$_POST['Name'] = $userName;      $this->$_POST['Birth'] = $userBirth;      $this->$_POST['Age'] = $userAge;      $this->$_POST['Depart'] = $userDepart;  }  }  $query = "INSERT into junkdb (username, birthday, age, department) VALUES('?','?','?','?')";    $stmt = mysqli_prepare($conn, $query);  $stmt->bind_param('ssss', $_POST['Name'], $_POST['Birth'], $_POST['Age'], $_POST['Depart']);  $stmt->execute();    if(mysqli_query($conn,$query)){      echo "<p>Record added.</p>";  } else {      echo "<p>Not added...</p>";  }  }      mysqli_close($conn);    ?>  

HTML:

<!DOCTYPE HTML>  <html>  <head>  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">  </head>      <body>          <div class="container">          <div class="row">          <div class="col-md-8">              <h1>Portfolio Project ITS345</h1>                  <form name="contact-form" action="insert.php" method="POST" id="contact-form">                  <div class="form-group">                      <label for="Name">Name</label>                      <input type="text" class="form-control" name="Name" placeholder="Name" required>                  </div>                  <div class="form-group">                      <label for="Birthday">Birthday</label>                      <input type="text" class="form-control" name="Birth" placeholder="YYYY/MM/DD"    required>                  </div>                  <div class="form-group">                      <label for="Age">Age</label>                      <input type="text" class="form-control" name="Age" placeholder="Your Age"    required>                  </div>                  <div class="form-group">                      <label for="Depart">Department</label>                      <input type="text" class="form-control" name="Depart" placeholder="Your    Department" required>                  </div>                  <button type="submit" class="btn btn-primary" name="submit" value="Submit"    id="submit_form">Submit</button>                  </form>      </div>      </div>      </div>   </body>   </html>  

I keep getting the error: Warning: mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement

Any assistance at completing this program would be insanely helpful. I just don't know what to do.

https://stackoverflow.com/questions/65571789/php-mysql-cant-figure-out-how-to-create-this-program January 05, 2021 at 08:45AM

没有评论:

发表评论