duanhui1185 2015-03-13 19:00
浏览 55

如何在提交sql语句后重新加载页面

I am using the following code to try and insert data into my database. The data works fine and i do this using a form. The form has a submit button that i will but the code in bellow the following code. I am using localhost to run my web application.

 try {
   $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
   $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
   $sql = "INSERT INTO PRODUCTS (P_Name, P_Description, P_Price) VALUES ('$_POST[Name]', '$_POST[description]', '$_POST[Price]' ) ";
   $conn->exec($sql);
   echo "New record created successfully";

 }
 catch(PDOException $e)
 {
   echo $sql . "<br>" . $e->getMessage();
 }

 $conn = null;
 ?>

the submit button:

<div id="theSubmit">
<input type="submit" name="submit">
</div>

At the moment when i click submit i get taken to a page display the text New record created successfully. However I would like it to reload the page.

I am aware of the javascript location.reload(); is this what I have to use, if so where?

  • 写回答

3条回答 默认 最新

  • douxian1939 2015-03-13 19:01
    关注

    You should reload after execution of your sql like this.

    $_SESSION['msg'] = "New record created successfully";
    header("Location: $url"); // tell the client to load $url
    exit(); // stop further execution of the current script
    

    the $msg should be set via session to be able to show the msg after reload. If you do it like that it is no problem if the user klicks the back button or do a page reload.

    After reload do:

    if(isset($_SESSION['msg'])) {
        echo $_SESSION['msg'];
        unset($_SESSION['msg']);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line