php explode function error - php

I'm getting this error message: Notice: Undefined offset: 1 in C:\xampp\htdocs\evantechbd\secure\content\right_cat_pr.php on line 18. I want get news_id and cat_name from a table.
Here is the html form:
<?php
include "db.php";
$sql = mysql_query("SELECT * FROM news_cat");
?>
<form action="right_cat_pr.php" method="post" name="right_cat">
<table width="400" border="0" cellspacing="5" cellpadding="5">
<tr>
<td>News Category Name</td>
<td>
<select name="cat_name">
<?php
while($row = mysql_fetch_assoc($sql))
{
$new_id = $row['news_id'];
$cat_name = $row['cat_name'];
?>
<option "<?php echo $row['news_id'] . '|' . $row['cat_name'] ?>"><?php echo
$row['cat_name']; ?></option>
<?php
}
?>
</select>
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Submit" name="submit"></td>
</tr>
</table>
</form>
Here is the process page:
<?php
include "db.php";
$row = explode('|', $_POST['cat_name']);
$news_id = $row[0]; // cat_id
$cat_name = $row[1];
$query = mysql_query("INSERT INTO right_cat VALUES ('','$news_id','$cat_name')");
if($query)
{
echo "Successfully Inserted your News Category<br/>";
}
else
{
echo "Something is wrong to Upload";
}
?>

You should set the option value with <option value="<?php echo $row['news_id'] . '|' . $row['cat_name'] ?>"

Related

how to refresh tabs data when update table?

index.php
<div class="tab">
<button class="tablinks" onclick="openCity(event, 'Engineering')">Engineering</button>
<button class="tablinks" onclick="openCity(event, 'LAW')">LAW</button>
</div>
<div id="Engineering" class="tabcontent">
<table class="items">
<tr>
<th>State</th>
<th>College Name</th>
</tr>
<?php
$query = "select * from college where field = 'engineering'";
$show = mysqli_query($link,$query);
while ($fetch = mysqli_fetch_array($show))
{
?>
<tr>
<td><?php echo $fetch['state']?></td>
<td><?php echo $fetch['college_name']?></td>
<td>
edit
</td>
</tr>
<?php
}
?>
</table>
</div>
<div id="Law" class="tabcontent">
<table class="items">
<tr>
<th>State</th>
<th>College Name</th>
</tr>
<?php
$query = "select * from college where field = 'law'";
$show = mysqli_query($link,$query);
while ($fetch = mysqli_fetch_array($show))
{
?>
<tr>
<td><?php echo $fetch['state']?></td>
<td><?php echo $fetch['college_name']?></td>
<td>
edit
</td>
</tr>
<?php
}
?>
</table>
</div>
edit.php
<?php
if(isset($_POST['update']))
{
$college_name = $_POST['colleges'];
$state = $_POST['state'];
$sqli = "update college set college_name = '$college_name', state = '$state' where id = '$id'";
$results = mysqli_query($link,$sqli);
if($result == true)
{
$msg .= "<p style='color:green;'>Your data update successfully</p>";
}
else
{
$msg .= "<p style='color:red;'>Errror!</p>";
}
}
?>
<form method="POST" enctype="multipart/form-data" >
<select name="state" id="state">
<option value="<?php echo $stateid; ?>"><?php echo $statename; ?></option>
<option value="">Select State</option>
<?php
$sql = "select * from statemaster";
$result = mysqli_query($link,$sql);
while($row = mysqli_fetch_array($result))
{
echo "<option value=".$row['stateid'].">".$row['statename']."</option>";
}
?>
</select>
<select name="colleges" id="colleges">
<option value="<?php echo $college_name; ?>"><?php echo $college_name; ?></option>
<option value="">Select College</option>
</select>
<button type="submit" name='update' id='update'>update</button>
</form>
In this code when I click on edit button then it will go to edit.php page where I get id from url and run update query after updating table college the data will update but when I move from edit page to index.php page the data will remain same but in database update data will be there. So, How can I fix this issue ?
Thank You
Check for caching. It could be that the browser is not going to the server in order to get the contents of index.php, as it thinks it has it.
Try calling index.php with a variable, like:
Home

onchange dropdown show checkbox is checked in php

