I am having trouble with my insert script. I can't seem to get my script to stop inserting empty data into mysql. I tried using !empty function. But yet it still submits it into the database. Here's my code:
<?php
// Connect to MySQL
$mysqli = new mysqli( 'localhost', 'xxx', 'xxx', 'xxx' );
if ( $mysqli->connect_error ) {
die( 'Connect Error: ' . $mysqli->connect_errno . ': ' . $mysqli->connect_error );
}
if (!empty($_POST)) {
// Insert into posts
$sql = "INSERT INTO posts (title, postdata ) VALUES ( '{$mysqli->real_escape_string($_POST['title'])}', '{$mysqli->real_escape_string($_POST['data'])}' )";
$insert = $mysqli->query($sql);
// Print response from MySQL
if ( $insert ) {
echo "Success! Row ID: {$mysqli->insert_id}";
} else {
die("Error: {$mysqli->errno} : {$mysqli->error}");
}
// Close our connection
$mysqli->close();
}
?>
<form action="" method="POST">
<br>
<br>
Title:
<br>
<input type="text" name="title">
<br>
<br>
Post:
<br>
<textarea name="data" id="data"></textarea>
<br>
<br>
<input type="submit" name="submit">
</form>