I'm new to MySQL and as a learning project I'd like to make a recipe database. I'd like to the user to be able to enter ingredients through a simple HTML form but I'm stuck in how to label the form so that I can enter several ingredients into the database at once.
I'd like to do something like this:
<form method="post" action="insert.php">
Ingredient 1: <input type="text" name="ingredient"><br />
Ingredient 2: <input type="text" name="ingredient"><br />
Ingredient 3: <input type="text" name="ingredient"><br />
<input type="submit" value="Submit">
</form>
When I do this, I add rows to the table but they're all empty. I know it's got something to do with me using "ingredient" (the table value where I want to add the ingredient name) several times in the form, but I just don't know how to solve it.
I would absolutely love some input on how to make it work.
write it like
Ingredient 1: <input type="text" name="ingredient[]"><br />
Ingredient 2: <input type="text" name="ingredient[]"><br />
and when you will get the REQUEST array in php, you will actually
get an array of names
like
$ing = $_POST['ingredient']; // $ing will be indexed array
You can't use same name for multiple textboxes like you have done. The last input box's value will overwrite all of the other ones' since they have the same name. Either you have to use different names for each input texts or define name as array like :
Ingredient 1: <input type="text" name="ingredient[]"><br />
Ingredient 2: <input type="text" name="ingredient[]"><br />
Ingredient 3: <input type="text" name="ingredient[]"><br />
So $_REQUEST['ingredient'] or $_POST['ingredient'] will be a normal PHP array from which you can get the value of each textbox like $_REQUEST['ingredient'][x] where x is some integer index, which is valid as long as count($_REQUEST['addCart']) > x.
Working Code :
index.html
<!DOCTYPE html>
<html>
<head>
<title> Recipes </title>
</head>
<body>
<form method="post" action="register.php">
Ingredient 1 : <input type="text" name="ing1"/><br/>
Ingredient 2 : <input type="text" name="ing2"/><br/>
Ingredient 3 : <input type="text" name="ing3"/><br/><br/>
<input type="submit" value="Enter" name="submit"/>
</form>
</body>
</html>
register.php
<?php
// coding to check database connection
$connection = mysqli_connect('localhost','root','adm','recipe');
/*
root - username
adm - passowrd
recipe - database name
*/
//checking whether submit button is clicked or not
if(isset($_POST['submit']))
{
// Escaping special char & getting the values we entered in form through POST method
$ing1 = mysqli_real_escape_string($connection,$_POST['ing1']);
$ing2 = mysqli_real_escape_string($connection,$_POST['ing2']);
$ing3 = mysqli_real_escape_string($connection,$_POST['ing3']);
//data is table name
$query = "INSERT into data VALUES('','$ing1','$ing2','$ing3')";
$result = mysqli_query($connection,$query);
echo "<p>Successfully Entered</p>";
?>
Click here to go to home
<?
}
?>
Create a database and name it as recipe and a table as data. Table data structure:
Output :
Related
<html>
<head>
<title>test radio button</title>
</head>
<body>
<form method="post" action="">
<input type="radio" name="row1"/>row1
<input type="radio" name="row2"/>row2
<input type="radio" name="row3"/>row3
<input type="submit" value="update"/>
</form>
</body>
</html>
I have a table called table_test. In this table there is a column named as
row1, row2 and row3.
I want to update value of each column in radio button at a time. I need to update one column when I submit the form.
I have searched so many times google but I can't find the solution. Please show me how I can implement this in PHP.
The name of the radio button has to be the same. You use a 'value' attribute to identify the value on the server.
<input type="radio" value="row1" name="myRadio"/>row1
<input type="radio" value="row2" name="myRadio"/>row2
<input type="radio" value="row3" name="myRadio"/>row3
Now, you can get the value in PHP:
echo $_POST['myRadio'];
To use this value in a query, you could do this:
mysql_query(sprintf("INSERT INTO myTable SET %s = 'myValue'", $_POST['myRadio']));
Be warned! Use something like PDO if you don't want your server to get hacked.
<form action="" method="post">
<fieldset>
<input type="text" name="link_name[]" value="xyz" />
<input type="text" name="link_name[]" value="abc" />
<input type="text" name="link_name[]" value="qwe" />
<input type="text" name="link_name[]" value="asd" />
<input type="submit" name="submit" value="submit"/>
</fieldset>
</form>
I want to update any of single data in database.. eg.. if i have to update first text field value like xxx.. then how can i update it using post method.. I want to update not insert so please help me.. give some demo or hint..
if i use like
if(isset($_post['submit']))
{
$link_name=$_POST['link_name'];
mysql_query("update tbl set link_name='$link_name' where id='$id'");
}
then it change all my record with same name.. i cant able to change any particular data.. so please give some suggestion.
I want to update any of single data in database.. eg.. if i have to update first text field value like xxx.. then how can i update it using post method.. I want to update not insert so please help me.. give some demo or hint..
You need to specify the id of every input. You can use different aproaches. For example you can have a hidden input for every text input with the id of this row
<input type="text" name="link_name[]" value="xyz" />
<input type="hidden" name="ids[]" value="340" />
And in the server you need to do a loop on the link_name array and for each element recover the id:
for($i=0;$i<count($link_name);$i++)
{
$id = $_POST['ids'][$i];
mysql_query("update tbl set link_name='$link_name' where id='$id'");
}
to update a single record put a hidden field in the form that holds the id if the post to be updated and then try this
// update only first field
if(isset($_post['submit']))
{
//var_dump($_POST['link_name']);
$link_name=$_POST['link_name'][0];
mysql_query("update tbl set link_name='$link_name' where id='$id'");
}
// update all field
foreach($_POST['link_name'] as $link_name)
{
mysql_query("update tbl set link_name='$link_name' where id='$id'");
}
I have two html forms:
one for entering new data which I need to do empty field validation also check the input against my table data (duplication).
the second is to update a one row of data that was entered by form number one and for this one I need to do name validation "like if name doesn't exists or doesn't match it gives error"
I found some examples online but I didn't understand them
maybe some on here can help
this is form one code :
<form action="http://localhost/wordpress/process.php" method="post" name="myForm">
Name <input id="name" type="text" name="name" />
Telephone <input id="telephone" type="text" name="telephone" />
Fax <input id="fax" type="text" name="fax" />
Web address <input id="webaddress" type="text" name="webaddress" />
State <input id="state" type="text" name="state" />
Address <input id="address" type="text" name="address" />
<input type="submit" name="submit" value="Submit" />
</form>
and this is form two :
<form action="http://localhost/wordpress/orgupdate.php" method="post" name="myForm">
<!-- Same Input fields as Form1 -->
<input type="submit" name= "submit" value="Update" />
</form>
thanks
I've created a Demo for you which you can check here.
1. For checking if input field is Empty, I've used following methods:
a) required = 'required' - Reference: Link.
b) jQuery: - fails if Javascript is disabled.
$("#submit").click(function() {
var name = jQuery.trim($('#name').val());
if(name == ''){
$(".err").text('Name can\'t be left empty.');
return false;
}
return true;
});
c) PHP's empty($_POST['name']);.
2. To check if Already an org exists with same name, after submitting and validating do,
"SELECT * FROM `table_name` WHERE `name` = $_POST['name'];"
If number of rows returned is > 0, then there's an organization that already exists.
3. For updating existing org details, follow two steps:
I. Provide a list of organization names fetched from db.
II. Select a name from the list and then edit.
While editing details, I've made name field as read-only so that You can use name field in where condition to write update query. But its not a correct method, you should make use of id (Primary key) to update a particular value.
I've made way for that also, you can achieve that by using input-type="hidden" to store the id and when you post the form you can retrieve it and use it in update query.
Useful Links:
1. Demo.
2. Download Source Code.
I am using a dynamically generated query string to display results from a search form for some reports - there are 5 search fields and $query2 could contain nothing or 5 additional search values.
$query="SELECT * from employee_work where company_id='$company_id' $query2";
When I POST the form I get the data displayed on screen which is great. I also then am using TCPDF to offer a PDF download of the data. I currently also the class for TCPDF via POST:
if($_POST['PDF']) {
do the TCPDF stuff......
}
<div id="export-buttons">
<form name="export" method="post" action="">
<input type="submit" name="PDF" value="PDF" class="button pdf">
</div>
The problem is that when a user clicks on the PDF button the POST array now only contains the value for the submit button and NOT the $query2 data from earlier so when I use TCPDF it outputs all the data and ignores the $query2 part of the search string.
How can I address this so either the original $query2 data stays available OR have anther way of checking if the form button has been clicked without overwriting the contents of POST? Can I use javascript to do this?
Thanks
Jason
why dont you add hidden inputs that have the values of the criteria from the search? That way when the user posts the request for the PDF you get also those fiels and can use them (AFTER making sure the values are SAFE) in the query.
Other safer way is to store in the session an object or array with the parameters that made the list and then pass the identifier of that search as a hidden input to the PDF form, like:
$_SESSION['sc0000001'] = array('field1'=>'value1', 'field2'=>'value2', 'field3'=123);
...
<form>
<input type="hidden" name="sc" value="0000001" />
...
</form>
when you post the form you get the identifier of the search and create the query with the criteria assign to session...
EDITED:
html before posting list criteria:
<form>
<input type="text" name="filter1" value="" />
<input type="text" name="filter2" value="" />
<input type="text" name="filter3" value="" />
<input type="text" name="filter4" value="" />
<input type="text" name="filter5" value="" />
...
<input type="submit" value="go go go" />
</form>
PHP that gets the filters, builds query, gets results and stores in session.
$sql = " select * from table_name where 1 ";
$arrFilters = array();
for($i=1;isset($_POST['filter'.$i]) && trim($_POST['filter'.$i])!="";$i++) {
$arrFilters['filter'.$i] = mysql_real_escape_string($_POST['filter'.$i]);
$sql.=" AND filter".$i."=".$arrFilters['filter'.$i];
}
// here you should have the complete $sql with the filters supplied
// lets save this search, we are going to keep only the last search from this user in his session.
$_SESSION['listingFilters'] = $arrFilters;
HTML with search results and after the form to get pdf:
<form>
<input type="submit" value="get pdf" />
</form>
PHP:
After the post to get the pdf we go check if there are filters
$sql = " select * from table_name where 1 "; // basic query
// get the filters array from session
$arrFilters = isset($_SESSION['listingFilters']) ? $_SESSION['listingFilters'] : array();
foreach($arrFilters as $filter => $value) { // for each filter add it to the query
$sql.=" AND filter".$i."=".$arrFilters['filter'.$i];
}
// here you should have the complete $sql with the filters in session
// and can do your pdf magic
Add pepper and salt to your pleasure (you need to revise the code to work for you and maybe also the query if you are using text has filters)
Without seeing more code, this is probably because you have two "forms" on the HTML page, with one submit button in one form, and another in the other form.
<form>
<input name="1" />
<input type="submit" name="go"/>
</form>
<form>
<input type="submit" name="createPDF" />
</form>
Make sure you have all the fields/buttons inside the one form.
<form>
<input name="1" />
<input type="submit" name="go"/>
<input type="submit" name="createPDF" />
</form>
i have made user-register.tpl.php file. And i have set many text field in that.
But now i need that....
i want to store the users information to the database. bcz i have created the customized registration page, so i need that my text field values should be store in the database.
like this.......
Username: <input type="text" name="myuser" id="myuser" />
Now i want to store the username, which will entered in this myuser text filed.
NitishPanchjanya Corporation
<form action="index.php" method="post" name="acc_info_form">
Username: <input type="text" name="myuser" id="myuser" /><br/>
First Name: <input type="text" name="name" id="name" /><br/>
<input type="submit" value="Save"/>
</form>
index.php
//Here you have to write database connection and then
<?PHP if ( $_POST){ ?>
mysql_query("INSERT INTO users (myuser, name) VALUES('".$_POST['myuser']."',".$_POST['name']."' )") or die(mysql_error());
<?PHP } ?>