user1715037
user1715037

Reputation: 1

Saving textbox values to database

I'm kinda new to PHP and MySQL.

<p>Pickup Date: <input type="date" name="date"></p>
<input type='submit' name='submit' value='Secure Order!'>

So I have his part in my code. I want the date to be saved in my database. In my database I have a table named date with columns 'date' and 'account name'. I want that when I click the 'secure order' button, the date in the textbox saves in the database. Also, the current user logged in would also be saved in account name.

Upvotes: 0

Views: 9017

Answers (4)

Famurewa Taiwo
Famurewa Taiwo

Reputation: 84

<?php 
    if(isset($_POST['submit'])){
        // you can get the logged in user from the session
        // this means when your user is logged in, you need to set the 
        //session
        $accountId = $_SESSION['userId']; 
        $date = filter_input(INPUT_POST, 'date');
        $sql= "INSERT INTO date (`date`, `account Id`) VALUES($date,$accountId)";
        //for this line to work, you need to store your database connection to mysqli in $mysqli
        if ($mysqli→query($sql)) {
           printf("Record inserted successfully.<br />");
        }
        if ($mysqli→errno) {
           printf("Could not insert record into table: %s<br />", $mysqli→error);
        }
        $mysqli→close();
    }else{
?>
<html>
<head>
  <title>Add New Record in MySQL Database</title>
</head>
<body>
 <form method = "post" action = "<?php $_PHP_SELF ?>">
   <p>Pickup Date: <input type="date" name="date"></p>
   <input type='submit' name='submit' value='Secure Order!'>
 </form>
</body>
</html>
<?php
  }
?>

Upvotes: 0

Rush
Rush

Reputation: 715

You can use PHP $_POST SuperGlobal to do that :

HTML:

<input type="text" name="username" />

PHP-onsubmit

<?php

if(isset($_POST['submit'])
{
   $username= $_POST["username"];
   $date= $_POST["date"];

// your insert code //

}
?>

Upvotes: -1

dan
dan

Reputation: 3519

Why are you using double quotes on your first <input> and then simple quotes on your second <input>? Most of the time, in HTML, we will encourage the use of double quotes.

Also, there are a lot of missing parts for this form to be correct. Here is just an example of how to use the variable typed in the input:

<?php
    if(isset($_POST['date'])) {
        echo "You picked ", $_POST['date'], "\n"
    }
?>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
    <p>Pickup Date: <input type="date" name="date"></p>
    <input type="submit" value="Secure Order!">
</form>

As for the MySQL part, you will need to learn and try a bit by yourself. Start there: http://www.php.net/manual/en/book.mysqli.php

Upvotes: 0

alanmanderson
alanmanderson

Reputation: 8230

on your form suppose you have:

<form id="myform" action="myphppage.php" method="post">

now when you hit submit the data in the input will be posted to myphppage.php.

Now you need to write myphppage.php.

At the top you will have something like:

<?php
    $account = $_POST['account'];
    $mydate = $_POST['date'];
    $query="INSERT INTO date (date,account name) VALUES($mydate,$account)";
    //Use mysqli object to execute query.
?>

I haven't used the mysqli library because I got used to using the old mysql_ libraries but I got yelled at for suggesting it since it is now deprecated.

This should get you started.

It is also common to have the form post back to itself with the data. In which imagine your form is on the page "myphppage.php" then you could have the following at the top:

<?php 
    if(isset($_POST['submit'])){
        //continue with the php code from above here
    }
?>

Upvotes: 0

Related Questions