PHP - Issues With If Statement And Mysql Data
Hi, I'm having a slight issue with some coding (see below). It worked a few minutes ago before I add "Packages" & "Safety & Technology" titles to the MySQL database. Hopefully the attached image has worked, but if it hasn't go to www.bikescarsandvans.co.uk/test.php and select the first Audi A3 additional extras drop down menu and you'll see whats going wrong. Code: [Select] $query_title = "SELECT * FROM extras JOIN car_to_extra ON (car_to_extra.extras_id = extras.id) WHERE car_to_extra.car_id = '{$car_row['id']}' ORDER BY extras.price ASC"; $title_results = mysql_query($query_title) or die ("Error in query: $query_title. ".mysql_error()); $current_heading = ''; print "<div class='addtional_extras'>";// ADDED TO TRY TO SORT OUT POSITIONING ISSUE while ($title_row = @ mysql_fetch_array($title_results )) { if ($current_heading != $title_row["title"]) { // The heading has changed from before, so print the new heading here. $current_heading = $title_row["title"]; print " <div class='title_tab2'>" . $title_row["title"] . "</div> "; } ?> <a class='data' href='#' onmouseout='hideTooltip()' onmouseover='showTooltip(event,"<?php print "" . $title_row["info"] . "<br/>(£" . $title_row["price"] . ")"; ?>");return false'> <?php print " <img class='extra_img' src=\"". $title_row["img"] ."\" alt='" . $title_row["img_alt"] . "' /></a> "; }// CLOSES WHILE LOOP ($title_row = @ mysql_fetch_array($title_results )) print "</div>";// CLOSES DIV IMAGE55 Similar TutorialsIt DOES NOT any of this information for the wrestlers that DO NOT have a manager and if they do then it shows all as undefined variables. I know it has to do with the while statement I'm sure it's how its echoing the info and I don't know how to modify it so that I can echo the variable and if there's no value for the row then it shows N/A as its value. function getWrestling($style, $id) { $id=mysql_real_escape_string($id); if ($style=='singles') { $query = "SELECT bio.charactername AS manager, ebw.finisher AS finisher, ebw.setup AS setup, ebw.music AS music FROM efed_bio_wrestling AS ebw JOIN efed_bio AS bio ON ( ebw.manager_id = bio.id) WHERE ebw.bio_id = '$id'"; $result = mysql_query($query) or die(mysql_error()); while ($row = mysql_fetch_array($result)){ ?> <h2>Wrestling</h2> <table class="biotable" cellspacing="10px"> <tr class="biotablerowb"> <td class="biotableheadingb">Manager/Valet:</td> <td class="biotabledatab"><?php if (strlen ($manager) < 1) { print "N/A"; } else { print "$manager";}?></td> </tr> <tr class="biotablerowb"> <td class="biotableheadingb">Finisher:</td> <td class="biotabledatab"><?php if (strlen ($finisher) < 1) { print "N/A"; } else { print "$finisher";}?></td> </tr> <tr class="biotablerowb"> <td class="biotableheadingb">Setup:</td> <td class="biotabledatab"><?php if (strlen ($setup) < 1) { print "N/A"; } else { print "$setup";}?></td> </tr> <tr class="biotablerowb"> <td class="biotableheadingb">Entrance Music:</td> <td class="biotabledatab"><?php if (strlen ($music) < 1) { print "N/A"; } else { print "$music";}?></td> </tr> </table> <?php } }?> Hey there, Now usually after a while I can always solve bugs with my code but this one really has me at a halt here. So basically I have a function that returns true if the accounts username = demo and false if it doesn't but the following if statement returns true and executes the code every time regardless of the returned value of the function like so: Code: [Select] if(isset($_POST['server_start']) or isset($_POST['server_stop']) or isset($_POST['server_restart']) && $Class['Account']->is_demo_account($_SESSION['account_id']) == false) Now if I run the following code right above that: Code: [Select] if($Class['Account']->is_demo_account($_SESSION['account_id']) == false) echo "it isn't a demo account."; else echo "it is a demo account."; It will echo "it is a demo account" correctly. Now can somebody please tell me why the if statement with the post values in always return true. Thanks for your time! Am trying to redirect a user based on their login details. it works fine till it get to a particular department.. "Marine Logistics". when i try to log in with ID of someone in that department, it redirects to invalid login page. the code below. what am i missing?
$sql1 = mysql_query("SELECT * FROM users WHERE username = '$username' AND password = '$password'"); $data = mysql_fetch_array($sql1); $department = $data['department']; if ($department== "Admin") { header("Location: admin/dash_admin.php"); exit; } else if ($department == "ICT" ) { if ($data['position'] == "HOD") { header("Location: ICT/HOD/hod_dash.php"); exit; } else{ header("Location: ICT/staff/staff_dash.php"); exit; } exit; } // check if user is in account department else if ($department == "Account" ) { if ($data['position'] == "HOD") { header("Location: account/HOD/account_dash.php"); exit; } else { header("Location: account/staff/staff_dash.php"); exit; } exit; } //check if user is in Supply chain/ Asset Integrity department else if ($department == "Supply Chain/ Asset Integrity" ) { if ($data['position'] == "HOD") { header("Location: supply_chain/HOD/hod_dash.php"); exit; } else{ header("Location: supply_chain/staff/staff_dash.php"); exit; } exit; } // check if user is in manpower department else if ($department == "Manpower" ) { if ($data['position'] == "HOD") { header("Location: manpower/HOD/hod_dash.php"); exit; } else{ header("Location: manpower/staff/staff_dash.php"); exit; } exit; } // check if user is in Business Development Department else if ($department == "Business Development" ) { if ($data['position'] == "HOD") { header("Location: business_development/HOD/hod_dash.php"); exit; } else{ header("Location: business_development/staff/staff_dash.php"); exit; } exit; } // check if user is in HR else if ($department == "HR" ) { if ($data['position'] == "HOD") { header("Location: HR/HOD/hod_dash.php"); exit; } else{ header("Location: HR/staff/staff_dash.php"); exit; } exit; } //check if user is in Marine Logistics Department else if ($department== "Marine Logistics" ) { if ($data['position'] == "HOD") { header("Location: logistics/HOD/hod_dash.php"); exit; } else{ header("Location: logistics/staff/staff_dash.php"); exit; } exit; } //check if user is from Maintenance Department else if ($department == "Maintenance" ) { if ($data['position'] == "HOD") { header("Location: Maintenance/HOD/hod_dash.php"); exit; } else{ header("Location: Maintenance/staff/staff_dash.php"); exit; } exit; } //check if user is from Admin/services Department else if ($department == "Admin / Services" ) { if ($data['position'] == "HOD") { header("Location: admin_services/HOD/hod_dash.php"); exit; } else{ header("Location: admin_services/staff/staff_dash.php"); exit; } exit; } } else{ header("Location: indexWrongPassOrUser.php"); exit; } Ok, i am adding a few things onto a "php email form" script, everything works as it should apart from this. The php email form, catches the submitted data and emails it accordingly, then after that, i use some of the data and show it, one form value i would like to show is from a radio button. Theres two radio buttons in one group. The radio buttons group name is "postage", value1 = "first" value2 = "second". $post1 is one cost, and $post2 is another cost, both get submitted an received fine. And as it stands when i submit the form without the line below, and tell it to echo "postage", it works fine. IF though i add this line after: if ($postage = "first") {$post_cost = $post1;} elseif ($postage = "second") {$post_cost = $post2;} If simply just comes threw as "first" everytime regardless, Why?? Heres the code for this section: //Collect postage details if(isset($_REQUEST['postage'])){$postage = stripslashes($_REQUEST['postage']);} if(isset($_REQUEST['post1']) && !empty($_REQUEST['post1'])){$post1 = stripslashes($_REQUEST['post1']);} if(isset($_REQUEST['post2']) && !empty($_REQUEST['post2'])){$post2 = stripslashes($_REQUEST['post2']);} if(isset($_REQUEST['Total:']) && !empty($_REQUEST['Total:'])){$total = stripslashes($_REQUEST['Total:']);} //Get disired postage type $post_cost = 0; if ($postage = "first") {$post_cost = $post1;} elseif ($postage = "second") {$post_cost = $post2;} //Work out total to pay $totaltopay = $total+$post_cost; ?> Thanks Andy Here's the code, this is the PHP in my html contacts page (that IS in fact saved with a PHP extension): <div class = "centercontainer"> Hello to all, I have problem figuring out how to properly display data fetched from MySQL database in a HTML table. In the below example I am using two while loops, where the second one is nested inside first one, that check two different expressions fetching data from tables found in a MySQL database. The second expression compares the two tables IDs and after their match it displays the email of the account holder in each column in the HTML table. The main problem is that the 'email' row is displayed properly while its while expression is not nested and alone(meaning the other data is omitted or commented out), but either nested or neighbored to the first while loop, it is displayed horizontally and the other data ('validity', 'valid_from', 'valid_to') is not displayed.'
Can someone help me on this, I guess the problem lies in the while loop? <thead> <tr> <th data-column-id="id" data-type="numeric">ID</th> <th data-column-id="email">Subscriber's Email</th> <th data-column-id="validity">Validity</th> <th data-column-id="valid_from">Valid From</th> <th data-column-id="valid_to">Valid To</th> </tr> </thead> Here is part of the PHP code:
<?php while($row = $stmt->fetch(PDO::FETCH_ASSOC)) { echo ' <tr> <td>'.$row["id"].'</td> '; while ($row1 = $stmt1->fetch(PDO::FETCH_ASSOC)) { echo ' <td>'.$row1["email"].'</td> '; } if($row["validity"] == 1) { echo '<td>'.$row["validity"].' month</td>'; }else{ echo '<td>'.$row["validity"].' months</td>'; } echo ' <td>'.$row["valid_from"].'</td> <td>'.$row["valid_to"].'</td> </tr>'; } ?>
Thank you. This could be PHP or MySql so putting it in PHP forum for now... I have code below (last code listed) which processes a dynamically created Form which could have anywhere from 0 to 6 fields. So I clean all fields whether they were posted or not and then I update the mySQL table. The problem with this code below is that if, say, $cextra was not posted (i.e. it wasnt on the dynamically created form), then this code would enter a blank into the table for $cextra (i.e. if there was already a value in the table for $cextra, it gets overwritten, which is bad). What is the best way to handle this? I'm thinking i have to break my SQL query into a bunch of if/else statements like this... Code: [Select] $sql = "UPDATE cluesanswers SET "; if (isset($_POST['ctext'])){ echo "ctext='$ctext',"; } else { //do nothing } and so on 5 more times.... That seems horribly hackish/inefficient. Is there a better way? Code: [Select] if (isset($_POST['hidden']) && $_POST['hidden'] == "edit") { $cimage=trim(mysql_prep($_POST['cimage'])); $ctext=trim(mysql_prep($_POST['ctext'])); $cextra=trim(mysql_prep($_POST['cextra'])); $atext=trim(mysql_prep($_POST['atext'])); $aextra=trim(mysql_prep($_POST['aextra'])); $aimage=trim(mysql_prep($_POST['aimage'])); //update the answer edits $sql = "UPDATE cluesanswers SET ctext='$ctext', cextra='$cextra', cimage='$cimage', atext='$atext', aextra='$aextra', aimage='$aimage'"; $result = mysql_query($sql, $connection); if (!$result) { die("Database query failed: " . mysql_error()); } else { } hi guys, im new to this forum I'm new also to php, I need help from you guys: I want to display personal information from a certain person (the data is on the mysql database) using his name as a link: example: (index.php) names 1. Bill Gates 2. Mr. nice Guy i want to click Bill Gates (output.php) Name: Bill Gates Country:xxxx Age: xx etc. How can i make this or how to learn this? Hi I am trying to select and order data/numbers from a colum in a mysql data base however i run the code and it returns no value just a blank page no errors or any thing so i think the code is working right but then it returns no result? Please help thanks Here is the code: <?php $host= "XXXXXX"; $mysql_user = "XXXXXX"; $mysql_password = "XXXXXX"; $mysql_database = "XXXXXXX"; $connection = mysql_connect("$host","$mysql_user","$mysql_password") or die ("Unable to connect to MySQL server."); mysql_select_db($mysql_database) or die ("Unable to select requested database."); $row = mysql_fetch_assoc( mysql_query( "SELECT XP FROM Game ORDER BY number DESC LIMIT 1" ) ); $number = mysql_result(mysql_query("SELECT XP FROM Game ORDER BY number DESC LIMIT 1"), 0); echo "The the highest XP is $number"; ?> I have a table with 5 records with the following "id_number" for each record in ASC order: 2, 6, 74, 86,87 There is one other field called "tag_number" and for each record in ASC order here is the data: 50670, 50077, 1234, 1235, 1236 I have a text field and if I search for a spastic "tag_number" I want to return the record but count and display what record like this: Record 3 of 5 The above 3 of 5 means I searched for "1234". I'm not sure what loop I need and how to count the records, any help would be great! Here is what I'm thinking: $id_number = 74; $result = db_query("select * from table order by id_number ASC"); $num_rows = mysql_num_rows($result); //this will provide how many records are in the table $result = db_query("select * from table where id_number = '$id_number' order by id_number ASC"); for ($i = 1; $i == $num_rows; $i = $i + 1) { if ($result['id_number'] == $id_number) { This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=312323.0
In the same $-POST , i wanted to perform update and delete. With the updated and deleted database, I need to select the updated data from the database. However, it tells me: Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in... .I am new to sql in php, is it true that i cannot perform sql in this way? Or is there any suggestion to perform select straight after deleting and updating? CANCEL BOOK RESERVATION if(isset($_POST['cancelres'])) { $query = "DELETE FROM reserved_books WHERE id='$resid';"; //delete reservation $query .="UPDATE reserved_books SET queue = queue - 1 WHERE bookid = '$bookid' AND queue > '$bookqueue';";//update queue number where reservation that queue behind/after/ the current reservation $query_run = mysqli_multi_query($connection, $query); if($query_run) { $_SESSION['success'] = "Reservation Cancelled"; } else { $_SESSION['status'] = " Reservation Not Cancelled"; } $query= "SELECT title FROM books WHERE id = '$bookid';"; //get book title from db $query_run = mysqli_query($connection,$query); if(mysqli_num_rows($query_run)>0) { foreach ($query_run as $row) { $title = $row['title']; } } $bookres = "SELECT * FROM reserved_books WHERE bookid = '$bookid' AND queue = 1"; $bookres_run = mysqli_query($connection,$bookres); foreach($bookres_run as $row) { $res_username = $row['username']; $res_id = $row['id']; } $query= "SELECT option_value FROM settings WHERE option_name = 'email_temp_rescollect'"; //get email template from db $query_run = mysqli_query($connection,$query); if(mysqli_num_rows($query_run)>0) { $row = mysqli_fetch_array($query_run); $rescollect_template = $row[0]; }}
Hello everyone! I'm new here on phpfreaks - Here's my problem. I get "1 <br/> 2" echoed, but not the query results. the connect() function connects and selects a table in a mysql database. Here's the code: Code: [Select] <?php connect(); $query = "SELECT `title`, `body`, `date` FROM `tutorials` ORDER BY `date` DESC" or die ("Query Error"); $counter = 0; if ($query_run = mysql_query($query)) { while ($query_row = mysql_fetch_assoc($query_run)) && ($counter <= 2) { $title = $query_row['title']; $body = $query_row['body']; $date = $query_row['date']; $counter ++; echo $counter; echo "<br/>"; echo $title; echo $body; echo $date; } } ?> If anyone has any idea, please help! - I'm been battling this for a few hours now ive tried to write the $sql in so many ways and this looks the best and its still not working and ive checked the correct syntax but still. this is how i wrote: $sql2 = "UPDATE `tvchaty`.`episodes` SET `showid` = ".($showid).", `epname` = ".($epname).", `season` = ".($season).", `episode` = ".($episode).", `info` = ".($info).", `airdate` = ".($airdate).", `directwatch` = ".($directwatch)." WHERE `episodes`.`id` = ".($id)." LIMIT 1;"; this is the error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Prideses, `season` = 6, `episode` = 11, `info` = , `airdate` = 2010-11-01, `dire' at line 1 what could be the problem? tnx.... Hi Guys I don't know if this is possible but can someone point me in the right direction. I have a php function which takes two inputs and returns an output. for simplicity's sake let's say it's an addition function. What I want to do is use a mysql select statement to show all the rows from a database where field1 and field2 equal '3'. Here's the sort of thing I mean. function addNumbers($one,$two) { return $one + $two; } mysql_query("SELECT * FROM table WHERE 'addNumbers(field1,field2)' = '3'"); What I actually want to do is a lot more complex than this but I am trying to understand how to make the syntax work in simple terms first. Can anybody help? Many Thanks Dan This isn't the entire code just enough to see what I'm trying to do. Everything was working until I added the mysql update query in the if statement. Is this possible or am I doing something wrong? When I run the script it just echos "No results found" twice as $num_results = 2. Code: [Select] <?php include("../includes/connect.php"); $query = "SELECT ........ "; $result = $db->query($query); $num_results = $result->num_rows; if ($num_results == 0) { exit; } else { $i=0; while ($i < $num_results) { $row = $result->fetch_assoc(); $id = $row['id']; if ($expiration_date > $today) { ### EMAIL CODE HERE ### $update = "UPDATE model SET reminder_sent = '1' WHERE id = '$id' "; $result_2 = $db->query($update); $i++; } else { echo "No results found."; $i++; } } } ?> I've decided to move over to using Prepared statements for security purposes, however I'm having problems with the following code. Any help or suggestions would be appreciated Output: Code: [Select] You are Logged In Fatal error: Call to a member function bindParam() on a non-object in [b]xxxxxxx[/b]/login.php on line 34 Code: Code: [Select] <?php include "functions.php"; $db_connection = db_connect(); $db_connection2 = db_connect(); $login_statement = $db_connection->prepare("SELECT COUNT(*) AS accounts FROM `accounts` WHERE `email` = ? AND `password` = ?"); $test_stmt = $db_connection2->prepare("INSERT INTO `test` (`test`) VALUES (:tst)"); login($_POST[email],$_POST[password],$login_statement); log_login($test_stmt); function login($email,$password,$login_statement){ $login_statement->bind_param("ss", $email, $password); $login_statement->bind_result($accounts); $login_statement->execute() or die ("Could not execute statement"); while ($login_statement->fetch()) { if ($accounts==1){ echo "<br/> You are Logged In <br/>"; } else{ echo "<br/>Credentials Invalid<br/>"; } } } function log_login($test_stmt){ $test_stmt->bindParam(':tst', $tst); //< ********LINE 34******* $tst="blah"; $test_stmt->execute() or die ("Could not execute statement"); } ?> Hi everyone, I am currently making a page for a friend to upload a bunch of photos at a time. I was quite pleased that after a bit of googling and trial and error, I figured out how to do this so that multiple records could be added to my table with one submit button. However, my form has 10 browse iconcs. A few tests have revealed that my problem is that if only one picture is uploaded, I still get 9 entries in my database, which I don't want. My question is how can I alter the code so that a row is only populated in the database if an image is uploaded. I guess something that sort of says : if($imgx!="") { populate that row in the table } else { don't } ...and the same for $imgx002 through to $imgx010 The current query is below. Any pointers are much appreciated. Code: [Select] $query = "INSERT INTO photo_uploads (date, photo_name)" . "VALUES (NOW(), '$imgx'), (NOW(), '$imgx002'), (NOW(), '$imgx003'), (NOW(), '$imgx004'), (NOW(), '$imgx005'), (NOW(), '$imgx006'), (NOW(), '$imgx007'), (NOW(), '$imgx008'), (NOW(), '$imgx009'), (NOW(), '$imgx010')"; I'm having two issues:
(1) The correct mysql query for multiple rows
(2) The if/elseif/else to pull data from these rows, and process it based on the row data
I'm only pulling data where A=1 and B=1,2 -- so two possible entries there (call them B1 and B2). I need a php if statement to choose whether the output of B is one of two urls (b1=google,b2=bing). The actual script is far more complex, with more than just 1,2 from B. I've stripped all the excesses down to this one if/else issue and the db query. The output php doesn't matter here. And I can add more elseif once this problem is solved. <?php mysql_connect(localhost, $db_username, $db_password); @mysql_select_db($db_database) or die("No connection"); $query = "Select * FROM table WHERE column='stuff' AND parent='1,2' ORDER BY id DESC LIMIT 10"; $query_result = mysql_query($query); $num_rows = mysql_num_rows($query_result); mysql_close(); ?> <?php for($i=0; $i< $num_rows; $i++){ //start a loop $stuff = mysql_result($query_result, $i, "column"); $row = mysql_fetch_assoc($query_result, $i); if($row['parent'] == 1) { $url = 'http://google.com'; } else { $url = 'http://www.bing.com'; } ?>My own first attempt at a if/then was 500. I got help on another site to redo it (new code shown here), but the new if/else always show the else (Bing). It was also at this time that I learned that "1,2" only showed 1. Hoping that this site is far more friendly than StackOverflow. . Edited by kpmedia, 17 December 2014 - 07:22 PM. Hi... I have query in highlighting null data using this code: Code: [Select] <?php include 'config.php'; $currentEmpID = $_SESSION['empID']; if(!isset($_POST['Regsubmit_'])){ $DATE1 = $_GET['Regfirstinput']; $DATE2 = $_GET['Regsecondinput']; $sql = "SELECT DISTINCT IF(ISNULL(a.LOG_IN), 'rdc', '') AS LOGIN_CLASS, IF(ISNULL(a.LOG_OUT), 'rdc', '') AS LOGOUT_CLASS, a.EMP_ID, CONCAT(LASTNAME, ', ' , FIRSTNAME) AS FULLNAME, a.LOG_IN, a.LOG_OUT FROM $ATTENDANCE.attendance_build AS a JOIN $ADODB_DB.employment em ON (a.EMP_ID = em.EMP_NO AND em.STATUS IN ('Reg Operatives', 'Reg Staff')) WHERE LOG_IN BETWEEN '$DATE1' AND '$DATE2' OR ISNULL(LOG_IN) OR ISNULL(LOG_OUT)"; $DTR = $conn3->GetAll($sql); $smarty->assign('attendance', $DTR); } $smarty->display('header_att.tpl'); $smarty->display('RegAttendance.tpl'); $smarty->display('footer.tpl'); ?> and here is the tpl code: Code: [Select] {section name=att loop=$attendance} <tr> <td colspan="2">{$attendance[att].EMP_ID}</td> <td colspan="2">{$attendance[att].FULLNAME}</td> <td colspan="2" class="{$attendance[att].LOGIN_CLASS}">{$attendance[att].LOG_IN|date_format:"%d-%m-%Y %I:%M %p"}</td> <td colspan="2" class="{$attendance[att].LOGOUT_CLASS}">{$attendance[att].LOG_OUT|date_format:"%d-%m-%Y %I:%M %p"}</td> </tr> {sectionelse} <tr><td colspan="1">No DATA</td></tr> {/section} this code highlight the null value of login or logout or both. this is the css: Code: [Select] .rdc {background-color:#ff0000;} Now, I need to revised my query statement, because i have separate code for adding attendance if the employee has no attendance or no login or no logout. I just want to happen is if the employee is already add his attendance in NRS table or should I said if the LOG_IN in attendance table is equal to TIME_IN in NRS table the data will have a color yellow. For Example: I have this data in attendance table: EMP_ID = 012012 LOG_IN = NULL LOG_OUT = 2011-12-12 13:35:00 I will his attendance in NRS table to have his attendance: EMP_NO = 012012 TIME_IN = 2011-12-12 05:35:00 TIME_OUT = 2011-12-12 13:35:00 In my above query the LOG_IN has a background color of RED. I want to happen is if I add his attendance in NRS the EMP_NO, LOG_IN, LOGOUT will have a color to notice that it is already have in NRS. Because theirs a scenario that the employee has no login or no logout or both. Feel free to ask me if my explanation is not clear to you. Thank you in advance |