search not found message after run query - php

I'm making a search query for my webs, and i get an error for my query after i add the if ($results==0) ......
Object of class mysqli_result could not be converted to int in line 65 which refer to if ($results==0)
$results = $mysqli->query("SELECT * FROM produk2 WHERE $construct" );
if ($results==0)
{
echo "Sorry, there are no matching result for <b>$search</b>.</br></br>1.
Try more general words. for example: If you want to search 'how to create a website'
then use general keyword like 'create' 'website'</br>2. Try different words with similar
meaning</br>3. Please check your spelling";
}
else{
//fetch results set as object and output HTML
while($obj = $results->fetch_object())
{
echo '<div class="product">';
echo '<form method="post" action="cart_update.php">';
echo '<div class="product-thumb"><img src="images/'.$obj->product_img_name.'"></div>';
echo '<div class="product-content"><h3>'.$obj->product_name.'</h3>';
echo '<div class="product-desc">'.$obj->product_desc.'</div>';
echo '<div class="product-info">';
echo 'Price '.$currency.$obj->price.' | ';
echo 'Qty <input type="text" name="product_qty" value="1" size="3" />';
echo '<button class="add_to_cart">Add To Cart</button>';
echo '</div></div>';
echo '<input type="hidden" name="product_code" value="'.$obj->product_code.'" />';
echo '<input type="hidden" name="type" value="add" />';
echo '<input type="hidden" name="return_url" value="'.$current_url.'" />';
echo '</form>';
echo '</div>';
}
}

$mysqli->query don't have the data count until you ask for it
Try like this
if($results ->num_rows == 0){
// no result found
}

Related

Page not found when my submitting my php form

i have this form i built using php the form is supposed to search for a user in formation in my database, but when i fill in the search box and click ok it will try to load process.php instead of the search.php i assigned it to redirect to and for that reason it shows me "PAGE NOT FOUND" *** and for the refrence it is a wordpress plugin that i want to use on my website that i am trying to build
this is the code
add_action( 'admin_menu', 'addAdminMenu' );
function addAdminMenu(){
add_menu_page('ClassifiedBr Settings', 'ClassifiedBr', 'manage_options', 'classifiedbr_settings_page', 'classifiedbr_init', '', 3);
add_submenu_page('classifiedbr_settings_page','Classified Sub', 'Classified Sub','manage_options', 'classified_sub_slug', 'stracking_init');
}
function classifiedbr_init(){
echo '<form action="demo.php" method="post" />';
echo "<h1>Britchi Tracking</h1>";
echo '<p>';
echo 'Costumers Name (required) <br/>';
echo '<input type="text" name="cname" placeholder="Emmanuel John"/>';
echo '</p>';
echo '<p>';
echo 'Tracking number (required) <br/>';
echo '<input type="text" name="ctracking" placeholder="EEX28PDS"/>';
echo '</p>';
echo '<p>';
echo 'Email (required) <br/>';
echo '<input type="email" name="cemail" placeholder="Email"/>';
echo '</p>';
echo '<p>';
echo 'Recived Port (required) <br/>';
echo '<input type="text" name="crport" placeholder="Brazil Port"/>';
echo '</p>';
echo '<p>';
echo 'Delivered Port (required) <br/>';
echo '<input type="text" name="cdport" placeholder="Lagos Port"/>';
echo '</p>';
echo '<p>';
echo 'Current Location (required) <br/>';
echo '<input type="text" name="clocation" placeholder="Abuja" />';
echo '</p>';
echo '<p>';
echo 'Destination (required) <br/>';
echo '<input type="text" name="cdestination" placeholder="Total filling Station, Abuja"/>';
echo '</p>';
echo '<p><input type="submit" name="cregistered" value="Register"></p>';
echo '</form>';
}
function stracking_init(){
echo '<form action="search.php" method="post" />';
echo "<h1>Britchi Tracking</h1>";
echo '<p>';
echo 'Costumers Name (required) <br/>';
echo '<input type="text" name="search" placeholder="Emmanuel John"' . '" size="70"/>';
echo '</p>';
echo '<p><input type="submit" name="csearch" value="Search"></p>';
echo '</form>';
}
add_shortcode('TRACSEARCH', 'stracking_init');
?>
in this code I have 2 forms the first one (classifiedbr_init)sending to demo.php and the second one (strackinh_init) sending to search.php
as you can see the classifiedbr_init and the stracking_init functions will show on the admin page but only the stracking_init will show on the the frontend becouse i created a shortcode for it
this is the search.php code
<table border="">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
<th>RecivedPort</th>
<th>DestinationPort</th>
<th>Location</th>
<th>Destination</th>
</tr>
<?php
$conn=mysqli_connect("localhost","root","","class");
$set=$_POST['search'];
if($set) {
$show="SELECT * FROM demo where fname='$set'";
$result=mysqli_query($conn, $show);
while($rows=mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>";
echo $rows['cname'];
echo "</td>";
echo "<td>";
echo $rows['ctracking'];
echo "</td>";
echo "<td>";
echo $rows['cemail'];
echo "</td>";
echo "<td>";
echo $rows['crport'];
echo "</td>";
echo "<td>";
echo $rows['cdport'];
echo "</td>";
echo "<td>";
echo $rows['clocation'];
echo "</td>";
echo "<td>";
echo $rows['cdestination'];
echo "</td>";
echo "</tr>";
echo "</br>";
}
}
else{
echo "nothing found";
}
?>
</table>
thanks as you help me out