code:
<script>
$(document).ready(function(){
$(".menu").click(function(){
ids = $('.menu:checked').map(function() {
return this.id;
}).get().join(',');
console.log(ids);
$("#ids").val(ids);
});
});
</script>
<?php
if(isset($_POST['submit']))
{
$adminid = $_POST['admin'];
$menuids = explode(",", $_POST['ids']);
foreach ($menuids as $idd)
{
$sql = "update menu set admin_id = concat(admin_id,'$adminid',',') where id = '$idd'";
$result = mysqli_query($link,$sql);
}
if($result == true)
{
$msg .= "<p style='color:green'>successfull</p>";
}
else
{
$msg .= "<p style='color:red'>error!</p>";
}
}
?>
<form method="post">
<select name="admin" id="admin">
<option value="">---Select Admin---</option>
<?php
$sql = "select * from admin";
$result = mysqli_query($link,$sql);
while ($row = mysqli_fetch_array($result))
{
?>
<option value="<?php echo $row['id']; ?>"><?php echo $row['firstname']?></option>
<?php
}
?>
</select>
<table>
<tr>
<th>Share</th>
<th>Menu Name</th>
</tr>
<?php
$query = "select * from menu";
$results = mysqli_query($link,$query);
while ($fetch = mysqli_fetch_array($results))
{
?>
<tr>
<td>
<input type="checkbox" class="menu" id="<?php echo $fetch['id']; ?>" name="menuid" />
</td>
<td>
<?php echo $fetch['menu_name']; ?>
</td>
</tr>
<?php
}
?>
</table>
<input type="text" name="ids" id="ids" value=""/>
<input type="submit" name="submit" id="submit" />
</form>
In this code I am update a table having name menu in database. Now, I want to check only those checkbox where admin_id like ,1, or ,2, which is update by query. How can I fix this issue ?please please help.
Thank You
while ($fetch = mysqli_fetch_array($results))
{
?>
<tr>
<td>
<input type="checkbox" class="menu" value="<?php if($fetch['id']==1 or
$fetch['id']==2 ) { echo "checked";} else{} ?>" name="menuid" />
</td>
<td>
<?php echo $fetch['menu_name']; ?>
</td>
</tr>
<?php
}
?>

SQL statement to populate more than one listbox at a time

