I have created a PHP-site which basically is a site with some forms for MySQL-input.
This is how it looks right now. Pretty simple, fill it in and click submit to enter the data into the database.
http://i.imgur.com/ieMf39f.jpg
This is how I want it to look and work. I wan't it to be able to send multiple querys to the database with only one submit-button.
http://i.imgur.com/IdqcJjb.jpg
So the question is.. how do I solve this problem in the most simple and efficient way?
In the name of each field, use e.g. name="opb1[]", this way it will turn into an array (opb = array).
Then you could do like:
foreach($opt1 as $k => $_opt1)
if(isset($opb2[$k]) && isset($opb3[$k]) .....etc) {
// use each row e.g. $opb2[$k]...
}
}
Your HTML should look like:
<input name="name[]" />
<input name="obp1[]" />
<input name="obp2[]" />
<br />
<input name="name[]" />
<input name="obp1[]" />
<input name="obp2[]" />
and your PHP:
for($i = 0; $i < count($_POST['name']); $i++) {
// verify if all fields are completed
if(empty($_POST['name'][$i]) || empty($_POST['obp1'][$i])) { // all fields here
// add an error
} else {
$mysqli->query("INSERT INTO my_table VALUES ('" . $mysqli->real_escape_string($_POST['name'][$i]) . "', '" . $mysqli->real_escape_string($_POST['obp1'][$i]) . "')");
}
}
Related
I am programming a page right now, and I am stuck with a problem.
The thing is that I am making a page, where you can answer all kind of question, but as it is possible to make new questions for that page, I am not able to keep a track of every input box, as it can't be something manual.
Let us say that i have the textboxes:
<input type="text" name="question_1" />
<input type="text" name="question_2" />
<input type="text" name="question_3" />
<input type="text" name="question_4" />
But as I add more questions from another page, with help from MySQL, there come new fields, for example:
<input type="text" name="question_5" />
<input type="text" name="question_6" />
<input type="text" name="question_7" />
As there can come unlimited questions, I cant really get their values by making PHP string like these:
$question1 = $_POST["question_1"];
$question2 = $_POST["question_2"];
$question3 = $_POST["question_3"];
and so on, because I can't know how many there are going to be made.
So my question is: Is it possible to get the value of every input box, which name is starting with "question_" and store them in a variable?
Thanks in advance, and sorry for my bad English :)
$i = 1;
$myarray = [];
while ($_POST["questions_".$i]) {
$i++;
array_push($myarray, $_POST["questions_".$i]);
}
Loop thru them until it's null. This pushes them to an array, but you could use them in the loop.
You can then generate a string from those too, for example a MySQL query:
$i = 1;
$firstpartofquery = "";
$secondpartofquery = "";
while ($_POST["questions_".$i]) {
$i++;
if ($firstpartofquery) { $firstpartofquery .= ", "; }
$firstpartofquery .= "question_" . $i;
if ($secondpartofquery) { $firstpartofquery .= ", "; }
$secondpartofquery .= "'" . $_POST["questions_".$i] . "'";
}
$myquery = "INSERT INTO question($firstpartofquery) VALUES($secondpartofquery)";
Just name you all of form's input elements as question[]
i.e
<input type="text" name="question[]" />
<input type="text" name="question[]" />
<input type="text" name="question[]" />
Then from your script you can loop through them, something like the following,:
$questions = $_POST['question'];
foreach ($questions as $question){
echo $question." <br /> \n";
}
This solution make life easier if you want to handle those fields on the client-side i.e via javascript or jquery for example, you will be able to handle them easily as:
<script>
questions = document.getElementsByName('question[]');
</script>
So by this way you have an array of all objects -fields- that contain your questions. As I think, that's the standard solution for handling multiple fields of the same entity in your forms.
I have a while loop generating information with a checkbox, I would like to update the database with the new "completed" value. How can I select the specific checkbox that is generated. Please help with showing me how I can grab the specific value of a checkbox and the task_name.
Thanks, Ryan
while ($row = mysql_fetch_array($query)){
$task_name = $row['task_name'] ;
$task_description = $row['task_description'];
$task_completed = $row['completed'];
$tasks .= '<div id="tasksBody">
<form action="" method="post">Completed? <input name="completed" type="checkbox" '.
($task_completed == 1?'checked="checked"':'').
' /><input type="submit" value="Update"><br /><br />
<b>'.$task_name.'</b><br /><br />'.$task_description.'<hr><br /></form></div>';
}
}
echo $tasks;
You need to name your input with something unique for the row, such as the task_name, or better, a database record ID.
Then when the user submits the form, you will use $_POST["YourTaskNameOrIDHere"] to check the value.
What you have currently calls all the check boxes the same thing.
EDIT: I'm sorry, you're isolating all of these in their own forms, I just realized that.
What you can add is an <input type="hidden" value="$task_name" name="TaskName" /> to the form, so you can look what the checkbox is corresponding to. Then, when the user submits the form, use $_POST["TaskName"] to find out the name of the task.
Add a hidden field to each of your forms containing the task_id
<form action="" method="post">
Completed?
<input name="completed" type="checkbox" <?=($task_completed == 1?'checked="checked"':'')?> value="1" />
...
<input name="task_id" value="<?=$task_id"?> type="hidden" />**strong text**
</form>
After submit:
if (isset($_POST['task_id']) { // form has been submitted
$task_id = $_POST['task_id'];
$completed = $_POST['completed'];
$sql = "UPDATE task SET task_completed=$completed WHERE task_id=$task_id LIMIT 1";
// code for updating database
// better use PDO or mysqli-* instead of old and deprecated mysql_*
}
With this line of code, I add every selected checkbox's value into the database:
// ADD ALL TYPES TO PRODUCTIONLOG_TYPE TABLE
$x=1;
$values=array();
foreach($_POST['id'] as $x)
{
$AddToListQuery = "
INSERT INTO
productionlog_type
(productionlogid, typeid)
VALUES
('" . mysql_real_escape_string($_GET['productionlog']) . "', '". mysql_real_escape_string($x) ."')
";
mysql_query($AddToListQuery)or die("query fout " . mysql_error() );
}
I do this so I can echo out the checkbox checked if this is alreayd in the database.
The problem now is, when the user unchecks the checkbox and sends the form, it's not passing any value, right? So I can't delete it from the database...means that the checkbox remains checked.
What can I do about this?
If you know which of the checkboxes will be returned, you can filter out the ones you know something about and only the checkboxes that are not checked remain then.
You can also have a look at some processing of your page before you send the request, with JavaScript for example.
Or you can pass another hidden variable in your form that contains all the checkboxes you should receive in your PHP script. For example:
<input type="hidden" value="id_1,id_2,id_3" />
If you then receive this in your PHP script, you can see which of the ids (of the checkboxes) are expected.
EDIT
If you have:
<form method="POST">
<input type="hidden" name="checks" value="id1,id2" />
<input type="checkbox" name="id1" checked="checked" />
<input type="checkbox" name="id2" />
</form>
And then in PHP:
$arr = split(",", $_POST["checks"]);
foreach ($arr as $x){
if(isset($_POST[$x])){
// Add or update
}else{
// Remove
}
}
Note: I haven't tested the code, but it you just to get an idea on how it works
I am trying to insert some values into a database from a form submitted by the user.
$id = mysql_insert_id();//Gets ID of last value inserted into related table
foreach( $_POST[ 'equipment' ] as $checkBoxIndex => $checkBoxValue ) {
mysql_query("INSERT INTO recipeequipment (recipeid, equipmentid, datetimeentered) VALUES('".$id."', '".$checkBoxValue."', '".$datetime."')");
}
The only thing that needs adding to this insert statement is the quantity, which is held in a similar fashion to the checkboxes, but in textboxes. like this:
<?php while($rowequipment = mysql_fetch_assoc($sqlequipment)) {
echo '<input type="checkbox" name="equipment[]" value="'.$rowequipment['equipmentid'].'"/>
<input type="text" name="count[]" id="count[]" size="3" value="" />'
.$rowequipment['description']."<br />";
}?>
The piece i need help with is the second input column. I need to put the values that correspond with the check boxes into the insert statement, but am unsure of how to do this.
Ignoring all the vunrabilities in your code for now, doing something like this should allow you to get a unique id and match the checkbox up to the input:
<?php while($rowequipment = mysql_fetch_assoc($sqlequipment)) {
echo '<input type="checkbox" name="equipment['.$rowequipment['equipmentid'].']" value="'.$rowequipment['equipmentid'].'"/>
<input type="text" name="count['.$rowequipment['equipmentid'].']" id="count-'.$rowequipment['equipmentid'].'" size="3" value="" />'
.$rowequipment['description']."<br />";
}?>
Then once that form is submitted you would simply loop through each checkbox by doing something like:
foreach ($_POST['equipment'] as $id => $checkboxValue) {
$textInputValue = $_POST['count'][$id];
// do something with the above value here
}
Although, I'm not quite sure why you're using checkboxes like this - is it so you ignore updating the rows that they don't check off or something?
I have created a form, with 5 textbox fields and I want to add those five entries in the database. I want to use the textbox "array", that way I can use a for-each when saving to the database. As anyone, any code on how to do this or can direct me in the right path?
input type="text" value="whateva" name= ?php text[0] ?>
input type="text" value="whateva" name= ?php text[1] ?>
input type="text" value="whateva" name= ?php text[2] ?>
if (isset($_POST['Submit']) {
//add to db
(for-each $text as $val) {
//add to db
}
}
Is this possible?
HTML
<input type="text" value="whateva" name="text[]" />
<input type="text" value="whateva" name="text[]" />
<input type="text" value="whateva" name="text[]" />
PHP
if (!empty($_POST['text'])) {
foreach ($_POST['text'] AS $value) {
// add to the database
$sql = 'INSERT INTO tableName SET fieldName = "' . mysql_real_escape_string($value) . '"';
}
}
Yes, HTML supports arrays. just name your textareas like this:
<textarea name="field[]"></textarea> /* Notice square brackets */
For this example, in PHP, your $_GET or $_POST will have array key with name 'field' and values from these textareas.
If 'Submit' is the name of the submit button. yeah that will work.
but few suggestions:
correct it as:
< input type="text" value="whateva" name= "" />
Use validation for the text submitted by user
IMPORTANT: "GET A BOOK ON PHP" and learn it. Seriously, if you learn this way, you wont become a good programmer. You are learning it the hardway. Book is must for you.