M
M
MRcracker2020-04-19 22:18:18
PHP
MRcracker, 2020-04-19 22:18:18

How to create data in a table?

There is a table with fields id, title - varchar (100), body - text
, you need to send data to it through the form. Where did I go wrong?

define('SERVERNAME', 'localhost');
define('USERNAME', 'root');
define('PASSWORD', '');
define('DBNAME', 'go');

function connect(){

  $conn = mysqli_connect(SERVERNAME, USERNAME, PASSWORD, DBNAME);
  mysqli_set_charset($conn, "utf8");
  if(!$conn) {
    die('error'.mysqli_connect_error());
  }
  return $conn;
}

function select($conn){

$sql = "SELECT * FROM users";
$result = mysqli_query($conn, $sql);
$a = array();

if(mysqli_num_rows($result) > 0) {
  while($row = mysqli_fetch_assoc($result)) {
    $a[] = $row;
  }
} 

return $a;

}

function close($conn) {
  mysqli_close($conn);
}

$input = $_POST['input'];
$textarea = $_POST['textarea'];

$conn = connect();
$sql = "INSERT INTO users ( title, body)
VALUES($input, '$textarea')";

if(mysqli_query($conn, $sql)) {
  echo "update";
} else {
  echo "error";
}

echo '<pre>';
print_r($a);
echo '</pre>';


<form action="index.php" method="POST" enctype="multipart/form-data">
  <p>title</p><input type="text" name="input"><br>
  <p>body</p><textarea name="textarea"></textarea> <br>
  <input type="submit" name="submit">
</form>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
FanatPHP, 2020-04-19
@MRcracker

$conn = connect();
$sql = "INSERT INTO users ( title, body) VALUES(?,?)";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ss", $input, $textarea);
$stmt->execute();
echo "update";

V
Viktor Yanyshev, 2020-04-19
@villiwalla

Pass connection details

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question