thank you for taking time to look at this. I have been dealing with this annoying foreach loop. Here is what I am trying to do.
I have the first page "form.php".
On this page I have check boxes. Each check box is generated from a database table. Here is the code:
<?php
include("config.php");
$mysqli = new mysqli($host, $db_uname, $db_pass, $db);
$query = "SELECT * FROM `plugins` WHERE 1";
if ($result = $mysqli->query($query)) {
echo '<form action="test.php" method="post">
<input name="gname" placeholder="Group Name..."/>
<table width="200">
';
while ($row = $result->fetch_assoc()) {
echo '<tr><td>
<label>
<input type="checkbox" value="'.$row["plugin"].'" name="checkbox[]">
'.$row["plugin"].'
</label>
</td></tr>';
}
echo '
</table>
<select name="permplugin">
<option>Select One...</option>';
$query2 = "SELECT * FROM `permission_types` WHERE 1";
if ($result2 = $mysqli->query($query2)) {
echo '<h3>Select Permission format below</h3><hr />';
while ($row2 = $result2->fetch_assoc()) {
echo '
<option value="'.$row2["plugin_name"].'">'.$row2["plugin_name"].'</option>';
}
echo '
</select>
<br />
<input name="" type="reset"><input name="" type="submit">
</form>';
}
}
?>
Now after that it sends the checked boxes to "test.php"
here is the code for that:
<?php
if(!empty($_POST['checkbox']) || !empty($_POST['select']) || !empty($_POST['gname'])) {
echo '<h1>'.$_POST['gname'].'</h1>';
$check1 = $_POST['checkbox'];
foreach($check1 as $check) {
include "functions.php";
checkboxes($check);
}
echo '<h3>Selected Permission format below</h3><hr />';
echo $_POST['permplugin'];
} else {
echo "please select atleast one plugin.";
}
?>
The functions page code looks like this:
<?php
//all functions are here.
function checkboxes($check){
$mysqli_perm = new mysqli("localhost", "uname", "pword", "tcordero_permnodes");
$query_perm = "SELECT * FROM permission_nodes WHERE plugin = `$check`";
if ($result_perm = $mysqli_perm->query($query_perm)) {
echo $check;
/* fetch associative array */
while ($row_perm = $result_perm->fetch_assoc()) {
echo $row_perm['node'].'<br />';
}
unset($check);
}
}
When I run the test.php I get this error:
Fatal error: Cannot redeclare checkboxes() (previously declared in C:\xampp\htdocs\TPYC\functions.php:3) in C:\xampp\htdocs\TPYC\functions.php on line 15
What am I doing wrong?
You need to take the include out of the foreach loop. Try this:
include "functions.php";
foreach($check1 as $check) {
checkboxes($check);
}
Related
Sorry if the Title doesn't make any sense.
So, my code is as follows:
<?php
$conn=new mysqli("localhost","root","aman8blue","users");
$conn->connect_error;
$querynor= mysqli_query($conn,"SELECT * FROM userinfo");
$nor=mysqli_num_rows($querynor);
for($i=1; $i<=$nor; $i++) {
?>
<form method='post'>
<label class='switch'><input type='checkbox' name='check' value=1 onChange='this.form.submit()'><span class='slider_round'></span></label>
</form>
<?php
$status = 0;
if(isset($_POST['check'])) {
$status=1;
}
if($status==1) {
echo "WORKING!";
}
else {
echo "NOPE!";
}
}
?>
But no matter what, even if I tick the checkbox, it shows "WORKING!". I can't understand where am I wrong.
(Sorry the previous one was incomplete)
Thanks in Advance!
add
name="check"
in your input since post is looking for name attribute and you don't have one
<?php
$conn = new mysqli("localhost", "root", "xxxx", "users");
$conn->connect_error;
$querynor = mysqli_query($conn, "SELECT * FROM userinfo");
$nor = mysqli_num_rows($querynor);
echo '<form method=\'post\'>';
for ($i = 1; $i <= $nor; $i++)
{
?>
<label class='switch'><input type='checkbox' name='check[<?php echo $nor[$i]['id']; ?>]' value=1 onChange='this.form.submit()' <?php echo (isset($_POST['check'][$nor[$i]['id']]) ? 'checked':''); ?>>
<span class='slider_round'></span>
</label>
<?php
}
echo '</form>';
?>
As the answer to the comment.
I am in the process of creating a filter page that should return a table of results dependent upon the selection chose from a drop down list.
I have generated the code for this and no errors are found. However, no results show up despite them being present in the database.
Am I missing something?
Please let me know if any further information is required.
Any help would be greatly appreciated!
<?php
error_reporting(E_ALL);
ini_set('display_errors','1');
$search_output= "";
$link= mysqli_connect("localhost","root","");
mysqli_select_db($link,"assessment_centre_app");
if(isset($_POST['submit'])) {
$Name_FK = $_POST['Name_FK'];
$sqlCommand = mysqli_query($link, "SELECT *
FROM scores
WHERE 'Name_FK' = {'$Name_FK'}");
$query = mysql_query($sqlCommand) or die (mysql_error());
$count = mysqli_num_rows($query);
if($count > 1) {
$search_output .="$count results for $searchquery";
while($row = mysql_fetch_array($query)) {
$Candidate_Name_FK = $row["Candidate_Name_FK"];
$search_output .="Item ID: $Candidate_Name_FK <br />";
}
} else {
$search_output= "0 results found";
}
}
?>
<html>
<head>
</head>
<body>
<form action="http://localhost/scoresheet/scoresheetfilter.php" method="POST">
<label> Assessment Day Name </label>
<select name = "Name_FK">
<?php
$res=mysqli_query($link,"SELECT * FROM scores");
while($row=mysqli_fetch_array($res))
{
?>
<option>
<?php echo $row["Name_FK"]; ?>
</option>
<?php } ?>
</select>
<br>
<input name ="myBtn" id="submit" type="submit" >
<br>
</form>
<div>
<?php echo $search_output;
?>
</div>
</body>
</html>
Try this out if it still does not display anything the check the variable used to call this other one's
<?php
error_reporting(E_ALL);
ini_set('display_errors','1');
$search_output= "";
$link= new mysqli("localhost", "user_name", "password", "database");
if(isset($_POST['submit'])) {
$Name_FK = $_POST['Name_FK'];
$sqlCommand = "SELECT * FROM scores WHERE Name_FK='$Name_FK'";
$query = mysqli_query($linnk, $sqlCommand) or die (mysqli_error());
$count = mysqli_num_rows($query);
if($count > 1) {
$search_output .="$count results for $searchquery";
while($row = mysqli_fetch_array($query)) {
$Candidate_Name_FK = $row["Candidate_Name_FK"];
$search_output .="Item ID: $Candidate_Name_FK <br />";
echo $search_output; //echo to check result
}
} else {
$search_output= "0 results found";
echo $search_output; //echo to check result
}
}
?>
Now edited
variable
$searchquery
not created
I have two forms on one page. First one take names of students according to group. Second form is used to enter marks individually. Now i want to insert their marks but failed to do this. Kindly help me regarding this. My code is:
$faculty = null; //declare vars
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
die('Not connected : ' . mysql_error());
}
mysql_select_db('Sims', $link) or die("cannot select DB");
if(isset($_POST["faculty"]))
{
$faculty = $_POST["faculty"];
}
?>
<script language="JavaScript">
function autoSubmit()
{
var formObject = document.forms['theForm'];
formObject.submit();
}
</script>
<form name="theForm" method="post">
<select name="faculty" onChange="autoSubmit();">
<option value="null"></option>
<option value="computer" <?php if($faculty == 'computer') echo " selected"; ?>>Computer</option>
<option value="commerce" <?php if($faculty == 'commerce') echo " selected"; ?>>Commerce</option>
</select>
<br><br>
<?php
if($faculty =='computer')
{
echo "<select name=\"name\">";
$sql = mysql_query("SELECT name FROM std_reg where faculty= 'computer' ") or die(mysql_error());
while($row = mysql_fetch_array($sql)){
echo "<option>".$row[name]."</option>";}
echo "</select>";
}
if($faculty =='commerce')
{
echo "<select name=\"name\">";
$sql = mysql_query("SELECT name FROM std_reg where faculty= 'commerce' ") or die(mysql_error());
while($row = mysql_fetch_array($sql)){
echo "<option>".$row[name]."</option>";}
echo "</select>";
}
?>
<br><br>
</form>
<form method="post">
math <input type="text" name="urdu" />
science <input type="text" name="science" />
social <input type="text" name="social" />
submit
</form>
I'm trying to do a select from a table based on the post value of an HTML select box. I'm getting no results at all, I'm echoing out the post value no problem. The statement works on it's own but won't when I use the select form to populate it. This is just my test I will be adding other options to the dropdown box.
<?php
if(isset($_POST['value'])) {
if($_POST['value'] == 'Militaria') {
$query = "SELECT * FROM listings WHERE category1=Militaria";
}
else {
// query to get all records
$query = "SELECT * FROM listings";
}
}
$sql = mysql_query($query);
while ($row = mysql_fetch_array($query)){
echo 'Description:' . $row['description'];
}
mysql_close($con);
?>
Here is the html form I'm using, can anyone tell me where I'm going wrong, should I do it a different way etc, I'm new to php? Thanks!!
<form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post' name='form_filter' >
<select name="value">
<option value="all">All</option>
<option value="Militaria">Militaria</option>
</select>
<br />
<input type='submit' value = 'Filter'>
</form>
mysql_fetch_array() should receive resorce as a parameter. Try mysql_fetch_array($sql).
Quote around 'Militaria' and mysql_fetch_array($sql)
<?php
if(isset($_POST['value'])) {
if($_POST['value'] == 'Militaria') {
$query = "SELECT * FROM listings WHERE category1='Militaria'";
}
else {
// query to get all records
$query = "SELECT * FROM listings";
}
$sql = mysql_query($sql);
while ($row = mysql_fetch_array($sql)){
echo 'Description:' . $row['description'];
}
mysql_close($con);
}
?>
<form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post' name='form_filter' >
<select name="value">
<option value="all">All</option>
<option value="Militaria">Militaria</option>
</select>
<br />
<input type='submit' value = 'Filter'>
</form>
You have two mistakes in your php code.
1st : quote around Militaria. The query should be, $query = "SELECT * FROM listings WHERE category1='Militaria'";
2nd : mysql_fetch_array accepts executed query's result as parameter. It should be, $row = mysql_fetch_array($sql)
Final code:
<?php
if(isset($_POST['value'])) {
if($_POST['value'] == 'Militaria') {
$query = "SELECT * FROM listings WHERE category1 = 'Militaria'";
}
else {
// query to get all records
$query = "SELECT * FROM listings";
}
}
$sql = mysql_query($query);
while ($row = mysql_fetch_array($sql)){
echo 'Description:' . $row['description'];
}
mysql_close($con);
?>
I am trying to create an update form to update to the database table. I have created a function that uses sql to create the update, and a function that updates the table. I have also created the code that calls these functions. The error is that nothing is posting or changing when I press submit. It isn't running or editing or updating any of the data.
Please help me get this code working!
Thanks
function updateSingleValue($ID, $Name)
{
$sql = "UPDATE faculty SET Name=$Name WHERE ID=$ID";
$mysqlConnection = getConnection();
$statement = $mysqlConnection->prepare($sql);
$bReturn = false;
try
{
$statement->execute();
$bReturn = true;
}
catch (PDOException $e)
{
echo $e->getMessage();
}
return $bReturn;
}
function getUpdateResults($tablename)
{
$mysqlConnection = getConnection();
$sql = "SELECT * FROM ".$tablename;
$ResultSet = $mysqlConnection->query($sql);
return $ResultSet;
}
<?php
include_once 'db.php';
if(isset($_POST['update']))
{
$success = updateSingleValue($_POST['ID'], $_POST['Name'], $_POST['update']);
echo $success;
if(!$success)
{
echo 'Sorry, the update failed.';
}
}
$Results = getUpdateResults('faculty');
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<label id="lblUpdate" for="update">Update</label>
<input type="text" name="update" />
<input type="text" name="ID" />
<input type="text" name="Name" />
<input type="submit" name="submit" value="Submit"/>
</form>
<table border="1">
<tr>
<th>ID</th>
<th>Name</th>
</tr>
<?php
if(isset($Results))
{
foreach($Results as $row)
{
echo '<tr><td>';
echo $row['ID'];
echo '</td><td>';
echo $row['Name'];
echo '</td></tr>';
}
}
?>
</table>
Iv'e noticed something with this:
function getUpdateResults($tablename)
{
$mysqlConnection = getConnection();
$sql = "SELECT * FROM ".$tablename;
$ResultSet = $mysqlConnection->query($sql);
return $ResultSet;
}
try doing concatinating your tablename like this
$t = $tablename;
$sql = "SELECT * FROM $t";
Please close the open connection at the end....
mysql_close($mysqlConnection);