PHP is not able to call a function properly.
Calling like this does not display an error message and
Empty data will be displayed.
If there is a value, it is correct to send it like this, but
https://i.stack.imgur.com/1SiBK.png
I want to display an error message when there is no value, but it is normally registered as empty data.
https://i.stack.imgur.com/IQyKc.png
By the way, the functions I want to call are check_name() and check_comment().
How can I call them?
Please let me know.
source code
<?php
define('DB_HOST', '); // Hostname or IP address of the database
define('DB_USER', '); // MySQL username
define('DB_PASSWD', '); // MySQL password
define('DB_NAME', '); // database name
define('HTML_CHARACTER_SET', 'UTF-8');
define('DB_CHARACTER_SET', 'UTF8');
date_default_timezone_set('Asia/Tokyo');
$errors=array();
$insert_datas=[];
$select_datas=[];
// db connection
$link = get_db_connect();
$insert_datas=insert_sql($link);
$select_datas=select_sql($link);
close_db_connect($link);
//entity_assoc_array($datas);
function get_db_connect(){
// Get Connection
if(!$link=mysqli_connect(DB_HOST, DB_USER, DB_PASSWD, DB_NAME)){
die('error:'.mysqli_connect_error());
}
// Character Code Set
mysqli_set_charset($link,DB_CHARACTER_SET);
return$link;
}
function get_as_array($link,$sql){
// return sequence
$data=[];
// execute a query
if($result=mysqli_query($link,$sql))}
if(mysqli_num_rows($result)>0){
// take out one by one
while($row=mysqli_fetch_assoc($result)){
$data[] = $row;
}
}
// Open result set
mysqli_free_result($result);
}
return$data;
}
function insert_sql($link){
if(isset($_POST['send'])===true){
$error1 = check_name();
$error2 = check_comment();
if($error1==="||$error2===""){
$name = $_POST ['name'];
$comment=$_POST ['comment'];
$date=date("Y-m-d H:i:s");
$sql = 'INSERT INTO comment_table(name, comment, date)VALUES(\'.$name.'\', \'.$comment.'\', \'.$date.'\')';
$result=mysqli_query($link,$sql);
return$result;
}
}
}
function select_sql($link){
$sql = 'SELECT name, comment, date FROM comment_table ORDER BY date DESC';
return get_as_array($link,$sql);
}
function close_db_connect($link){
mysqli_close($link);
}
function check_name(){
if(mb_strlen($_POST['name'])===""){
$errors['name'] = 'Please enter a name';
} elseif(mb_strlen($_POST['name'])>20){
$errors['name'] = 'Name must be up to 20 characters long';
} else{
$errors=";
}
return$ errors;
}
function check_comment(){
if(mb_strlen($_POST['comment'])===""){
$errors['comment'] = 'Please enter a word';
} elseif(mb_strlen($_POST['comment'])>100){
$errors['comment'] = 'Please enter up to 100 characters per word';
} else{
$errors=";
}
return$ errors;
}
/* functionality_str($str){
return html specialchars($str,ENT_QUOTES,HTML_CHARACTER_SET);
}
functionality_assoc_array($assoc_array){
$assoc_array=[];
foreach($asoc_array as$key=>$value){
foreach($value as$keys=>$values){
$assoc_array[$key][$keys]=entity_str($values);
}
}
return$assoc_array;
}*/
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<metacharset="UTF-8">
<title> </title>
</head>
<body>
<h1>One word bulletin board</h1>
<form method="post">
<?php if(count($errors)>0){?>
<ul>
<?php foreach($errors as$value){?>
<li><?phpechohtmlspecialchars($value,ENT_QUOTES,'UTF-8');?></li>
<?php}?>
</ul>
<?php}?>
<p>Name:
<input type="text" name="name">
One word:
<input type="text" name="comment" size="60">
<input type="submit" name="send" value="send">
</p>
</form>
<ul>
<?php if(!empty($select_datas)){?>
<?php foreach(array)$select_datasas$hitokoto){?>
<li>
<?php echo htmlspecialchars($hitokoto['name'], ENT_QUOTES, 'UTF-8');?>
<?php echo htmlspecialchars($hitokoto['comment'], ENT_QUOTES, 'UTF-8');?>
<?php echo htmlspecialchars($hitokoto['date'], ENT_QUOTES, 'UTF-8');?>
</li>
<?php}?>
<?php}?>
</ul>
</body>
</html>
The mb_strlen conditional statement below is not working as designed.
if(mb_strlen($_POST['name'])===""){
If you look at the mb_strlen documentation, you will see
mb_strlen(string$string, string | null$encoding=null): int
Therefore, the return value must return int.
However, since the condition is ==="
, we are comparing empty characters with int, so it is unlikely that this condition will be met.
If you want to compare the length of a multi-byte character to zero,
if(mb_strlen($_POST['name'])===0){
If you simply want to check if it's empty, you can do the following.
if($_POST['name']===""){
© 2023 OneMinuteCode. All rights reserved.