I'm trying to allow a user to specify how many rows they would like to add to the order form for the customer's purchase. This allows the user to have as many rows as needed for purchasing products rather than having a set list. I have the functionality working properly, where if you type in 3 and submit, it will give you three rows to enter in product order information.
The problem I am running into is where I am populating a listbox with the product id and name for the user to select. It populates the first row's list box, but the following list boxes only get the " - " and not the $row[] values. It seems like it's not passing in the sql statement anymore, why is this?
This is the area in my code where I'm running into a problem with the functionality:
<?
if (isset($_POST['update']))
{
//Execute this code if the update button is clicked.
$num = $_POST['rows'];
for ($i=0; $i<$num; $i++) { ?>
<tr>
<td class="inputCol2">
<select name="'product<?= $i ?>">
<option value="selectProduct">Select Product</option>
<!-- Populate listbox with Product ID and Product Name -->
<?
do { ?>
<option value="<?= $row[0]; ?>"><?= $row[0] . " - " . $row[2]; ?></option>
<? } while($row = mysqli_fetch_array($result)) ?>
</select>
</td>
<td class="inputCol2"><input type="text" name="'quantity<?= $i ?>" ></td>
<td class="inputCol2">$<input type="text" name="'unit<?= $i ?>" value=""></td>
<td class="inputCol2">$<input type="text" name="'total<?= $i ?>" value="" ></td>
</tr>
<? } ?>
And this is my entire code:
<?
connectDB();
$sql = "SELECT * FROM product";
$sql2 = "SELECT DISTINCT emp_id, emp_fname, emp_lname FROM employee";
$sql3 = "SELECT DISTINCT status_id FROM salesorder ORDER BY status_id asc";
$sql4 = "SELECT * FROM salesorder ORDER BY order_id desc";
$result = mysqli_query($db, $sql) or die("SQL error: " . mysqli_error());
$result2 = mysqli_query($db, $sql2) or die("SQL error: " . mysqli_error());
$result3 = mysqli_query($db, $sql3) or die("SQL error: " . mysqli_error());
$result4 = mysqli_query($db, $sql4) or die("SQL error: " . mysqli_error());
$row = mysqli_fetch_array($result);
$row2 = mysqli_fetch_array($result2);
$row3 = mysqli_fetch_array($result3);
$row4 = mysqli_fetch_array($result4);
?>
<div id="order-wrap">
<form method="post" action="order.php">
<table class="orderInfo"><br>
<tr>
<th class="textCol">Product Rows:</th>
<td class="inputCol"><input type="text" name="rows"></td>
<td><input class="update" type="submit" name="update" value="Update"></td>
<td class="inputCol"></td>
</tr>
</table>
</form><!-- Order Rows -->
<form class="orderform" action ="order-report.php" METHOD = "post">
<h2>Order Form</h2>
<h3>Piedmont Furnishings</h3>
<img id="couch-img" src="couch.jpg" alt="couch">
<table class="orderInfo">
<tr>
<th class="textCol">Order Number:</th>
<td class="inputCol"><input type="text" name="orderNumber" value="<?= $row4[0] + 1; ?>" disabled></td>
<th class="textCol">Order Date:</th>
<td class="inputCol"><input type="text" name="orderDate" value="<?= date("Y-m-d") ?>"></td>
</tr>
<tr>
<th class="textCol">Customer:</th>
<td class="inputCol"><input type="text" name="customer"></td>
<td class="textCol"></td>
<td class="inputCol"></td>
</tr>
<tr>
<th class="textCol">Sales Agent:</th>
<td class="inputCol">
<select name="salesAgent">
<option value="selectAgent">Select One</option>
<!-- Populate listbox with Sales Agents ID -->
<?
do { ?>
<option value="<?= $row2[0]; ?>"><?= $row2[1] . " " . $row2[2]; ?></option>
<? } while($row2 = mysqli_fetch_array($result2)) ?>
</select>
</td>
<th class="textCol">Order Status:</th>
<td class="inputCol">
<select name="orderStatus">
<option value="selectStatus">Select One</option>
<!-- Populate listbox with Status ID -->
<?
do { ?>
<option value="<?= $row3[0]; ?>"><?= $row3[0] ?></option>
<? } while($row3 = mysqli_fetch_array($result3)) ?>
</select>
</td>
</tr>
</table>
<!-- Where the product rows input show go ??? -->
<table class="bottomTable">
<tr>
<th class="textCol">Product</th>
<th class="textCol">Quantity</th>
<th class="textCol">Unit Price</th>
<th class="textCol">Total Price</th>
</tr>
<?
if (isset($_POST['update']))
{
//Execute this code if the update button is clicked.
$num = $_POST['rows'];
for ($i=0; $i<$num; $i++) { ?>
<tr>
<td class="inputCol2">
<select name="'product<?= $i ?>">
<option value="selectProduct">Select Product</option>
<!-- Populate listbox with Product ID and Product Name -->
<?
do { ?>
<option value="<?= $row[0]; ?>"><?= $row[0] . " - " . $row[2]; ?></option>
<? } while($row = mysqli_fetch_array($result)) ?>
</select>
</td>
<td class="inputCol2"><input type="text" name="'quantity<?= $i ?>" ></td>
<td class="inputCol2">$<input type="text" name="'unit<?= $i ?>" value=""></td>
<td class="inputCol2">$<input type="text" name="'total<?= $i ?>" value="" ></td>
</tr>
<? } ?>
<tr>
<td class="textCol"></td>
<td class="textCol"></td>
<td class="textCol">Total Order:</td>
<td class="inputCol2">$<input type="text" name="totalfinal"></td>
</tr>
<input class="submit" type="submit" value="Submit" name="orderSubmit"/>
</table>
</form>
<? } else {?>
<tr>
<td class="textCol"></td>
<td class="textCol"></td>
<td class="textCol">Total Order:</td>
<td class="inputCol2">$<input type="text" name="totalfinal"></td>
</tr>
<input class="submit" type="submit" value="Submit" name="orderSubmit"/>
</table>
</form>
<? } ?>
<?
mysqli_free_result($result);
mysqli_close($db);
?>
</div>
the problem with your code is for first iteration while($row = mysqli_fetch_array($result)) the internal pointer of $result reached at the end... so for next iteration $i=1 there is nothing in the $result but As you use do-while loop the loop must run at least one time and $row[0] & $row[2] is null so you get only "-" . to fix the problem you need to change code slightly.
remove this line $row = mysqli_fetch_array($result);
and add
$options = '<option value="selectProduct">Select Product</option>';
while($row = mysqli_fetch_array($result,MYSQLI_NUM)){
$options .= '<option value="'.$row[0].'">'.$row[0].' - '.$row[1].'</option>';
}
then change like this inside for loop :
<td class="inputCol2">
<select name="'product<?= $i ?>">
<?php
echo $options;
?>
</select>
</td>

How to populate a textbox with the appropriate information related to the item selected from a drop down list in PHP?

I'm creating a form where it allows a user to enter in the number of product rows they would like to have in the form. Then with the corresponding rows, you can select the Product with the Product_ID & Product_Name from a Drop Down List. With the selected item, it should pull the Product_Cost from the table and populate the Unit Price textbox. I can't seem to get the textbox to populate with the correct data. My if statement if (isset($_POST['product' . $i])){ doesn't seem to be working properly, it runs as if the statement were false. I'm trying to say "If a select box has an option selected, take the option selected and find it's corresponding row in the database and take the price found in that row for that product and populate the unit price textbox."
<? require_once("connect_to_DB.php"); //inserts contents of this file here ?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Order Form</title>
<meta charset="utf-8">
</head>
<body>
<?
connectDB();
$sql = "SELECT * FROM product";
$sql2 = "SELECT DISTINCT emp_id, emp_fname, emp_lname FROM employee";
$sql3 = "SELECT DISTINCT status_id FROM salesorder ORDER BY status_id asc";
$sql4 = "SELECT * FROM salesorder ORDER BY order_id desc";
$result = mysqli_query($db, $sql) or die("SQL error: " . mysqli_error());
$result2 = mysqli_query($db, $sql2) or die("SQL error: " . mysqli_error());
$result3 = mysqli_query($db, $sql3) or die("SQL error: " . mysqli_error());
$result4 = mysqli_query($db, $sql4) or die("SQL error: " . mysqli_error());
//This is for the options in the product list box
$options = '<option value="selectProduct">Select Product</option>';
while($row = mysqli_fetch_array($result,MYSQLI_NUM)){
$options .= '<option value="' . $row[0] . '">' . $row[0] . ' - ' . $row[2] . '</option>';
}
$row2 = mysqli_fetch_array($result2);
$row3 = mysqli_fetch_array($result3);
$row4 = mysqli_fetch_array($result4);
?>
<div id="order-wrap">
<form method="post" action="example.php">
<table class="orderInfo"><br>
<tr>
<th class="textCol">Product Rows:</th>
<td class="inputCol"><input type="text" name="rows"></td>
<td><input class="update" type="submit" name="update" value="Update"></td>
<td class="inputCol"></td>
</tr>
</table>
</form><!-- Order Rows -->
<form class="orderform" action ="order-report.php" METHOD = "post">
<h2>Order Form</h2>
</table>
<!-- Where the product rows input show go ??? -->
<table class="bottomTable">
<tr>
<th class="textCol">Product</th>
<th class="textCol">Quantity</th>
<th class="textCol">Unit Price</th>
<th class="textCol">Total Price</th>
</tr>
<?
if (isset($_POST['update']))
{
//Execute this code if the update button is clicked.
$num = $_POST['rows'];
for ($i=0; $i<$num; $i++) { ?>
<tr>
<td class="inputCol2">
<select name="'product<?= $i ?>'">
<?
echo $options;
?>
</select>
</td>
<td class="inputCol2"><input type="text" name="'quantity<?= $i ?>'" ></td>
<? if (isset($_POST['product' . $i])){ ?>
<td class="inputCol2"><input type="text" name="'unit<?= $i ?>'" value="<?= $row[3] ?>" placeholder="$" ></td>
<? } else { ?>
<td class="inputCol2"><input type="text" name="'unit<?= $i ?>'" value="" placeholder="$"></td>
<? } ?>
<td class="inputCol2"><input type="text" name="'total<?= $i ?>'" placeholder="$"></td>
</tr>
<? } ?>
<tr>
<td class="textCol"></td>
<td class="textCol"></td>
<td class="textCol">Total Order:</td>
<td class="inputCol2"><input type="text" name="totalfinal" placeholder="$"></td>
</tr>
</table>
<input class="submit" type="submit" value="Submit" name="orderSubmit"/>
</form>
<? } else {?>
<tr>
<td class="textCol"></td>
<td class="textCol"></td>
<td class="textCol">Total Order:</td>
<td class="inputCol2">$<input type="text" name="totalfinal"></td>
</tr>
</table>
<input class="submit" type="submit" value="Submit" name="orderSubmit"/>
</form>
<? } ?>
<?
mysqli_free_result($result);
mysqli_free_result($result2);
mysqli_free_result($result3);
mysqli_free_result($result4);
mysqli_close($db);
?>
</div>
</body>
There appear to be some syntax errors in there, for instance:
<select name="'product<?= $i ?>'">
<?
echo $options;
?>
</select>
There seems to be several instances where this type of quotation marks are used - I think they ought to be more like:
<select name="product<?= $i ?>">
<?
echo $options;
?>
</select>
Also, in a few places you are not using the correct opening tag for the php blocks though someone might point out different. Generally they ought to be:
<?php
/* statements */
?>
or
<?="print out this string";?>

SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '= '{search_term}''

I can't do the live search table thing. Can someone help me please?
Here is my code. I want to show only the data I've search.........................................................................................................................................................................................................................................................................
<?php
//include the connection file
include "conn.php";
$sql = "SELECT * FROM tblreservation";
if (isset($_POST['search'])) {
$search_term = mysql_real_escape_string($_POST['search_box']);
$sql .= "WHERE Name = '{search_term}'";
}
$query = mysql_query($sql) or die(mysql_error());
?>
<form name="search_form" method="POST" action="trys.php" align="center">
Search: <input type="text" name="search_box" value="" />
<input type="submit" name="search" value="Search the table...">
</form>
<table width="70%" cellpadding="5" cellspace="5">
<tr>
<td>ID</td>
<td>Name</td>
<td>Email</td>
<td>Packages</td>
<td><select name="Packages" class="fieldsize">
<option value="">select package</option>
<option value="budget" <?php if($valid_Packages=='budget') echo "selected='selected'";?>>Budget</option>
<option value="standard" <?php if($valid_Packages=='standard') echo "selected='selected'";?>>Standard</option>
<option value="super" <?php if($valid_Packages=='super') echo "selected='selected'";?>>Super</option>
<option value="mega" <?php if($valid_Packages=='mega') echo "selected='selected'";?>>Mega</option>
</select>
<span class="err"><?php echo $error["Packages"];?></span></td>
</tr>
<td>Contactno</td>
<td>Gender</td>
<td><input type="radio" name="gender" value="male" <?php if($valid_gender=='male') echo "checked='checked'";?> />
Male
<input type="radio" name="gender" value="female" <?php if($valid_gender=='female') echo "checked='checked'";?>/>
Female <span class="err"><?php echo $error["gender"];?></span></td>
<td>file</td>
<td><input type="file" name="file" value="upload" />
<span class="err"><?php echo $error["file"];?></span></td>
<td>Address</td>
</tr>
<?php while ($row = mysql_fetch_array($query)) { ?>
<td><?php echo $row['id']; ?> </td>
<td><?php echo $row['Name']; ?> </td>
<td><?php echo $row['Email']; ?> </td>
<td><?php echo $row['Packages']; ?> </td>
<td><?php echo $row['Contactno']; ?> </td>
<td><?php echo $row['Gender']; ?> </td>
<td><?php echo $row['file']; ?> </td>
<td><?php echo $row['Address']; ?> </td>
</tr>
<?php } ?>
</table>
You are missing a $ and a space in this line:
$sql .= "WHERE Name = '{search_term}'";
The correct line should be as follows:
$sql .= " WHERE Name = '{$search_term}' ";
The SQL statement you are currently generating is exactly this:
SELECT * FROM tblreservationWHERE Name = '{search_term}'
Additionally, I would recommend checking for the existence of $_POST['search_box'] rather than $_POST['search'] in your if-statement and that it actually has a value before appending it as this is what you actually want to use in your query:
if (isset($_POST['search_box']) && $_POST['search_box']) {
$search_term = mysql_real_escape_string($_POST['search_box']);
$sql .= " WHERE Name = '{$search_term}' ";
}

Categories