dousi7579 2015-12-31 18:19
浏览 7
已采纳

在名称存在或添加时获取消息


I'am tryng to show a message after you pressed submit with information.

1. This name allready exists, use another
or
2. The name is added!

html

<form action="add.php" method="post">
<input type="text" name="name" id="name" class="form-control" required> 
<input class="btn btn-default" type="submit" value="Submit">
</form>

php

$connectie=mysqli_connect("localhost","usernam","pw","db");

$name = $_POST['name'];
$name = stripslashes($name);
$name = mysql_real_escape_string($name);

$check_if_exists="SELECT * FROM names WHERE name = '$name'";

if($data[0] > 1) {
echo"already exists";
} else {

$newUser="INSERT INTO users (name) values('$name')";
    if (mysqli_query($connectie,$newUser))
    {
        echo "name is registerd";
    }
    else
    {
        echo "error<br/>";
    }

Right now it post the echo on add.php, not on the page where the form is. How do I get it there?

  • 写回答

2条回答 默认 最新

  • dongtang6775 2015-12-31 18:35
    关注

    You can use session / sessionflashdata.

    First, you are not running the select query properly. It should have been:

    $check_if_exists="SELECT * FROM names WHERE name = '$name'";
    
    $count = mysql_num_rows($check_if_exists);
    if($count != 0) { // this means that the name already exists:
    
     $_SESSION['msg']="The name already exists. Please try another name";
      
    
    } else {
      
      //perform the insert query and have the session msg
       $_SESSION['msg']="The name was inserted successfully";
      
    }
      
      header('Location: form.php');
    
    // Now in your form page just above the form, have a div to display this message as:
    
    <div class="msg"> 
      <?php if(isset($_SESSION['msg']))
    
    {
    echo $_SESSION['msg'];
    unset($_SESSION['msg']);
    }
    ?>
    </div>
    
    NOTE: you must start the session on both the pages. Have this code on top of both the pages
    
    <?php
    session_start();
    ?>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法