All menu is not displayed

There are 4 sub menus in "menu" button which are drinks, main, appetizers and desserts. When users click drinks, they will be directed to drinks and the same when they go other sub menu buttons. But I want to display all menu when users click "menu" button. I know I have to use "else" but the menu does not display at all.
menu.php
<div class="products">
<?php
if (isset($_GET['id'])) {
//current URL of the Page. basket_update.php redirects back to this URL
$current_url = base64_encode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
$results = $mysqli->query("SELECT * FROM menu WHERE MenuTypeID = ".$_GET['id']);
$currency = '$';
if ($results) {
//fetch results set as object and output HTML
while($obj = $results->fetch_object())
{
echo '<div class="product">';
echo '<form method="post" action="basket_update.php">';
echo '<div class="product-thumb"><img src="'.$obj->MenuPicture.'" /></div>';
echo '<div class="product-content"><h3>'.$obj->MenuName.'</h3>';
echo '<div class="product-desc">'.$obj->MenuDescription.'</div>';
echo '<div class="product-info">';
echo 'Price: '.$currency.$obj->MenuPrice.' | ';
echo 'Qty <input type="text" name="menu_qty" value="1" size="1" />';
echo '<button class="add_to_basket">Add To Basket</button>';
echo '</div></div>';
echo '<input type="hidden" name="MenuID" value="'.$obj->MenuID.'" />';
echo '<input type="hidden" name="type" value="add" />';
echo '<input type="hidden" name="return_url" value="'.$current_url.'" />';
echo '</form>';
echo '</div>';
}
}
?>
<?php
}else{
$query2 = "SELECT * FROM Menu";
$result2 = mysql_query($query2);
}
?>
</div>
Try this code.
<div class="products">
<?php
if (isset($_GET['id'])) {
$results = $mysqli->query("SELECT * FROM menu WHERE MenuTypeID = ".$_GET['id']);
}
else{
$results = $mysqli->query("SELECT * FROM menu");
}
//current URL of the Page. basket_update.php redirects back to this URL
$current_url = base64_encode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
$currency = '$';
if ($results) {
//fetch results set as object and output HTML
while($obj = $results->fetch_object())
{
echo '<div class="product">';
echo '<form method="post" action="basket_update.php">';
echo '<div class="product-thumb"><img src="'.$obj->MenuPicture.'" /></div>';
echo '<div class="product-content"><h3>'.$obj->MenuName.'</h3>';
echo '<div class="product-desc">'.$obj->MenuDescription.'</div>';
echo '<div class="product-info">';
echo 'Price: '.$currency.$obj->MenuPrice.' | ';
echo 'Qty <input type="text" name="menu_qty" value="1" size="1" />';
echo '<button class="add_to_basket">Add To Basket</button>';
echo '</div></div>';
echo '<input type="hidden" name="MenuID" value="'.$obj->MenuID.'" />';
echo '<input type="hidden" name="type" value="add" />';
echo '<input type="hidden" name="return_url" value="'.$current_url.'" />';
echo '</form>';
echo '</div>';
}
}
?>

calculating the sum of an array php

