Jump to content

Form/DB table connection


Mekaboo
 Share

Recommended Posts

Hey😊

Ive added this to my site: https://www.w3schools.com/php/php_form_complete.asp   and edited it to look like this:
 

 <?php
// define variables and set to empty values
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
 
  if (empty($_POST["comment"])) {
    $comment = "";
  } else {
    $comment = test_input($_POST["comment"]);
  }
}

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
?>

It works great but trying to connect it to DB tables because when I test post the comments don't change. Want to store and receive so words differ per post:

Comment DB

Id

commentid

comment

 

Thank ya🧡

 

Link to comment
Share on other sites

@dsonesuk

Here is my code..can you show me where to edit this so it can work:

 <?php
    $servername = "db5004526329.hosting-data.io";
$username = "dbu1026002";
$password = "meka%1981";
$database = "dbs3781610";
// Create connection
$conn = new mysqli($servername, $username, $password,$database);

// Check connection
if (mysqli_connect_error()) {
  die("Database connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
    
// define variables and set to empty values
$name = $email = $gender = $comment = $website = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  $comment = test_input($_POST["commentid"]);

}

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
?>

Here is my while loop which is already connected to another to another. Tried to do a multi query and that didnt work.

<?php
 require_once "db.php";
   $sql = "SELECT imageId FROM output_images ORDER BY imageId DESC";
    $result = mysqli_query($conn, $sql);
 
//  Loop thru comments and display all of them
while($row = mysqli_fetch_array($result)) {
    printf("%s (%s)\n", $row["username"], $row["comment"],$row["imageId"]);

 ?>   

  

Link to comment
Share on other sites

THIS

$sql = "SELECT imageId FROM output_images ORDER BY imageId DESC";

Will only allow you to get and show data from imageId column.

To show comment data and the rest you would need to add them individually

$sql = "SELECT imageId, username, comment FROM output_images ORDER BY imageId DESC";

OR list all using universal selector *

$sql = "SELECT * FROM output_images ORDER BY imageId DESC";

 

Link to comment
Share on other sites

When that is sorted! This

printf("%s (%s)\n", $row["username"], $row["comment"],$row["imageId"]);

Should show data related to sql statement of column listing and table used.

But! you are listing 3 columns and only providing 2 formatting types https://www.w3schools.com/PHP/func_string_printf.asp

printf("%s (%s)\n", $row["username"], $row["comment"],$row["imageId"]);

For the number of column used there should be the same number of formatting types in same order, within text

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...