I would like to know what code I would need to use to get the values from a row (which would naturally have data from different columns)
I would like to check if the IP address is the same as the Poll ID. As you would expect the IP address is stored in one column, and the poll id in a next column.
$q = mysql_query("SELECT * FROM logs");
while($row = mysql_fetch_array($q))
{
if(($ip = $_SERVER['REMOTE_ADDR']) == $row['ip'])
{
$duplicateIP = true;
}//end if
if(($row['poll_id'] == 1))
{
$duplicatePoll = true;
}//end if
}//end while
I realised this code won't work as it will return the poll id to be true as long as it is equal to 1. How do I ensure that it will only return to be true if that poll id matches the IP address? Thanks.
Instead of:
if(($ip = $_SERVER['REMOTE_ADDR']) == $row['ip'])
{
$duplicateIP = true;
}//end if
if(($row['poll_id'] == 1))
{
$duplicatePoll = true;
}//end if
You should write:
if($_SERVER['REMOTE_ADDR'] == $row['ip'] && $row['poll_id'] == 1){
$duplicateIP = true;
}//end if
Or change your query:
$q = mysql_query(
"SELECT * FROM logs WHERE ip = '".$_SERVER['REMOTE_ADDR'] .
"' and poll_id = '".$iPollid."' "
);
Related
I'm having an issue trying to update my database. I'm able to insert if it doesn't exist, but it doesn't update the record if exists. I think is not finding the current values of the table. Any ideas?
<?php
function add_log($input_output, $l_sales, $l_enroll, $l_offers)
{
global $database;
$date = date('m-d-Y');
$l_sales = safety_filter($l_sales);
$l_enroll = safety_filter($l_enroll);
$l_offers = safety_filter($l_offers);
$cusersup = get_the_current_user('u_manager');
$cuseropm = get_the_current_user('u_opsmanager');
$cuserid = get_the_current_user('id');
$cuser = get_the_current_user('user_name');
if($input_output == 'input')
{
$query_call = mysql_query("SELECT 1 FROM $database->log WHERE l_date='$date' AND l_user_name='$cuser'");
if(mysql_num_rows($query_call) > 0)
{
while($list_calls = mysql_fetch_assoc($query_call))
{
$old_calls = $list_calls['l_calls'];
$old_sales = $list_calls['l_sales'];
$old_enroll = $list_calls['l_enroll'];
$old_offers = $list_calls['l_offers'];
}
$update = mysql_query("UPDATE $database->log SET
l_call=[$old_calls] + [1],
l_sales=[$old_sales] + [$l_sales],
l_enroll=[$old_enroll] + [$l_enroll],
l_offers=[$old_offers] + [$l_offers]
WHERE l_date='$date' AND l
_user_name='$cuser'");
if(mysql_affected_rows() > 0){return true; }
else { if($update == true){ return true; } else { return false; } }
}
else
{
mysql_query("INSERT INTO $database->log (l_date, l_user_name, l_calls, l_sales, l_enroll, l_offers) VALUES ('$date', '$cuser', '1', '$l_sales', '$l_enroll', '$l_offers')");
if(mysql_affected_rows() > 0){return true; }
else{ return false; }
}
}
}
?>
I think the problem is that you are selecting 1 ("SELECT 1 FROM..."), so you are never getting any of the values from the table (just the number "1"). This tells you something is there, but doesn't actually return any values.
Try selecting all fields ("SELECT * FROM..."), or at least list the fields you need to do the update.
my url i have a parameter called uid
in my sql i have
Select * from users Where uid = {$_GET['uid']}
now I have my while loop
while (($row = mysqli_fetch_assoc($result)) != false) {
$uid = $row['uid'];
}
every thing is fine to this point. what i want is if the uid in the database does not equal the $_GET from parameter redirect.
if ($uid == $_GET['uid']) {
return true;
} else {
redirect(ROOT_URI);
}
what i am trying to prevent is modifying uid in the url. that if the uid does not exists it will redirect.
simply you can do like this
$rowcount=mysqli_num_rows($result);
if($rowcount != 0)
{
return true;
}
else
{
redirect(ROOT_URI);
}
since if the uid is in the table mysqli_num_rows doesn't return 0
Use this. I've included some comments as explanation to what I am doing.
$x = 0; //checker
while (($row = mysqli_fetch_assoc($result)) != false) {
if($uid == $row['uid']){
$x = 1; //logic is if there is a match, $x will become 1, else it will stay at 0 value
}
}
//now check the value of $x
if ($x == 1){
//there is a match
return true;
}
else{
//there is no match
redirect(ROOT_URI);
}
To redirect in PHP, you can use the header() function:
header("Location: your_url");
Your sql query is wrong. Try with this one:
Select uid from users Where uid = {$_GET['soldier']}
When a user registers, the script sends an email to verify his account. Clicking on the link, the script gets the token
$token = mysql_real_escape_string($_GET["token"]);
and what I thought to do is
if($token != '') {
mysql_query("UPDATE members SET verified = '' WHERE verified = '$token'");
}
or
if($token != '') {
$result = mysql_query("UPDATE members SET verified = '' WHERE verified = '$token'");
if($result) { }
else { }
}
What is my purpose is to echo a success or failed message on the user. When it will be success then the verified will be empty.
What is the appropriate way of doing this with my examples above?
Should I check if there is the token in the DB before updating it?
Thank you.
Your current method updates a record if it exists but does not take into account that which does not exist or match. You should run something similar to:
if($token != '') {
$result = mysql_query("SELECT COUNT(*) FROM members WHERE verified = '$token'");
while($row = mysql_fetch_row($result)) {
$records = $row[0];
}
if($records == 0) { echo 'no results'; }
elseif($records ==1) { echo 'you matched'; then update the record. }
}
edit
changed BACK to the while loop, wasn't thinking of the count returning 0 rows
I have output from a select query as below
id price valid
1000368 69.95 1
1000369 69.94 0
1000370 69.95 0
now in php I am trying to pass the id 1000369 in function. the funciton can execute only if the valid =1 for id 1000368. if it's not 1 then it will throw error. so if the id passed is 1000370, it will check if valid =1 for 1000369.
how can i check this? I think it is logically possible to do but I am not able to code it i tried using foreach but at the end it always checks the last record 1000370 and so it throws error.
regards
Use a boolean variable:
<?php
$lastValid=false;
while($row = mysql_fetch_array($result))
{
if ($lastValid) {
myFunction();
}
$lastValid = $row['valid'];
}
?>
(Excuse possible errors, have no access to a console at the moment.)
If I understand correctly you want to check the if the previous id is valid.
$prev['valid'] = 0;
foreach($input as $i){
if($prev['valid']){
// Execute function
}
$prev = $i;
}
<?php
$sql = "SELECT * FROM tablename";
$qry = mysql_query($sql);
while($row = mysql_fetch_array($qry))
{
if ($row['valid'] == 1)
{
// do some actions
}
}
?>
I really really recommend walking through some tutorials. This is basic stuff man.
Here is how to request a specific record:
//This is to inspect a specific record
$id = '1000369'; //**some specified value**
$sql = "SELECT * FROM data_tbl WHERE id = $id";
$data = mysql_fetch_assoc(mysql_query($sql));
$valid = $data['valid'];
if ($valid == 1)
//Do this
else
//Do that
And here is how to loop through all the records and check each.
//This is to loop through all of it.
$sql = "SELECT * FROM data_tbl";
$res = mysql_query($sql);
$previous_row = null;
while ($row = mysql_fetch_assoc($res))
{
some_action($row, $previous_row);
$previous_row = $row; //At the end of the call the current row becomes the previous one. This way you can refer to it in the next iteration through the loop
}
function some_action($data, $previous_data)
{
if (!empty($previous_data) && $condition_is_met)
{
//Use previous data
$valid = $previous_data['valid'];
}
else
{
//Use data
$valid = $data['valid'];
}
if ($valid == 1)
{
//Do the valid thing
}
else
{
//Do the not valid thing
}
//Do whatever
}
Here are some links to some good tutorials:
http://www.phpfreaks.com/tutorials
http://php.net/manual/en/tutorial.php
I have a field ,if user enters data it should go and check db if it is present it should redirect to next page.
But here i m not sure whether it is checking the db but mysql query is correct.
$ThirdPartyCategoryName =$_POST['ThirdPartyCategoryName'];
$activate = mysql_query("SELECT * FROM `thirdpartycategorymaster` WHERE `delete` = 'y' ");
if($activate=='y')
{
header("location:catact.php");
}
else
{
//$activate=='NULL';
header("location:tp_home.php");
}
mysql_query returns a ressource that you need to parse with function such as mysel_fetch_array. Your first condition will always be false, you have to parse the ressource before comparing the result contained in it.
$ressource = mysql_query("SELECT * FROM `thirdpartycategorymaster` WHERE `delete` = 'y'");
$firstLine = mysql_fetch_array($ressource);
if ($firstLine && $firstLine['delete'] == 'y') {
// There is an entry with delete = y
} else {
// There is no entry with delete = y
}
This code could also be simplify to just this :
$ressource = mysql_query("SELECT * FROM `thirdpartycategorymaster` WHERE `delete` = 'y'");
if (mysql_fetch_array($ressource)) {
// There is an entry with delete = y
} else {
// There is no entry with delete = y
}
Or also :
$ressource = mysql_query("SELECT * FROM `thirdpartycategorymaster` WHERE `delete` = 'y'");
if (mysql_num_rows($ressource) > 0) {
// There is an entry with delete = y
} else {
// There is no entry with delete = y
}