SQL unable to insert data into database - php

My SQL code is not inserting in the table. Does someone know what i do wrong?
SQL/PHP Code:
$ownerid = $input->FilterText($_GET['ownerId']);
$widgetid = $input->FilterText($_GET['ratingId']);
$rate = $input->FilterText($_GET['givenRate']);
}
if(is_numeric($ownerid) && is_numeric($widgetid) && is_numeric($rate)){
$myvote = $db->result($db->query("SELECT COUNT(*) FROM ".PREFIX."ratings WHERE raterid = '".$user->id."' AND userid = '".$ownerid."'"));
if($myvote < 1 && $ownerid != $user->id && $rate > 0 && $rate < 6){
$db->query("INSERT INTO cms_ratings (userid,rating,raterid) VALUES ('".$ownerid."','".$rate."','".$user->id."')");
}
Hope someone can help me with this problem. Its not inserting # database.

Related

Multiple tinyint when insert data into MySql database

I have a php code which insert questions and answers for a quiz into a mysql database and for each answer a specifically tinyint number. With this method I can choose which one is the correct answer by allocating the number 1 to it and 0 to the rest of them. What I want is to can allocate to the wrong answers the numbers 2, 3 and 4, not 0 to all. I hope you understood. My English is not so good. Thank you very much !
My code:
// Question query
$query = "INSERT INTO `questions`(question_number, text)
VALUES('$question_number', '$question_text')";
// Run query
$insert_row = $mysqli->query($query) or die($mysqli->error.__LINE__);
// Validate insert
if ($insert_row) {
foreach ($choices as $choice => $choice_text) {
if($choice_text != '') {
if($correct_choice == $choice) {
$is_correct = 1;
} else{
$is_correct = 0;
}
Why don't you specify the wrong answer when it's not correct?
if($choice_text != '') {
if($correct_choice == $choice) {
$is_correct = 1;
} else{
$is_correct = $choice;
}

I can't access the custom database table in WordPress

I am trying to make a rating system to my posts. First I have created a custom table that will contain post_id, post_title_, user ip, user machine, post rate, username, user id, timestamp.
Until now all is fine and I can insert data without any issue into this table. Next I am trying to create a function to verify if the user has already voted, to not allow user to rate more than one time per post, so created this function
function alreadyvoted($post_id) {
// variables
global $wpdb;
$table_name = $wpdb - > prefix.
"ratings_fansub";
$ip = $_SERVER['REMOTE_ADDR'];
$rate_userid = get_current_user_id();
$rate_user_machine = #gethostbyaddr($ip);
// getting data from databaase by current post_id
$results = $wpdb - > get_results('SELECT * FROM $table_name WHERE ratings_fansub_postid = $post_id ');
$NumRows = count($results);
$RandNum = rand(0, $NumRows);
$i = 0;
while ($i < $RandNum):
if ($rate_userid != 0) {
if ($rate_userid == $results[$RandNum] - > ratings_fansub_username)
return 1;
} else {
if ($rate_user_machine == $results[$RandNum] - > ratings_fansub_host or $ip == $results[$RandNum] - > ratings_fansub_ip)
return 1;
}
$i++;
endwhile;
}
But this function does not work, I believe I have made a mistake somewhere in the sql line but I can't figure out what it is.
Please check your table name in database. Table name like "wp_ratings_fansub" or not.
OR you can also print table name 'echo $wpdb - > prefix."ratings_fansub";exit();'
and check database table name is match or not.
i have fix it,
function alreadyvoted($post_id) {
// variables
global $wpdb;
$table_name = $wpdb - > prefix.
"ratings_fansub";
$ip = $_SERVER['REMOTE_ADDR'];
$rate_userid = get_current_user_id();
$rate_user_machine = #gethostbyaddr($ip);
// getting data from databaase by current post_id
$results = $wpdb - > get_results('SELECT * FROM wp_ratings_fansub WHERE ratings_fansub_postid ='.$post_id);
$NumRows = count($results);
$i = 0;
while ($i < $NumRows):
if ($rate_userid != 0) {
if ($results[$i] - > ratings_fansub_userid == $rate_userid)
return true;
} else {
if ($results[$i] - > ratings_fansub_host == $rate_user_machine or $results[$i] - > ratings_fansub_ip == $ip)
return true;
}
$i++;
endwhile;
return false;
} //end of function alreadyvoted

issue in form validation in php?

I have a form with several fields and with 4 radio buttons. Based on this radio button click, the form has to be validated. All the fields in the form need not be validated.
Check this link, the similar thing I want:
https://stackoverflow.com/questions/30396119/i-have-issue-in-php-validation-for-nested-if-else-condition
The code i tried:
<?php
$guestname="";$validguestname="";
$guestno= "";$validguestno="";
$nochild= 0;$validnochild="";
$noadults=0;$validnoadults="";
$infants =0;$validinfants="";
$noofperson ="";$validnoofperson="";
$check_in="";$validdate1="";
$check_out="";$validdate2="";
$noofnight = "";$validnoofnight="";
$room_type="";$validroom_type="";
$nfroom="";;$validnfroom="";
$noofextrabed ="";$validnoofextrabed="";
$roomarray="";$validroomarray="";
$mealplan="";$validmealplan="";
$rateperday ="";$validrateperday="";
$rateperbed ="";$validrateperbed="";
$total_amount ="";$validtotal_amount="";
$advance = "";$validadvance="";
$balance ="";$validbalance="";
$receiptno ="";$validreceiptno="";
$dateofremitance ="";$validdateofremitance="";
$bankorcash="";$validbankorcash="";
$bank_name="";$validbank_name="";
$bookedby="";$validbookedby="";
$paymentmode ="";$validpaymentmode="";
$bookingtype="";$validbookingtype="";
$reservationstatus="";$validreservationstatus="";
$book_by="" ;$validbook_by="";
$agentname= "";$validagentname="";
$agentphoneno="";$validagentphoneno="";
$error_message="";$error_message1="";$error_message2="";
$date3=date('Y-m-d');
if(isset( $_POST['submit']))
{
$guestname= $_POST['guest_name'];
$bookingtype=$_POST['booking_type'];echo $bookingtype."helooo";
$guestno= $_POST['guest_no'];
$reservationstatus=$_POST['reservation_status'];
$nochild= $_POST['no_child'];
$book_by= $_POST['bookby'];
$agentname= $_POST['bookbyother'];
$agentphoneno= $_POST['phone1'];
$noadults= $_POST['no_adults'];
$infants = $_POST['no_infants'];
$noofperson = $nochild+$noadults+$infants;
$check_in=trim( $_POST['checkin']);
$check_out=trim( $_POST['checkout']);
$noofnight = $_POST['no_of_days'];
$room_type= $_POST['roomtype'];
$nfroom= $_POST['noofroom'];
$noofextrabed = $_POST['no_of_extra_bed'];
$roomarray= $_POST["room_no"];
$mealplan= $_POST['meal_plan'];
$rateperday = $_POST['rp_day'];
$rateperbed = $_POST['extrarate'];
$total_amount = $_POST['totalamount'];
$advance = $_POST['advance_amount'];
$balance = $_POST['val_username'];
$receiptno = $_POST['receipt_no'];
$dateofremitance = $_POST['date_remitance'];
$bankorcash= $_POST['paymentmode'];
$bank_name= $_POST['bankname'];
$booking_date=date('y-m-d');
$bookedby= $_POST['adminbooking'];
$paymentmode = $_POST['payment_mode'];
$date1 = date('Y-m-d', strtotime($check_in));
$date2 = date('Y-m-d', strtotime($check_out));
///1st condition///
if($bookingtype=="direct" && $reservationstatus=="H" && other_conditions)
{
insert query;
}
///2nd condition///
else if($bookingtype=="direct" && $reservationstatus=="C" && othercondition for particular fields)
{insert query;}
///3rd condition///
else if($bookingtype=="agent" && $reservationstatus=="H" && $book_by=="other")
{echo "agent anfd holding and other";}
///4th condition///
else if($bookingtype=="agent" && $reservationstatus=="H" && $book_by!="other")
{echo "agent anfd holding and not toher";}
///5th condition///
else if($bookingtype=="agent" && $reservationstatus=="C" && $book_by=="other")
{echo "agent anfd confirm and other";}
///6th condition///
else if($bookingtype=="agent" && $reservationstatus=="C" && $book_by!="other")
{echo "agent anfd confirm and not other";}
//else condiition
else
{echo "choose ur option";}
From what I gather, your problem seems to be figuring out the full range of values that have been entered on the first. You can try one of two approaches.
Get your form values default options if they have not been entered by the user. As an example:
if (!empty($_POST['')) {
$reservationstatus=$_POST['reservation_status'];
}
else {
$reservationstatus=false;
}
then your following query will work:
if($bookingtype=="direct" && $reservationstatus=="H" && other_conditions)
{
your_query_here();
}
Check explicitly for entered values, but this makes for a more complex check.
if( ((!empty($_POST['booking_type']) &&
($_POST['booking_type']=="direct") ) &&
((!empty($_POST['reservation_status']) &&
($_POST['reservation_status']=="H") ) &&
other_conditions)
{
your_query_here();
}

mysql where condition not working?

I have written my php code in codeigniter but when I set $limit = 1, still i get records that have seen = 1 why??
i have written a mysql query in pure sql but the problem remains !
here is my code :
function getComment($limit)
{
while( !$this->checkFuncState('getComment') );
$this->lockFunc('getComment');
$grade = $this->session->userdata('grade');
$this->db->select('*');
$this->db->select('m_comments.id AS `ComId`');
$this->db->select('m_comments.approved AS `Comappr`');
$this->db->select('m_comments.content AS `ComContent`');
$this->db->select('m_users.hid AS `UserHid`');
$this->db->from('m_comments');
$this->db->join('m_users','m_comments.user_id = m_users.id');
if( $grade == "0" || $grade == "1" )
$this->db->where("( m_comments.approved = 0 || m_comments.approved = -15 )");
else
$this->db->where('m_comments.approved','0');
$this->db->where('m_comments.seen','0');
$this->db->limit($limit);
$q = $this->db->get();
$this->setComsSeen($q);
$this->unlockFunc('getComment');
return $q;
}

Php : Logical query with some complications

I have basic knowledge to write codes in php but I'm stucked in logical process. Hope I can get some help here.
Problem :
I want to compare and declare winner between 2 workers based on performance. Details are given below :
$working_first = 2;
$available_canches = 5;
$target_minutes = 40;
Details of Worker1
$worker1 = 2 ;
$worker1_chances_taken = 4;
$worker1_points = 80;
$worker1_minutes = 40;
Details of Worker2
$worker2 = 5 ;
$worker2_chances_taken = 5;
$worker2_points = 95;
$worker2_minutes = 35;
Expected Solution:
I want to run some process...
if $working_first == $worker1 AND $target_minutes>$worker1_minutes OR $available_chances>$worker1_chances_taken
OR
if $working_first != $worker1 AND $worker1_points>$worker2_points AND $target_minutes>$worker2_minutes OR $available_chances>$worker2_chances_taken
Syntax I'm Using For Output:
if (($working_first==$worker1 && ($target_minutes>$worker1_minutes || $available_chances>$worker1_chances_taken)) || ($working_first != $worker1 && $worker1_points>$worker2_points && ($target_minutes>$worker2_minutes || $available_chances>$worker2_chances_taken)) {
echo 'Process Continues';
}
else {
echo 'Process Ends';
}
I'm unable to get desired output.
Please help me to get it.
Thanks.
You haven't closed the brackets correctly. Try with this:
if (
(
$working_first == $worker1
&&
(
$target_minutes > $worker1_minutes
||
$available_chances > $worker1_chances_taken
)
)
||
(
$working_first != $worker1
&&
$worker1_points > $worker2_points
&&
(
$target_minutes > $worker2_minutes
||
$available_chances > $worker2_chances_taken
)
)
) {
echo 'Process Continues';
} else {
echo 'Process Ends';
}

Categories