0

I'm pretty new to this, so any help would be appreciated. I have successfully inserted data into a database. But how can i echo the data inserted into the database out into a form field. I have tried the value="<?php echo [variable here]; ?>". it does work all i get is

Notice: Undefined variable: c_fname in /Applications/MAMP/htdocs/PhpProject2/customer/Cus_Account.php on line 129

PHP

<?php
if (isset($_POST['Update'])) {
$c_fname = $_POST['fname'];
$c_lname = $_POST['lname'];
$c_email = $_POST['email'];
$c_phone = $_POST['phone'];



$insert_det = "INSERT INTO Cus_acc_details(CUS_Fname,CUS_Lname,Cus_Email,CUS_Phone) VALUES (?,?,?,?)";
$stmt = mysqli_prepare($dbc, $insert_det);
//new
// $stmt = mysqli_prepare($dbc, $insert_c);
//debugging
//$stmt = mysqli_prepare($dbc, $insert_c)  or die(mysqli_error($dbc));

mysqli_stmt_bind_param($stmt, 'sssi', $c_fname, $c_lname, $c_email, $c_phone);

/* execute query */
$r = mysqli_stmt_execute($stmt);

if ($insert_det) {
    echo "<script> alert('registration sucessful')</script>";
 }
 } else {
echo "<b>Oops! Your passwords do not </b>";
}
?>

HTML

    <section class="container">
    <form id="myform " class="Form" method="post" action="Cus_Account.php?c_id=<?php echo $c_id ?>" accept-charset="utf-8">

        <!--                    <div id="first">-->
        <input type="text" id="fname" name="fname" value="<?php echo $c_fname;   ?>" required> 
        <input type="text" id="lname" name="lname"  required>
        <input type="text" id="email" name="email" value="<?php echo    $_SESSION['Cus_Email']; ?>" required>
        <input type="number" id="phone" name="phone"  required>
        <input type="submit" name="Update" value="Update">
        <br>
    </form>

any suggestions would be much appreciated.

Sean
  • 12,443
  • 3
  • 29
  • 47
jerneva
  • 473
  • 1
  • 8
  • 25
  • 1
    `if ($insert_det)` should be `if ($r)` – rmondesilva Mar 23 '16 at 05:32
  • @mondesilva really :s. because the insert is working perfectly. It is justing it to echo out the results is the issue – jerneva Mar 23 '16 at 05:34
  • Yes, but I mean to validate correctly if the query execution(insert) works. – rmondesilva Mar 23 '16 at 05:35
  • @mondesilva still doing the same thing. The same error message – jerneva Mar 23 '16 at 05:38
  • Yes, The error will still be there, that is just additional correction. I will post an answer to fix your problem. But I want to ask if your HTML and PHP is on the same page? – rmondesilva Mar 23 '16 at 05:39
  • @mondesilva thank you, that would be much appreciated as i think I'm misunderstanding what you are suggesting. And yes they are. – jerneva Mar 23 '16 at 05:41
  • Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – alexander.polomodov Mar 23 '16 at 05:42
  • 2
    Since your variable `$c_fname` is only set when `if (isset($_POST['Update']))` is `true`, you will get that error message on initial page load. You could prevent it by changing it to `value=""`. Another solution would be to define `$c_fname` before your `if (isset($_POST['Update']))`, ie. `$c_fname = "";`. – Sean Mar 23 '16 at 05:44
  • Is the php coded first before HTML? I mean is the PHP at the top before your HTML tags? Possible problem of `Notice` you have is you are trying to access $c_fname while it's not yet declared. – rmondesilva Mar 23 '16 at 05:44
  • @mondesilva yes it is before the HTML – jerneva Mar 23 '16 at 05:45
  • And where did you get `$c_id` in your ``? – rmondesilva Mar 23 '16 at 05:47
  • @mondesilva it is another part of my code that i didn't include in this post as it was irrelevant to my problem. – jerneva Mar 23 '16 at 05:51

1 Answers1

2

I think what you're trying to do is to keep the values in form once you submitted the data.

So If I'm right, Do this

<section class="container">
<form id="myform " class="Form" method="post" action="Cus_Account.php?c_id=<?php echo isset($c_id) ? $c_id : ''; ?>" accept-charset="utf-8">

    <!--                    <div id="first">-->
    <input type="text" id="fname" name="fname" value="<?php echo isset($_POST['fname']) ? $_POST['fname'] : '';   ?>" required> 
    <input type="text" id="lname" name="lname"  required>
    <input type="text" id="email" name="email" value="<?php echo isset($_SESSION['Cus_Email']) ? $_SESSION['Cus_Email'] : ''; ?>" required>
    <input type="number" id="phone" name="phone"  required>
    <input type="submit" name="Update" value="Update">
    <br>
</form>
rmondesilva
  • 1,732
  • 3
  • 17
  • 29