How to store data in database using redbeans php - php

I am trying to store data from my web form to database using redbeans but I cannot make it work. I have already make sure that database is connected. I am using apache xampp server for web and sql.
This is my code
Form
<head>
</head>
<?php
require "validatecar.php";
?>
<body>
<form method="POST">
Model <input type="text" name="Model">
Manufacturing Company <input type="text" name="Company">
Pessenger Capacity <input type="number" name="Pessenger">
Luggage Capacty <input type="number" name="Luggage">
Doors <input type="number" name="Doors">
Transmission <input type="text" name="Transmission">
Stereo <input type="text" name="Stereo">
Air Conditioning <input type="text" name="AC">
Image <input type="file" name="Image">
<input type="submit" name="Submit">
</form>
</body>
Validate
<?php
include "dbconnection.php";
?>
<?php
if(isset($_POST['Submit']))
{
$model = $_POST['Model'];
$company = $_POST['Company'];
$pessenger = $_POST['Pessenger'];
$luggage = $_POST['Luggage'];
$doors = $_POST['Doors'];
$transmission = $_POST['Transmission'];
$stereo = $_POST['Stereo'];
$ac = $_POST['AC'];
$image = $_POST['Image'];
$cars = R::dispense('cars');
$cars->model = $model;
$cars->company = $company;
$cars->pessenger = $pessenger;
$cars->luggage = $luggage;
$cars->doors = $doors;
$cars->transmission = $transmission;
$cars->stereo = $stereo;
$cars->ac = $ac;
$cars->image = $image;
R::store($cars);
R::close();
}
?>
If you could tell me what am I doing wrong and how to solve it, that would be very helpful. Thanks in advance.

You've gone for naming using first letter capital. But for some reason your name for your submit fails your naming convention.
You are looking for $_POST['Submit'] when you have named it all lower case 'submit' instead of Submit so your code for storing in your Database will never run.
If you were to perform a var_dump() or print_r() on $_POST.
I.E. in your validate.php for instance, prior to your if test, you will see what is being "Posted".
<?php
var_dump($_POST); // Added for Debug - View what is being posted.
if(isset($_POST['Submit']))
{
//... Rest of code here
So in your form, your submit input has a wee typo for your "name" value.
<input type="submit" name="submit">
should be...
<input type="submit" name="Submit">

Related

Problem: Part of the PHP exercise does not run correctly

I created a section for editing. When I edit the information and click the save button, the information is not saved and the header section does not display completely.
<?php
if (isset($_POST['submit_btn']))
{
$id = $_POST['id'];
$fn = trim($_POST['name']);
$ln = trim($_POST['lastname']);
$age = trim($_POST['age']);
$q = "UPDATE `users` SET `fn` = '$fn',
`ln` = '$ln',
`age` = '$age'
WHERE id = '$id'";
mysqli_query($dbconnect,$q);
if (mysqli_affected_rows($dbconnect) > 0)
redirect("?msg=ok&id=**$id**");
else
redirect("?msg=error&id=**$id**");
}
else
echo ("Not In If(isset)");
?>
<form action="" method="post">
<label for="name">FirstName:</label>
<input type="text" name="name" id="name" value="<?php echo $row['fn']?>">
<br>
<label for="lastname">LastName:</label>
<input type="text" name="lastname" id="lastname" value="<?php echo $row['ln']?>">
<br>
<label for="age">Age:</label>
<input type="text" name="age" id="age" value="<?php echo $row['age']?>">
<br>
<input type="submit" name="submit_btn" value="Save">
<a href="index2.php">
Back
</a>
</form>
</body>
Bold sections do not work here.
Below is a picture of the result:
In the link that I specified, after clicking on save the ID will not be displayed and all the information filled in the forms will be lost.
Sorry if the result is styleless and boring and I just created this page to practice php😁
Thank you for being responsive🙏🙏🙏
You are mistaking a POST request with a GET request.
Part, which appears in the URL is sent to the webserver in GET request.
Your form is submitting POST request to the webserver, logic in the code does the same, but you are trying to display information from url (GET).
Please check the examples in php.net:
POST variables: https://www.php.net/manual/en/reserved.variables.post.php
GET variables: https://www.php.net/manual/en/reserved.variables.get.php
You can take an example with GET request variable below, however, be careful with trusting the "end client" and always prepare your statements, which you send to your database to execute queries.
if (isset($_GET['submit']))
{
$number = $_GET['number'];
echo $number
? "Number which was submitted: $number <br>"
: 'Number is not set';
} else {
echo 'Form has not been yet submitted';
}
?>
<form action="" method="get">
<input type="number" name="number" placeholder="Number">
<input type="submit" name="submit" value="Save">
</form>

How to hold the form data from one page and show it in after two pages without session and database

i want to display my first page data in third page without session or database.
here is my page1.php form.
<form method="post" action="page1">
name<input type="text" name="name">
password<input type="password" name="pass">
retype password<input type="password">
<input type="submit" name="submit" value="Submit"/>
</form>
it post the value to page two. and my page2.php contain one button. if i click that button means it goes to page2
<form method="post" action="page3">
<?php
if(isset($_POST["submit"]))
{
$name= $_POST["name"];
$pass= $_POST["pass"];
}
?>
<input type="submit" name="submit1" value="submit">
</form>
now i need to display fist page data to here in page3.php
<?php
if(isset($_POST["submit1"]))
{
$name= $_POST["name"];
$pass= $_POST["pass"];
echo $name;
echo $pass;
}
please any one give idea to make this thing to work.
thank you.
We can maintain the data without using session variable or database storage, using the "input type=hidden" property like this
<form method="post" action="page3">
<?php
if(isset($_POST["submit"]))
{
$name= $_POST["name"];
$pass= $_POST["pass"];
}
?>
<input type="hidden" name="name" value="<?php echo $name; ?>">
<input type="hidden" name="pass" value="<?php echo $pass; ?>">
<input type="submit" name="submit1" value="submit">
</form>
And you can retrieve this values in your 3rd form using post variables like this..
<?php
if(isset($_POST["submit1"]))
{
$name= $_POST["name"];
$pass= $_POST["pass"];
echo $name;
echo $pass;
}
?>
Although I personally wouldn't recommend this method for a sensitive data as passwords, as the users can see the values in html form if they need to, using inspect element.

PHP GET and POST request in signle page what is happning

I have PHP code that do the following things
1-get some request parameters from parent page
2-send post request for it self and validate data
3-redirect the combined data to the next page
here is sample snippet
I am having difficult time fixing my messy code... pleas help me!
<?php
//on page load---from parent page
if ($_SERVER["REQUEST_METHOD"] == "GET") {
$room_number=$_GET['room_number'];
}
//on submit
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST["name"];
$lastname = $_POST["lastname"];
//do some stuff... validation
if($valid){
// if valide redirect to ...
header("Location: RegisterUser.php?name=".$name."&lastname=".$lastname."& roomnumber=".$room_number."");
exit();
}
}
?>
<form action='<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>' method='post' >
<input type="text" name="name">
<input type="text" name="lastname">
</form>
the problem i am having is at the redirection $room_number is empty always
how can i fix it??
I know i am doing a lot of stuff in single file that is because i am very beginner.
it would be great if someone can suggest me better way to so such stuff.
Thank you in advanced!
You could use hidden input for room_number input.
<?php
if(isset($_POST['submit'])) {
$name = $_POST['name'];
$lastname = $_POST['lastname'];
$room_number = $_POST['room_number'];
header("Location: RegisterUser.php?name=".$name."&lastname=".$lastname."&roomnumber=".$room_number);
exit();
}
?>
<form action='<?php echo $_SERVER["PHP_SELF"];?>' method='post'>
<input type="text" name="name">
<input type="text" name="lastname">
<input type="hidden" name="room_number" value="<?php echo $room_number; ?>" />
<input type="submit" name="submit" value="Submit" />
</form>
And then use the above code in your file. It may help you. And Why are you redirecting those variables to another register page. Just use them in itself to insert into database or anything else what you want.
Add your $room_number in your form as a hidden input field so that you can pass it through as a post variable.
<input type="hidden" name="room_number" value="<?php echo $room_number; ?>" />

