-1

I'm new to PHP. I want to display a message that the database is updated after each time I redirect it after entering the data.

$sql = "INSERT INTO incoming (recipt, userid, username, money)
VALUES ('$recipt', '$userid', '$username', '$money')";

if ($conn->query($sql) === TRUE) {
     echo "<script>window.open('incoming2.php','_self')</script>";  
    echo "New record created successfully";

} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}   

} 
maxshuty
  • 9,708
  • 13
  • 64
  • 77
karthik
  • 1,100
  • 9
  • 21
  • Why do you have an extra brace? Also look in to affected rows. – Script47 Aug 17 '15 at 19:23
  • idnt get ..... i want the data to be updated to the database after updating it should return the same page and should show a message that the database is updated – karthik Aug 17 '15 at 19:25
  • `window.open` will replace your current page. Any code after that will not get executed. It will start from the beginning. –  Aug 17 '15 at 19:28
  • Is this all one page or 2 different pages? – RiggsFolly Aug 17 '15 at 19:31

4 Answers4

1

Pass the message to your url:

echo "<script>window.open('incoming2.php?message=New+record+created+successfully','_self')</script>";

Then you can get the message in incoming2.php:

echo urldecode($_GET['message']);

Be careful: sanatize your input!

schellingerht
  • 5,726
  • 2
  • 28
  • 56
1

2 methods
1-you can redirect to any page adding message in get variable and check at that page if that variable is set then display it as message

//redirect to index.php with msg as
header('location:index.php?msg=2 records updated');

//at index page where you want to display message
if(isset($_GET['msg']) && !empty($_GET['msg'])){
    echo '<p class="myMsg">'.$_GET['msg'].'</p>'
}

2- second method is to save the message to session variable and access it at page but you will have to unset that variable as below

 //sending message assuming session_start() is written at to of all pages
 $_SESSION['msg']="2 records updated or what ever your message is";

//where you want to display message
if(isset($_SESSION['msg']) && !empty($_SESSION['msg'])){
    echo '<p class="myMsg">'.$_SESSION['msg'].'</p>'
    unset($_SESSION['msg']);
}
alamnaryab
  • 1,480
  • 3
  • 19
  • 31
0

Use header("Location: incoming2.php"); instead of echoing JS.

Also, check your SQL statement for SQL injection vulnerabilities.

Community
  • 1
  • 1
al'ein
  • 1,711
  • 1
  • 14
  • 21
0

If you are posting the data on the same page you could do the following:

<?php
    if(isset($_REQUEST["submit"])){
        // mySQL code here
        // return either success or failed
        $confirmation="success";
    }

?>

<html>
<head>
    <title>Feedback</title>
</head>
<body>

    <div>
        <?php 
            if(isset($confirmation)){
             echo $confirmation;
            } 
        ?>
    </div>

    <form method="post" action="">
        <input type="text" name="username">
        <input type="password" name="password">
        <input type="submit" name="submit" Value="Submit">
    </form>

</body>
</html>

If you are sending the data to a separate page:

On the receiving page:

<?php
        // mySQL code here
        // return either success or failed
        //redirect to index.php with confirmation as true or false
        header('location:index.php?confirmation=success');

?>

and on the page that you Sent the data from:

<html>
<head>
    <title>Feedback</title>
</head>
<body>

    <div>
        <?php 

            //at index page where you want to display message
            if(isset($_GET['confirmation']) && !empty($_GET['confirmation'])){
                echo $_GET['confirmation'];
            }

        ?>
    </div>

    <form method="post" action="uploaddata.php">
        <input type="text" name="username">
        <input type="password" name="password">
        <input type="submit" name="submit" Value="Submit">
    </form>

</body>
</html>

You can then use CSS3 animations to fade the message in and out for a better user experience :-)

Jono M
  • 91
  • 2
  • 9