Answer the question
In order to leave comments, you need to log in
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
$conn = connect();
$sql = "INSERT INTO users ( title, body) VALUES(?,?)";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ss", $input, $textarea);
$stmt->execute();
echo "update";
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question