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";
}
}
}
Related
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
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
}
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
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.
I've been looking through many threads on here without finding a solution to my problem.
I've created a form that is supposed to show content of a database in input boxes, and when i change the content, it should be updated in the database.
No errors, nothing gets changed.
<?php
$con=mysqli_connect("localhost","root","","frontpage");
// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM frontpage_left_links")
or die("Error: ".mysqli_error($con));
while($row = mysqli_fetch_array($result)){
echo '<form action="" method="post">';
echo '<div style="float:left">';
echo '<table border="1" bordercolor="#000000">';
echo '<tr>';
echo '<td>link</td>';
echo '<td><input type="text" name="linkid" value="'.$row['link'].'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td>img</td>';
echo '<td><input type="text" name="imgid" value="'.$row['img'].'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td>tekst</td>';
echo '<td><input type="text" name="imgid" value="'.$row['name'].'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td><input type="submit" id="update" name="gem" value="Gem"</td></td>';
echo '<td><input type="hidden" name="id" value="'.$row['id'].'"></td>';
echo '</tr>';
echo '</table></div>';
echo '<div style="float:left"><center><img src="img/'.$row['img'].'"><br />'.$row['name'].'</center></div>';
echo '</form><br /><br /><br /><br /><br /><br /><br /><br />';
}
if(isset($_POST['update'])){
$id = $_POST['id'];
$link = $_POST['linkid'];
$img = $_POST['imgid'];
$name = $_POST['nameid'];
$sql = mysqli_query("UPDATE frontpage_left_links SET link = '$link', img = '$img', name = '$name' WHERE id = '$id'");
$retval = mysqli_query( $sql, $con );
if(! $retval ){
die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
}
mysqli_close($con);
?>
The form show the database content fine, but nothing happens when changed.
I appreciate any help I can get.
This is what it looks like now.
<?php
$con=mysqli_connect("localhost","root","","frontpage");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(isset($_POST['gem']))
{
$id = $_POST['id'];
$link = $_POST['linkid'];
$img = $_POST['imgid'];
$name = $_POST['nameid'];
$sql = mysqli_query("UPDATE frontpage_left_links SET link = '$link', img = '$img', name = '$name' WHERE id = '$id'");
$retval = mysqli_query( $con, $sql );
if(! $retval )
{
die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
}
$result = mysqli_query($con,"SELECT * FROM frontpage_left_links")
or die("Error: ".mysqli_error($con));
while($row = mysqli_fetch_array($result))
{
echo '<form action="" method="post">';
echo '<div style="float:left">';
echo '<table border="1" bordercolor="#000000">';
echo '<tr>';
echo '<td>link</td>';
echo '<td><input type="text" name="linkid" value="'.$row['link'].'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td>img</td>';
echo '<td><input type="text" name="imgid" value="'.$row['img'].'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td>tekst</td>';
echo '<td><input type="text" name="nameid" value="'.$row['name'].'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td><input type="submit" id="update" name="gem" value="Gem"</td></td>';
echo '<td><input type="hidden" name="id" value="'.$row['id'].'"></td>';
echo '</tr>';
echo '</table></div>';
echo '<div style="float:left"><center><img src="img/'.$row['img'].'"><br />'.$row['name'].'</center></div>';
echo '</form><br /><br /><br /><br /><br /><br /><br /><br />';
}
mysqli_close($con);
?>
Now i get this error.
Warning: mysqli_query() expects at least 2 parameters, 1 given in /Applications/XAMPP/xamppfiles/htdocs/page/admin.php on line 17
Warning: mysqli_query(): Empty query in /Applications/XAMPP/xamppfiles/htdocs/page/admin.php on line 19
Could not update data:
Because your udate is at the end of the page put it above the rest.
And also change isset($_POST['update'] to isset($_POST['gem']
<?php
$con=mysqli_connect("localhost","root","","frontpage");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(isset($_POST['gem']))
{
$id = $_POST['id'];
$link = $_POST['linkid'];
$img = $_POST['imgid'];
$name = $_POST['nameid'];
$sql = "UPDATE frontpage_left_links SET link = '$link', img = '$img', name = '$name' WHERE id = '$id'";
$retval = mysqli_query($con,$sql );
if(! $retval )
{
die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
}
$result = mysqli_query($con,"SELECT * FROM frontpage_left_links")
or die("Error: ".mysqli_error($con));
while($row = mysqli_fetch_array($result))
{
echo '<form action="" method="post">';
echo '<div style="float:left">';
echo '<table border="1" bordercolor="#000000">';
echo '<tr>';
echo '<td>link</td>';
echo '<td><input type="text" name="linkid" value="'.$row['link'].'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td>img</td>';
echo '<td><input type="text" name="imgid" value="'.$row['img'].'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td>tekst</td>';
echo '<td><input type="text" name="imgid" value="'.$row['name'].'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td><input type="submit" id="update" name="gem" value="Gem"</td></td>';
echo '<td><input type="hidden" name="id" value="'.$row['id'].'"></td>';
echo '</tr>';
echo '</table></div>';
echo '<div style="float:left"><center><img src="img/'.$row['img'].'"><br />'.$row['name'].'</center></div>';
echo '</form><br /><br /><br /><br /><br /><br /><br /><br />';
}
mysqli_close($con);
?>
try to replace :
echo '<td><input type="submit" id="update" name="gem" value="Gem"</td></td>';
with :
echo '<td><input type="submit" id="update" name="update" value="Gem"</td></td>';
In your form you are having update button name as gem but you are using if(isset($_POST['update'])) to run update query.
Change it to if(isset($_POST['gem']))
The easiest way would be to simply change the name attribute on your submit to the $_POST[] variable you used. name="update"The easiest way would be to simply change the name attribute on your submit to the $_POST[] variable you used. name="update"
Edit :: Answered already.
echo '<td><input type="submit" id="update" name="update" value="Gem"</td></td>';
Just delete the second td from above!!!