Hi im currently doing making a website that sells games as a project but im having problems calculating the sum for the price of the games
I have this loop which displays the games added into the basket
cart.php
<?php
$count = 0;
while ($count < $numrow)
{
$row = $results -> fetch_assoc();
extract($row);
echo"<div>";
echo"<div class='recommended_games'>";
echo "<img src='images/".$gameIMG."' />";
echo "</div>";
echo '<div class="price_tag">';
echo '<div class="price_tag" name="price" method="POST">£'.$gamePrice. '</div>';
echo'</div>';
echo '<div id="update_form"><form action="updatebasket.php" method="POST" name="updateform">';
echo '<select name="quantity" id="quantity" />';
echo '<option value="1">1</option>';
echo '<option value="2">2</option>';
echo '<option value="3">3</option>';
echo '<option value="4">4</option>';
echo '<option value="5">5</option>';
echo '</select>';
echo '<input type="hidden" value="'.$gameID.'" name="gameid" id="gameid" />';
echo '<input type="submit" value="update" />';
echo '</form>';
echo '<div class="quantity_update">';
echo '<form action="remove_item.php" method="POST">';
echo '<input type="hidden" value="'.$gameID.'" name="gameid" id="gameid" />';
echo '<input type="submit" value="Remove Item" />';
echo '</form>';
echo '</div>';
echo '</div>';
echo"<img class='box1' src='Images/Grey-Banners.png' />";
echo"</div>";
$count = $count + 1;
}
echo '<div id="delete_all">';
echo '<form action="delete_cart.php" method="POST">';
echo '<input id="hide_button" type="submit" value="Clear All" />';
$a=array($gamePrice);
echo array_sum($a);
echo '</form>';
echo '</div>';
?>
this is where im trying to calculate the total price
$a=array($gamePrice);
echo array_sum($a);
The reason this doesnt work, is because $gamePrice never is an array (unless you didnt provide all code). In the loop, it gets set to a new value, after the loop only the last one is stored.
Based on some hints in your code, I guessing this is a cart and you're looping through the cart. An easy way to get a total is like this:
$total = 0;
while( $itemsThatWeLoop){
// Some code here
$total+= $gamePrice*$quantity;
}
You add a variable which increments with the product's price
To explain the while-only-last-value-saved:
$i=0;
while( $i<=10){
$i= $i+1;
}
echo $i;
Will give 10. All other iterations $i gets set to a new value. The original value is not saved

Update the quantity of cart using PHP & SQL

I've this problem in which I can't update the cart for some reason. I've looked for many solutions to see if they can solve my problem but no luck. I've 2 files one called cart.php which contains the form and a updatebasket.php file which contains query.
Cart file
<?php
$count = 0;
while ($count < $numrow)
{
$row = $results -> fetch_assoc();
extract($row);
echo"<div>";
echo"<div class='recommended_games'>";
echo "<img src='images/".$gameIMG."' />";
echo "</div>";
echo '<div class="price_tag">';
echo '<div class="price_tag">£'.$gamePrice. '</div>';
echo'</div>';
echo '<div id="update_form"><form action="updatebasket.php" method="POST" name="updateform">';
echo '<input type="text" value="1" name="quantity" id="quantity" />';
echo '<input type="hidden" value="'.$gameID.'" name='.$gameID.' id="gameid" />';
echo '<input type="submit" value="update" />';
echo '</form>';
echo '</div>';
echo"<img class='box1' src='Images/Grey-Banners.png' />";
echo"</div>";
$count = $count + 1;
}
?>
updatebasket file
<?php
session_start();
require "dbconnect.php";
$memberID = $_SESSION['id'];
$quantity = $_POST['quantity'];
$gameID = $_POST['gameid'];
mysqli_autocommit($con,FALSE);
$connect->query($query);
$query = "UPDATE basket SET quantity = ".$quantity." WHERE gameid = ".$gameID." AND id = ".$memberID."";
$results = $connect->query($query);
mysqli_commit($con);
header('Location: cart.php');
?>
Change cart.php to this:
<?php
$count = 0;
while ($count < $numrow)
{
$row = $results -> fetch_assoc();
extract($row);
echo"<div>";
echo"<div class='recommended_games'>";
echo "<img src='images/".$gameIMG."' />";
echo "</div>";
echo '<div class="price_tag">';
echo '<div class="price_tag">£'.$gamePrice. '</div>';
echo'</div>';
echo '<div id="update_form"><form action="updatebasket.php" method="POST" name="updateform">';
echo '<input type="text" value="1" name="quantity" id="quantity" />';
echo '<input type="hidden" value="'.$gameID.'" name="gameid" id="gameid" />';
echo '<input type="submit" value="update" />';
echo '</form>';
echo '</div>';
echo"<img class='box1' src='Images/Grey-Banners.png' />";
echo"</div>";
$count = $count + 1;
}
?>
The big change is this:
BEFORE:
echo '<input type="hidden" value="'.$gameID.'" name='.$gameID.' id="gameid" />';
AFTER:
echo '<input type="hidden" value="'.$gameID.'" name="gameid" id="gameid" />';
php uses the name attribute as the key for post and get, not id.
id is used by javascript, jquery (also javascript), css, and probably a few other things.
But in forms, name is the one you want for the post and get key.

