Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PLEASE I NEED HELP! so i need to do a form that writes into my a table in my dat

ID: 3757647 • Letter: P

Question

PLEASE I NEED HELP! so i need to do a form that writes into my a table in my database and at the same time display the information submitted in the same page. for some reason it wont write into my table and it doesnt display any information on the same page. table name (State_Address)

I NEED SOMEONE TO CHECK MY CODE, NOT DO THE TABLE.

<?php
error_reporting(0);
require 'dbb/connect1.php';
require 'function/securities.php';

$records = array();

if(!empty($_POST)){
   if(isset($_POST['first_name'], $_POST['last_name'], $_POST['state'], $_POST['email'], $_POST['nationality'])){
      
       $first_name = trim($_POST['first_name']);
       $last_name = trim($_POST['last_name']);
       $state = trim($_POST['state']);
       $email = trim($_POST['email']);
       $nationality = trim($_POST['nationality']);
  
   if(!empty($first_name) && !empty($last_name) && !empty($state) && !empty($email) && !empty($nationality)){
       $insert = $db->prepare("INSERT INTO State_Address (first_name, last_name, state, email, nationality) VALUES (?, ?, ?, ?, ?)");
       $insert->bind_param('sssss', $first_name, $last_name, $state, $email, $nationality);
      
      
       if($insert->execute()){
           header('Location: uno.php');
           die();
       }
   }
      
   }
}

if($results = $db->query("SELECT * FROM State_Address")){
   if($results->num_rows){
       while($row = $results->fetch_object()){
           $records[] = $row;
       }
       $results->free();
   }
}
?>

<!DOCTYPE html>
<html>
   <head>
       <title>People</title>
   </head>
   <body>
       <h3>People</h3>
      
       <?php
           if(!count($records)) {
           echo 'no records';
           } else {
       ?>
       <table>
           <thead>
               <tr>
                   <th>First name</th>
                   <th>Last name</th>
                   <th>State</th>
                   <th>Email</th>
                   <th>Nationality</th>
               </tr>
           </thead>
           <tbody>
           <?php
           foreach($records as $r){
           ?>
               <tr>
                   <td><?php echo escape($r->first_name); ?></td>
                   <td><?php echo escape($r->last_name); ?></td>
                   <td><?php echo escape($r->state); ?></td>
                   <td><?php echo escape($r->email); ?></td>
                   <td><?php echo escape($r->nationality); ?></td>
               </tr>
               <?php
               }
               ?>
           </tbody>
       </table>
           <?php
           }
           ?>
       <hr>
      
       <form action="" method="post">
           <div class="field">
               <label for="first_name">Firts name</label>
               <input type="text" name"first_name" id"first_name" autocomplete="off">
           </div>
           <div class="field">
               <label for="last_name">Last name</label>
               <input type="text" name"last_name" id"last_name" autocomplete="off">
           </div>
           <div class="field">
               <label for="state">State</label>
               <input type="text" name"state" id"state" autocomplete="off">
           </div>
           <div class="field">
               <label for="email">Email</label>
               <input type="text" name"email" id"email" autocomplete="off">
           </div>
           <div class="field">
               <label for="nationality">Nationality</label>
               <input type="text" name"nationality" id"nationality" autocomplete="off">
           </div>
          
           <input type="submit" value="Insert">
       </form>
      
   </body>
</html>

CONNECT FILE

<?php

$db=new mysqli('127.0.0.1','mvelazcuez','PASSWORD','mvelazcu_People_email_address');
if($db->connect_errno){
   die('sorry, we are having some problems.');
}
?>

SECURITY FILE

<?php
function escape($string){
   return htmlentities(trim($string), ENT_QUOTES, 'UTF-8');
}

Explanation / Answer

Create a table guestbook in MYSQL
*************************************
mysql> create databse guestbook;
mysql> use guestbook;

CREATE TABLE IF NOT EXISTS `guestbook` (
`name` varchar(40) DEFAULT NULL,
`location` varchar(40) DEFAULT NULL,
`email` varchar(40) DEFAULT NULL,
`url` varchar(80) DEFAULT NULL,
`comments` text
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT COMMAND
=================
INSERT INTO `guestbook` (`name`, `location`, `email`, `url`, `comments`) VALUES
('ssh', 'seet', 'GTA@gmail.com', 'http://aaa.com', 'hi');
====================
Create the following php files
1)dbconnect.php
2) create_entry.php
3)sign.php
4)view.php
code for the above files
------------------------------

1)dbconnect.php

<html>
<body>
<?php
mysql_connect("localhost", "id","password") or
die ("Could not connect to database");
mysql_select_db("guestbook") or
die ("Could not select database");
?>
</body>
</html>

2)create_entry.php

<html>
<body>
<?php
include("dbconnect.php");
$submit = "Sign!";
$name=$_POST['name'];
$location=$_POST['location'];
$email=$_POST['email'];
$url=$_POST['URL'];
$comments=$_POST['comments'];
if ($submit == "Sign!")
{
$query = "insert into guestbook
(name,location,email,url,comments) values
('$name','$location','$email','$url','$comments')";
mysql_query($query) or die (mysql_error());
?>
<h2>Thanks!</h2>
<h2> <a href="view.php">View My Guest Book!</a></h2>
<?php
}
else
{
include("sign.php");
}
?>
</body>
</html>

3)sign.php

<html>
<body>
<h2>Sign my Guest Book!</h2>
<form method=post action="create_entry.php">
<b>Name :<t></t></b>
<input type=text size=40 name=name>
<br>
<b>Location :</b>
<input type=text size=40 name=location>
<br>
<b>Email :<t></t></b>
<input type=text size=40 name=email>
<br>
<B>Home page URL:</B>
<INPUT TYPE=TEXT SIZE=40 NAME=URL>
<BR>
<b>comments :<t></t></b>
<textarea name=comments cols=40 rows=10 wrap=virtual></textarea>
<br>
<input type=submit name=submit value="Sign!">
<input type=reset name=reset value="Start Over">
</form>
</body>
</html>
4)view.php

<html>
<body>
<?php include("dbconnect.php"); ?>
<h2>View My Guest Book!</h2>
<?php
$result = mysql_query ("select * from guestbook") or
die (mysql_error());
while ($row = mysql_fetch_array($result))
{
echo "<b>Name:</b>";
echo $row["name"];
echo "<br> ";
echo "<b>Location:</b>";
echo $row["location"];
echo "<br> ";
echo "<b>Email:</b>";
echo $row["email"];
echo "<br> ";
echo "<b>URL:</b>";
echo $row["url"];
echo "<br> ";
echo "<b>Comments:</b>";
echo $row["comments"];
echo "<br> ";
echo "<br> ";
echo "<br> ";
}
mysql_free_result($result);
?>
<h2> <a href="sign.php">Sign My Guest Book!</a></h2>
<body>
</html>

Save all files and run in localhost
sign.php

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote