I have problem in storing checkbox values in mysql db . How can I fix this code to store all checked values?
There are seven different columns in type must be stored in the database when you checked for rows
This part of the code is responsible for fetching data from the database and put it in a table:
while ($row = mysqli_fetch_array($query)) {
print
"<tr> <td><input type='checkbox' name='check[]' value='
".$row['course_id'].",
".$row['course_name'].",
".$row['day'].",
".$row['place'].",
".$row['hour'].",
".$row['type'].",
".$row['instructor_name']."' /></td>
<td>{$row['course_id']}</td>
<td>{$row['course_name']}</td>
<td>{$row['day']}</td>
<td>{$row['place']}</td>
<td>{$row['hour']}</td>
<td>{$row['type']}</td>
<td>{$row['instructor_name']}</td>
</tr>";
}
print "</table>
<input type='submit' name='Submit' value='Submit'>
</form>";
This part of the code responsible for storing the data in databases
if(isset($_POST['check'])){
$returned = $_POST['check'];
$office = array('course_id','course_name','day','place','hour','type','instructor_name');
$values = array();
foreach($office as $selection){
$insert1 ="INSERT INTO initial_registration
(course_id,course_name,day,place,hour,type,instructor_name)
SELECT *
FROM offerd_course";
mysqli_query($insert1) or die(mysqli_error());
}
}
I think your whole setup is not good, here is some code to get you started:
first print all courses
while ($row = mysqli_fetch_array($query)) {
print
"<tr>
<td><input type='checkbox' name='row_checked_".$row['course_id']."' value='1' /></td>
<td>{$row['course_id']}</td>
<td>{$row['course_name']}</td>
<td>{$row['day']}</td>
<td>{$row['place']}</td>
<td>{$row['hour']}</td>
<td>{$row['type']}</td>
<td>{$row['instructor_name']}</td>
</tr>";
}
And then for the inserting part:
again go through all courses to see which courses have been 'checked'
while ($row = mysqli_fetch_array($query)) {
if(isset($_POST['row_checked_'.$row['course_id']]) && $_POST['row_checked_'.$row['course_id']]==1){
//insert query here
}
}
Your insert query is also messed up a bit, Why do you want to insert the whole course into initial registration? You'll rather want to do something like this:
$insert1 ="INSERT INTO initial_registration (course_id,STUDENT) VALUES ($row['course_id'],$some_student_id_value)";
Related
I want to enroll the student and insert the student id into Mysql database after I check and submit the checkbox value, but I already tried so many ways but still cannot...
This is the php code
<?php
if (isset($_POST['submitxd'])) {
foreach ($_POST['enrol'] as $items) {
$insert = $link->query("INSERT INTO student_course(studentID) values ('$items')");}
}
?>
This is the html code
$result = $link->query("SELECT * FROM student WHERE programmeName = '$programme' AND intake = '$intake'");
while ($row = mysqli_fetch_array($result)) {
echo "<tr>
<td>".$row['studentID']."</td>
<td>".$row['studentName']."</td>
<td>".$row['studentGender']."</td>
<td>".$row['studentContact']."</td>
<td>
<input type='checkbox' name='enrol[]' value='".$row['studentID']."'>
</td>
</tr>";
}
check whether your array contains values or not:
echo "<pre>";
print_r($_POST['enrol']);
echo "</pre>";
if not, you should write html code properly i.e. check form tag and its action path carefully and before submitting the form, remember to check out the checkbox
The query works fine. I get a table with the associated data in rows. As you see I'd like a button with each row that calls a function that affects that specific row with which it is echoed. I have it working with a normal form, that is, If I put the value of Part_Number in manually the function will run successfully, but the issue is to have it more dynamic. I provided the applicable code.
In short: When I push the button that is echoed with the Part_Number it should update (add one) to the Quantity of that Part_Number.
The SQL doesn't throw an error but it also doesn't work. Everything except passing the variables dynamically works.
$sql = "SELECT id, Part_Number, Description, Location, Quantity, Certificate
FROM Inventory WHERE Part_Number LIKE'%$q%'";
$result = $conn->query($sql);
while($row = $result->fetch_assoc())
echo "
<form method='POST' action='plusone.php'><tbody>
<tr>
<th><h2>".$row["Part_Number"]."</h2></th>
<td><h2>".$row["Description"]."</h2> </td>
<td><h2>".$row["Location"]."</h2> </td>
<td>
<input type='submit' class='btn btn-success' value='Plus 1'>
<input type='hidden' class='form-control' name='Part_Number_Entry'
id='$Part_Number_Entry' value=''>
</tr>
</tbody>
</form>";
if (isset($Part_Number1)) {
// the single quotes around the last variable are very important. If left out
the update wont work
$sql = "UPDATE TestTable SET Quantity= Quantity + 1 WHERE Part_Number=
'$Part_Number1'";
}elseif (!isset($PartNumber)) {
echo "PartNumber is not set";
# code...
}
if (mysqli_query($conn, $sql)) {
echo "Added One to current Record successfully";
} else {
echo "Error updating record: " . mysqli_error($conn);
echo $Quantity;
echo " ";
}
mysqli_close($conn);
?>
there are quite a few issues but this is the least code i could come up wit that will hopefully get you at least going in the right direction:
<input type='hidden' class='form-control' name='Part_Number_Entry' id='$Part_Number_Entry' value=''>
should be
<input type='hidden' class='form-control' name='Part_Number_Entry' value='$row["Part_Number_Entry"]' >
its the 'name' that becomes the array key and the value is from your db row
if (isset($Part_Number1)) {
should be
if (isset($_POST['Part_Number_Entry')) {
your posting the form, and the value you want is in that array.
To get $Part_Number1 for the query
$Part_Number1=(int)$_POST['Part_Number_Entry');
cast as int by you need to much safer db queries in the future.
This is not a great answer and i hope some posts something better.
long post so this is my disclaimer.
I'm working on a function that builds a table from a MYSQL database and displays it in a webpage (using localhost XAMP). That part was easy, figured out that no problem. However, I attempted to make another column for checkboxes to allow me to check off the entries I would like to delete, then click the delete button to delete all those selected. Here is the full function:
<?php // functions.php
$conn = new mysqli($hn, $un, $pw, $db);
if ($conn->connect_error) die ($conn->$connect_error);
function buildTable($query) {
global $conn; // need clarification why this works
$result = $conn->query($query);
if (!$result) die ("Database access failed: " . $conn->error);
$rows = $result->num_rows;
echo "<table> <tr> <th>Delete</th> <th>stockID</th> <th>cardID</th>
<th>categoryID</th> <th>condID</th> <th>Potential Value</th> </tr> ";
for ($j = 0 ; $j < $rows ; ++$j) {
$result->data_seek($j);
$row = $result->fetch_array(MYSQLI_NUM);
This bit of the code is my problem; when I print_r($_POST) on the action page 'inventory.php' the $_POST array doesn't show stockID to exist.
echo "<tr> <td> <form method='post' action='inventory.php'>
<input type='checkbox' name='stockID[]' value='$rows[0]'>
</form></td>";
for ($l = 0 ; $l < 5 ; ++$l) echo "<td>$row[$l]</td>";
echo "</tr>";
};
End possible problem bit, continue rest of code:
echo <<<_END
?>
</table> <a href='card input.php'> Record inventory </a>
<form method='post' action='inventory.php'>
<input type='hidden' name='delete' value='true'>
<input type='submit' value='Delete Record(s)'>
</form>
_END;
};
I then use this IF statement on the action page to determine what is to be deleted:
<?php //inventory.php <-- NOTICE NEW FILE
if ( isset($_POST['delete']) && isset($_POST['stockID']) ) {
$stockID = $_POST['stockID'];
foreach ($stockID as $value) {
$query = 'DELETE FROM `itemized inventory`
WHERE stockID="$value" ';
queryMysql($query);
};
};
Possible issues i've visited:
1.) stockID is used on another file in the same directory and is conflicting.
I don't think two identical associative array keys will conflict if they're on two different files that are not connected by a 'require' or 'include' call. I'm newb so I could be wrong. Ive alse tried different ID's, without it fixing the issue.
2.) My IF statement to call the DELETE command into SQL is incorrect.
This could still be true, but I don't think I can even say that for sure because I would be getting an error message due to my queryMysql($query) function.
<?php //function.php
function queryMysql($query) {
global $conn;
$result = $conn->query($query);
if (!$result) echo "Database action failed: $query" . "<br>" .
$conn->error . "<br><br>";
};
?>
Because I'm not a.) receiving an error and b.) not deleting anything from the database, it leads me to conclude that the <input type='checkbox' name='stockID[]' value='$row[0]'> from the 'functions.php page' is not being pushed to the $_POST array on the action page 'inventory.php' (further confirmed by using print_r($_POST) on the 'inventory.php' page). Because isset($_POST['stockID']) is then false, the IF statement to determine what is to be deleted is not even being called because its condition is false. Therefore, the query isn't even being submitted, and so I cannot prove this to be the problem.
3.)For loops and heredocs are not friends. Maybe? At this point i'm just guessing.... Maybe its for loops and <input> statements that aren't friends?
Thats about all I can think of. Any help would be greatly appreciated. Newb programmer and poster here on stack so plz go easy. Roughness is okay as along as we snuggle afterwards.
thanks!
This last form with the Delete button has no fields named "stockID[]". These fields belong to other forms, so they are not sent with this form.
You can wrap the whole table with checkboxes into one form
<form method='post' action='inventory.php'>
<table> <tr> <th>Delete</th> <th>stockID</th> <th>cardID</th>
<th>categoryID</th> <th>condID</th> <th>Potential Value</th> </tr>
<?php
for ($j = 0 ; $j < $rows ; ++$j) {
$result->data_seek($j);
$row = $result->fetch_array(MYSQLI_NUM);
echo "<tr> <td><input type='checkbox' name='stockID[]' value='$rows[0]'></td>";
for ($l = 0 ; $l < 5 ; ++$l) echo "<td>$row[$l]</td>";
echo "</tr>";
};
</table>
<input type='hidden' name='delete' value='true'>
<input type='submit' value='Delete Record(s)'>
</form>
Or call javascript function before sending the last form, collect values of all checked checkboxes, and pass this into the sent data.
I need help for this problem that I've been trying to solve for a while (I'm new in PHP). I have a form with several checkboxes whose values are pulled from a specific table of the database. I managed to display them in the form, but cannot insert their values into the table connected to that specific page since there is ony one column.I want to enter all the selected values of the checkbox into that single column separated by commas.
Here's the code:
url <BR><?php $query = "SELECT url FROM webmeasurements";
$result = mysql_query($query);
while($row = mysql_fetch_row($result))
{ $url = $row[0];
echo "<input type=\"checkbox\" name=\"url\" value=\"$row[0]\" />$row[0]<br />";
$checkbox_values = implode(';', $_POST['url']); }
?>
<input type="submit" name="Submit" value="Submit">
</form>
<?php
if(isset($_POST['url']))
{ echo $url;
foreach ($_POST['url'] as $statename)
{
$url=implode(',',$statename)
}
}
$q="insert into table (url) values ($url)";
?>
You should give each checkbox a different name, not name="url" for all, and then check if(isset($_POST['checkbox_name'])).
I have a php form with some textbox and checkboxes which is connected to a database
I want to enter all the details into the database from the form.All the textbox values are getting entered except the checkbox values.I have a single column in my table for entering the checkbox values and the column name is URLID.I want to enter all the selected checkbox(URLID)values to that single column only ,separated by commas.I have attached the codings used.can anyone find the error and correct me?
NOTE: (The URLID values in the form is brought from the previous php page.those codings are also included.need not care about it)
URLID[]:
<BR><?php
$query = "SELECT URLID FROM webmeasurements";
$result = mysql_query($query);
while($row = mysql_fetch_row($result))
{
$URLID = $row[0];
echo "<input type=\"checkbox\" name=\"checkbox_URLID[]\" value=\"$row[0]\" />$row[0]<br />";
$checkbox_values = implode(',', $_POST['checkbox_URLID[]']);
}
?>
$URLID='';
if(isset($_POST['URLID']))
{
foreach ($_POST['URLID'] as $statename)
{
$URLID=implode(',',$statename)
}
}
$q="insert into table (URLID) values ($URLID)";
You really should separate your model from the view. It's 2011, not 2004 ;o
if (isset($_POST['checkbox_URLID']))
{
// Notice the lack of '[]' here.
$checkbox_values = implode(',', $_POST['checkbox_URLID']);
}
$URLIDs = array();
while($row = mysql_fetch_row($result))
{
$URLIDs[] = $row[0];
}
foreach ($URLIDs as $id)
{
?>
<input type="checkbox" name="checkbox_URLID[]" value="<?php echo $id; ?>" /><?php echo $id; ?><br />
<?php
}
?>
<input type="submit"/>