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
Related
why if conditon is not behaving properly in comparing values one which is coming from database and other coming from form via post..inside while loop of mysql fetch array
<?php
$right=0;
$wrong=0;
$result = mysql_query("SELECT * FROM mcq") ;
$total =mysql_num_rows($result);
echo "total questions are :".$total;
echo "<br>";
while($row = mysql_fetch_array($result))
{
$b=$row['id'];
$a=$_POST['a_'.$b];
$cor=$row['correct'];
if($a==$cor)
$right++;
else
$wrong++;
}
$a is coming from radio buttons from previous page and $cor is coming from database..i m comparing selected value of radio button with cor(correct answer of that value) which is coming from data base.. but condition is not executing rightly so please help me!!!!
The radio button just send one value. Your way to retrieve it is wrong, because you are basing in the ID, but the id should be used in the value.
Usually in HTML you should use some like this:
<input type="radio" name="myradio" value="item1" />
<input type="radio" name="myradio" value="item2" />
<input type="radio" name="myradio" value="item3" checked />
So, you should try to get the value from post:
$myvar = $_POST["myradio"];
Can you paste the radio button code?
I'm trying to add multiple values into a database using PHP from an HTML. However, I can't just refer to the name attribute of the HTML form because each field in the form is generated by a PHP script. I've tried Googling around, but since I don't exactly know what I'm looking for, my search has been futile.
Here's the bit of code that I use to generate the HTML form:
<form action="input_points.php" method="post">
<?php
while($row = mysql_fetch_array($result)) {
echo $row['Name'] . ' <input type="text" name="userpoints">';
}
?>
<button type="submit" name="add_points">Add Points </button>
</form>
I don't know what names are currently in the directory so I need this piece of php to determine what names are in the database. Afterwards, I want to have a bunch of boxes for people to input points (hence the form). I'm having trouble figuring out how to link the particular text box with the user.
For example, if I have a text box for Bob, how would I link up the input text field that contains the number of points Bob earns with Bob's entry in the database?
I know you can do this with regular form fields:
$userpoints = $_POST['userpoints'];
UPDATE members SET points = $userpoints where $user = "Bob";
But since I have multiple users, how do I link up the correct database entry with the right user? Also, how would I determine which boxes are empty and which boxes are updated with a value?
If you want to update multiple filed then are using array
Please changes some code
<form action="input_points.php" method="post">
<?php
$userCount=mysql_num_rows($result);
echo '<input type="hidden" name="userCount" value="' .$userCount. '">';
while($row = mysql_fetch_array($result)) {
echo '<input type="hidden" name="userid[]" value="' .$row['id']. '">'; //Give the uniq id
echo $row['Name'] . ' <input type="text" name="userpoints[]">';
}
?>
<button type="submit" name="add_points">Add Points </button>
</form>
PHP Code -
$userCount = $_POST['userCount'];
for($i=1; $i=$userCount; $i++){
$userpoints = $_POST['userpoints'];
$userid = $_POST['userid'];
//UPDATE members SET points = $userpoints where $user = $userid;
//YOUR CODE HERE
}
The update you're trying to do is not safe unless you treat values to prevent SQL injection... but if you really want it, instead of mysql_fetch_array(), try using mysql_fetch_assoc().
Using mysql_fetch_assoc() you can extract the keys (database field names) with array_keys(). The keys will be your the name property of your form fields and the values of will be the fields' values.
Hope it helps.
You can use an array to store all the data that you need and add a hidden field that contains the missing data:
<form action="input_points.php" method="post">
<?php
for($i=0; $row = mysql_fetch_array($result); $i++ ) {
echo ' <input type="hidden" name="user[0]['name'] value ='". $row['name'] ."'">';
echo $row['Name'] . ' <input type="text" name="user[$i]['points'] ">';
}
?>
<button type="submit" name="add_points">Add Points </button>
</form>
Problem when you hit submit userpoints contain only the last value previous all values are overwritten
solution
name="userpoints"
must be different each time why not you define it in database and then fetch it just like you fetch $row['Name']?
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_*
}
Below is an explanation of exactly what I want
On the submission form I have multiple checkboxs. when users click on checkbox a textbox appears next to it, where user input text. I want to insert the value of texbox in array and insert it in database with PHP-MYSQL.
for example checkbox1 is index 0=>valueoftextbox, checkbox2 is index 1=>value of texbox2
How can I achieve this?
Serialize received $_POST.
<input type="checkbox" name="smth[]" value="One" />
<input type="checkbox" name="smth[]" value="Two" />
<input type="checkbox" name="smth[]" value="Three" />
<?php
if (isset($_POST)) {
$smth = serialize($_POST['smth']);
$query = mysql_query("INSERT INTO table VALUES ('$smth')") or die(mysql_error());
}
A few pointer to get you going:
1) From PHP use:
echo "<pre>";
print_r($_POST);
echo "</pre>";
That way you can see WHAT is posted from your client-form.
2) Note that an unchecked checkbox ISN'T posted at all.
So if you have following checkbox in your form named "wantsnewsletter".
Then check that from PHP like this:
if (isset($_POST["wantsnewsletter"])){
// It was checked.
} else {
// It wasn't checked
}
Futhermore you just have to think up some sensible naming and do the inserts into you DB.
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?