I need to insert the value of form into the data table.
here is my form for taking a test.
<form method="post" action="">
1<input type="text" name="answer">
3<input type="text" name="answer">
5<input type="text" name="answer">
7<input type="text" name="answer">
<input type="submit" name="submit">
</form>
how to store this all answer in one field of the data table
i have tried this code but its not storing all value
if(isset($_POST['submit']))
{
$answer = json_encode($_POST['answer']);
$query = "INSERT INTO `test` (`answer`) VALUES ('$answer')";
mysqli_query($con, $query);
if (mysqli_query($con, $query)) {
echo "inserted ";
} else {
echo "Error updating record: " . mysqli_error($con);
}
}
Try changing your HTML code to this.
<form method="post" action="">
1<input type="text" name="answer[]">
3<input type="text" name="answer[]">
5<input type="text" name="answer[]">
7<input type="text" name="answer[]">
<input type="submit" name="submit">
</form>
When you submit the form the $_POST['answer'] would look something like this
Array
(
[answer] => Array
(
[0] => first answer
[1] => second answer
[2] => third answer
[3] => fourth answer
)
)
Above is a printout of $_POST variable like so print_r($_POST)
Now you can easily get all the answer inputs inside PHP and have it converted to json like so
$answer = json_encode($_POST['answer']);
However since you are saving this inside mysql I would suggest using serialize() instead of json_encode(). Please read about serialize() https://www.php.net/manual/en/function.serialize.php
Related
I am posting a values from a HTML form to a php file. In my current code I am submitting several post values and have to do a check for each post variable if they are set.
I'd like to know if there is a more effective way of doing this. One requirement is that the values will be inserted in order.
HTML form:
<form>
<input type=text name=exercise1> <input type=text name=sets1>
<input type=text name=exercise2> <input type=text name=sets2>
<input type=text name=exercise3> <input type=text name=sets3>
<input type=text name=exercise4> <input type=text name=sets4>
...
</form>
SQL table:
id autoincrement
exercise varchar(200)
sets varchar(10)
I tried the next code:
$exercise1 = $_POST['exercise1'];
$sets1 = $_POST['sets1'];
$exercise2 = $_POST['exercise2'];
if(isset($exercise1)){
$sql = "insert into exercises (exercise, sets) values ($exercise1, $sets1)";
execute_sql($sql);
}
if(isset($exercise2)){
$sql = "insert into exercises (exercise, sets) values ($exercise2, $sets2)";
execute_sql($sql);
}
Default form method is GET, so you are probably not getting anything while trying to read $_POST. To fix it, you need to change this:
<form>
to this:
<form method="post">
To make it easier, you should redefine your form, so it would be an array:
<input type="text" name="exercise[]"> <input type="text" name="sets[]">
<input type="text" name="exercise[]"> <input type="text" name="sets[]">
<input type="text" name="exercise[]"> <input type="text" name="sets[]">
<input type="text" name="exercise[]"> <input type="text" name="sets[]">
That will keep an order as in code. Some browsers are not sending empty values, so it would be better to manually order them (so you would know, if there was no answer or whatever):
<input type="text" name="exercise[1]"> <input type="text" name="sets[1]">
<input type="text" name="exercise[2]"> <input type="text" name="sets[2]">
<input type="text" name="exercise[3]"> <input type="text" name="sets[3]">
<input type="text" name="exercise[4]"> <input type="text" name="sets[4]">
Now you can iterate through it in PHP like this:
<?php
foreach($_POST["exercise"] as $id => $exercise){
echo "EXERCISE $id: " . $exercise . ", SETS $id: " . $_POST["sets"][$id] . "<br />";
}
?>
Please note, that your SQL query is probably vulnerable to injection attacks!
Instead of raw query, you should use something like mysqli_real_escape_string() (or similar; depends what lib are you using to connect to database):
<?php
$sql = "insert into exercises (exercise, sets) values (" . mysqli_real_escape_string($exercise) . "," . mysqli_real_escape_string($_POST["sets"][$id]) .")";
?>
Lets say inside my sql have some data:
name | table_no
Apple | c1
Grape | c1
if($result->num_rows > 0){ //read the data inside sql and display out
while($row = $result->fetch_assoc()){
echo "Name: ". $row['name']. "<br>". "Table No.: ". $row['table_no']. "<br>";
}
}else{ // insert the data if inside sql have no data of table_no
if($_SERVER['REQUEST_METHOD'] == "POST"){
$value = $_POST['value'];
$table = $_POST['table'];
}
$sql = "INSERT INTO testing (name, table_no) VALUES ('$value', '$table')";
mysqli_query($conn, $sql);
}
?>
<form method="post" action="">
<h2>C1</h2>
<input type="hidden" value="c1" name="table">
<input type="text" name="value">
<input type="submit">
</form>
<form method="post" action="">
<h2>C2</h2>
<input type="hidden" value="c2" name="table">
<input type="text" name="value">
<input type="submit">
</form>
My problem is, how to make the webpage more dynamic that can automatically change to a blank form that can be fill in the data(table_no) and if the form already has the data it will display the data instead of showing the blank form to let user fill in.
You can dynamically update the value in your input. Assuming $result is the result of your SQL query and there is a field named C1 in $result, here an example for your form C1:
<!-- langage: html -->
<form method="post" action="">
<h2>C1</h2>
<input type="hidden" value="<?php echo $result['c1']; ?>" name="table">
<input type="text" name="value">
<input type="submit">
</form>
I know there are similar topics out there but haven't been able to find what I'm looking for. So what I need to do is target a specific input name and foreach loop only that input instead of the whole form.
HTML look something like below.
<form action"<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data" name="table1" method="post">
<input name="something1" type="text" />
<input name="something2" type="text" />
<input name="something3" type="text" />
<input name="something4" type="text" />
<input name="something4" type="text" />
<input name="something4" type="text" />
<input name="button" type="submit" value="Add" />
</form>
So I wanna loop every "something4" and just ignore the rest. Is this possible?
Just to explain what I want to do with the value is for every "something4" I'm gonna add a field to my DB and the input the respective input value into that field.
something like below...
$i = 0;
foreach ($_POST as $something4 => $something4_value) {
$add = mysqli_query($connect, "ALTER TABLE 'table' ADD something4$i VARCHAR( 255 ) NOT NULL") or die (mysql_error());
$sql_update = mysqli_query($con, "UPDATE 'table' SET something4$i='$something_value' WHERE id='$id'") or die (mysql_error());
$i++;
}
I hope this make sense! Thank you! :)
Create each of the name="something4" into an array like this:
<input name="something4[]" type="text" />
Then you can do a
foreach($_POST['something4'] as $something4) {
}
I have 4 different MySQL query's and I want to be able to choose which one is used based on which radio button is clicked. I was wondering what would be the best way to do this?
<form action="">
<input type="radio" name="query1" value="">Rating<br>
<input type="radio" name="query2" value="">Genre<br>
<input type="radio" name="query3" value="">Year<br>
<input type="radio" name="query4" value="">Rating,Genre,Year
</form>
Do I store the query's on individual php pages and then call them using the radio button or ....?
The query's select movie info from a database and then order them by either rating, year, genre or all three.
Set all your radio buttons to hold the same name attribute but with different values, then upon submit and choosing the desired radio button, query for what is matched.
<?php
if(isset($_POST['submit'])){
if(!isset($_POST['query'])){
echo "You chose nothing";
}
if($_POST['query'] == "Rating"){
// query for Rating
echo "Rating";
}
if($_POST['query'] == "Genre"){
// query for Genre
echo "Genre";
}
if($_POST['query'] == "Year"){
// query for Year
echo "Year";
}
if($_POST['query'] == "Rating,Genre,Year"){
// query for Rating,Genre,Year
echo "Rating,Genre,Year";
}
} // brace for if(isset($_POST['submit']))
?>
<form action="" method="post">
<input type="radio" name="query" value="Rating">Rating<br>
<input type="radio" name="query" value="Genre">Genre<br>
<input type="radio" name="query" value="Year">Year<br>
<input type="radio" name="query" value="Rating,Genre,Year">Rating,Genre,Year
<br>
<input type="submit" name="submit" value="Submit query">
</form>
This is at best, a basic example.
When using database-related code, remember to use mysqli with prepared statements, or PDO with prepared statements, they're much safer.
Radio buttons must have same name.
Without a method="post" the <form> method will be "get" so the PHP would use $_GET not $_POST
<form action="">
<input type="radio" name="query" value="1">Rating<br>
<input type="radio" name="query" value="2">Genre<br>
<input type="radio" name="query" value="3">Year<br>
<input type="radio" name="query" value="4">Rating,Genre,Year
</form>
I am not a big fan of using IF ELSE branching structures and avoid them whenever possible.
I prefer passing integer values to be used in arrays.
PHP
$q = intval($_GET['query']);
The intval() will return a zero if ($_GET['query'] return no value.
$queries = array(
0 => $query4,
1 => $query1,
2 => $query2,
3 => $query3,
4 => $query4);
$sql = $queries[$q];
please forgive naivety and innocence...I am not a programmer! I have spent the best part of 4 days on this and I am ready for a PHP lesson or intense therapy.
Scenario: DB built in mySQL. Table with all columns varchar(50) except ID and age - both INT. See below, I just need a 'Yes' value in the checkbox linked colums/fields.
I want to insert data with a form that has both textboxes and checkboxes. I thought best way to do this was php array...??
Form:
<form action="process.php" method="post" enctype="multipart/form-data">
<label>Childname
<input type="text" name="textfield[childname]" />
</label>
<p>
<label>Age
<input type="text" name="textfield[age]" />
</label>
</p>
<p>
<label>Parent Name
<input type="text" name="textfield[parent_name]" />
</label>
</p>
<p>
<label>Contact Number
<input type="text" name="textfield[contact_no]" />
</label>
</p>
<p>Subjects<br />
<label>
<input type="checkbox" name="checkbox[scratch]" value="checkbox" />
Scratch</label>
<label>
<input type="checkbox" name="checkbox[app_inventor]" value="checkbox" />
App Inventor</label>
<label>
<input type="checkbox" name="checkbox[html]" value="checkbox" />
HTML</label>
</p>
<p>Sessions Attended<br />
<label>
<input type="checkbox" name="checkbox[nov12]" value="checkbox" />
Nov 2012</label>
</p>
<p>
<label>
<input type="checkbox" name="checkbox[dec12]" value="checkbox" />
Dec 2012</label>
</p>
<p> </p>
<p>
<label>
<input type="submit" name="Submit" value="Submit" />
</label>
</p>
</form>
PHP script:
<?php
include("config.php");
$childrecord = array("childname","age","parent_name","contact_no","scratch","app_inventor","html");
if(isset($_POST['childrecord'])){
$childrecord = $_POST['childrecord'];
$i = 0;
foreach ($childrecord as $key => $value); {
$i++;
$sql="INSERT INTO tblchildren (childrecord) VALUES ($_POST['childrecord'])";
mysql_query($sql);
}
?>
Please help!
Thanks in advance....
You want to store your form data in your code.
For this you have to make following changes in your code and database.
In Form
note: Input field value should be relevant.
With your existing html code your process.php will get data in this structure
Array
(
[textfield] => Array
(
[childname] => dang
[age] => 18
[parent_name] => doctor
[contact_no] => 100
)
[checkbox] => Array
(
[scratch] => checkbox
[app_inventor] => checkbox
[html] => checkbox
[nov12] => checkbox
[dec12] => checkbox
)
[Submit] => Submit
)
So you need to modify your process.php
Before that you have to modify structure of your table, follow these steps
1. Delete your existing table
2. Create new table
DROP TABLE tblchildren ;
CREATE TABLE tblchildren
(
id INT AUTO_INCREMENT PRIMARY KEY,
childname VARCHAR(30),
age TINYINT,
parent_name VARCHAR(30),
contact_no VARCHAR(20),
scratch ENUM('yes','no'),
app_inventor ENUM('yes','no'),
html ENUM('yes','no'),
sesNov12Attnd ENUM('yes','no'),
sesDec12Attnd ENUM('yes','no')
);
Since you are new to php so i have used basic function,
I will suggest you to use switch from mysql to mysqli
process.php
<?php
$con=mysql_connect("hostname","username","pass");
$db=mysql_select_db('dbname', $con);
//check form is submitted
if(isset($_POST)){
//mysql_real_escape_string() prevents from sql injection.
$childname= mysql_real_escape_string($_POST['textfield']['childname']);
$age=mysql_real_escape_string($_POST['textfield']['age']);
$parentName=mysql_real_escape_string($_POST['textfield']['parent_name']);
$contactNo=mysql_real_escape_string($_POST['textfield']['contact_no']);
$scratch=isset($_POST['checkbox']['scratch'])?'yes':'no';
$appInventor=isset($_POST['checkbox']['app_inventor'])?'yes':'no';
$html=isset($_POST['checkbox']['html'])?'yes':'no';
$nov12=isset($_POST['checkbox']['nov12'])?'yes':'no';
$dec12=isset($_POST['checkbox']['dec12'])?'yes':'no';
$sql="INSERT INTO tblchildren(childname, age, parent_name, contact_no, scratch,app_inventor,html, sesNov12Attnd, sesDec12Attnd )
VALUES
('$childname',$age,'$parentName','$contactNo','$scratch','$appInventor','$html','$nov12','$dec12')";
mysql_query($sql);
}else{
echo "Form Not SUbmitted";
}
?>