Simple PHP: getting variable from a form input

I can't really use PHP and from what I've seen in tutorials this should work, but it doesn't:
<html>
<head></head>
<body>
<form>
<input type='text' name="name" value='myName'>
</form>
<p>
<?php
$name = $_POST['name'];
echo $name
?>
</p>
</body>
</html>
Is there a reason I can't get the value of name?
Sorry for asking such a simple question...
here is the fiddle http://jsfiddle.net/DCmu5/1/, so, please try what you said and send it to me only when it works before answering
PHP is a server-side language, so you'll need to submit the form in order to access its variables.
Since you didn't specify a method, GET is assumed, so you'll need the $_GET super-global:
echo $_GET['name'];
It's probably better though, to use $_POST (since it'll avoid the values being passed in the URL directly. As such, you can add method attribute to your <form> element as follows:
<form method="post">
<input type='text' name="name" value="myName" />
<input type="submit" name="go" value="Submit" />
</form>
Notice I also added a submit button for good measure, but this isn't required. Simply hitting return inside the textbox would submit the form.
Well, you have to POST your form to get the value of $_POST variables. Add this to your <form>
<form action="yourpagename.php" method="post">
<input type='text' name="name" value='myName'>
<button type="submit">Submit</button>
</form>
Click button and whatever is typed in your field will show up.
<html>
<body>
<form method="post" action="1.php">
Name: <input type="text" name="fname">
<input type="submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect value of input field
$name = $_POST['fname'];
if (empty($name)) {
echo "Name is empty";
} else {
echo $name;
}
}
?>
</body>
</html>
try this
<?php
if(isset($_REQUEST['name'])){
$name = $_REQUEST['name'];
echo $name;
}
?>

Is it possible to include a form in a wordpress page?

I have the following code in a php template called contact_us. I have created a new page which uses this template, but when you click submit it doesn't post back to the same page and display what the user entered in the form. Any ideas why this doesn't work?
Thanks,
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = $_POST["name"];
$comments = $_POST["comments"];
echo $name;
echo $comments;
}
?>
<form action="<?php echo $PHP_SELF;?>" method="post" >
Name : <br/>
<input type="text" name="name" /><br/>
Comment <br/>
<textarea name="comments" name="comments"></textarea>
<br/><br/>
<input name="submit" type="submit" id="submit" value="Send" />
</form>
Make sure that you don't use "name" as a variable name. I will assume that the same thing goes for comments.
More information here
http://wpquicktips.wordpress.com/2010/02/17/use-an-empty-action-attribute-in-forms/
Does it make any difference removing <?php echo $PHP_SELF;?> from the action and leaving it blank?
I managed to get this to work by adding an id attribute to the form. I think this is something wordpress requires.

Categories