Function not Inserting Values in wordpress

function name_to_match($nametocheck){
global $wpdb,$namematch,$nomatchfound;
$query="select * from currency";
$namematch=$wpdb->get_col($query,1);
//echo $namematch;
foreach($namematch as $namet){
//echo $name;
if($namet == $nametocheck){
echo "Name Already Exists<br />";
$nomatchfound=0;
}
}
}
function add_signal_form(){
global $wpdb,$insert,$nametocheck;
echo "<br /><br /><br />";
$nametocheck=trim($_POST['name']);
$sign=trim($_POST['sign']);
$status=$_POST['status'];
if(isset($_POST['submit'])){
//..................Function to call if name exist it will not add
name_to_match($nametocheck);
if($nomatchfound ==0){
echo "Match Found";}
else
{
$insert= $wpdb->insert('currency',array('name'=>$nametocheck,'sign'=>$sign,'status'=>$status));
if(!$insert){
echo "Currency Not Added Query Fails";
}
else
{
echo "Currency Successfully Added";
}
}
}
echo '<form name="form1" method="post" action="">';
echo '<div class="label">';
echo '<label for="name">Name</label>';
echo '<div class="field">';
echo '<input type="text" name="name" id="name">';
echo '</div>';
echo '<div class="label">';
echo '<label for="sign">Sign</label>';
echo '</div>';
echo '<div class="field">';
echo '<input type="text" name="sign" id="sign">';
echo '</div>';
echo '<div class="label">';
echo '<label for="status">Status</label>';
echo '</div>';
echo '<div class="field">';
echo '<select name="status" id="status">';
echo '<option value="1">Publish</option>';
echo '<option value="0">Draft</option>';
echo '</select>';
echo '</div>';
echo '<div class="submit">';
echo '<input type="submit" name="submit" id="submit" value="Save Currency">';
echo '</div>';
echo '</p>';
echo '</form>';
/*
echo "<form action='' name=form1\" id=\"form1\" method=\"post\">";
echo '<input type="text" name="text" id="text">';
echo '<input type="submit" name="submit" value="Add New Signal">';
//wp_dropdown_pages();
echo "</form>";
*/}
This function working properly if name exist it return exists but when it not exist nothing happen no else clause fire what is the problem. I am doing something wrong . i want after checking it submit the form and show success message when currency added but nothing happening i try whole the day long and at last i fails in it suggest me
As #akirk suggested, you're never setting $nomatchfound to true or 1 in your name_to_match function.
But there are some other things you could do to make the code clearer and quicker. For example, name_to_match could return a value, instead of setting a global variable. And you could use the database to check for $nametocheck, rather than looping over all the values in the table - it'll make your code shorter and it'll run quicker. So I'd change name_to_match to something like this:
function name_to_match($nametocheck){
global $wpdb;
$query = $wpdb->prepare("select count(*) from currency where name = %s", $nametocheck);
$matching_row_count = $wpdb->get_var($query);
return $matching_row_count;
}
Then the start of your add_signal_form function becomes
function add_signal_form(){
global $wpdb;
echo "<br /><br /><br />";
$nametocheck=trim($_POST['name']);
$sign=trim($_POST['sign']);
$status=$_POST['status'];
if(isset($_POST['submit'])){
$existing_rows = name_to_match($nametocheck);
if($existing_rows != 0){
echo "Match Found";
}
else {
$insert= $wpdb->insert('currency',array('name'=>$nametocheck,'sign'=>$sign,'status'=>$status));
if (!$insert) {
echo "Currency Not Added Query Fails";
}
else {
echo "Currency Successfully Added";
}
}
}

Categories