if statement

Hello, currently working on making a register of my games, and in it i have a “rental” part where i can rent out the games. Now i try connecting the games to the people renting them, so that if its rented, it says who rented it. I tried this php

<?php if($row_utlanregister2['spill_idspill']==$row_Recordset1['idspill']) { echo $row_utlanregister2['fornavn']; } else echo "Not rented" ?>

The idea is that if the gameID from my rental table (utlanregister2), = the gameID from my game table (recordset1), then display the first name of the renter. This php works for me, but it only works for the first rental. So if i have 2 people renting two games, it shows that “Bob” rented game 1, but none rented game2. And if i remove Bob from Game1, then it shows that “Rob” rented game2. It seems like i need to have the if statement work on ALL games, and not only the first one it meets.

Sorry for bad english, and a bad explanation, but i hope you’ll understand.

Post an sql dump of your tables along with a few sample records along with the rest of the code you are using.

Im got close to no experience in coding at all, our course bases of the use of a guide book and dreamweaver inbuilt features. The coding i’ve been doing/looking at is at my own initiative, so im sorry if i seem completely stupid at this topic.

Sql dump i assume is this:
– MySQL Administrator dump 1.4


– Server version 5.1.53-community-log

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT /;
/
!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS /;
/
!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION /;
/
!40101 SET NAMES utf8 */;

/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 /;
/
!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 /;
/
!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE=‘NO_AUTO_VALUE_ON_ZERO’ */;


– Create schema spilldatabase

CREATE DATABASE IF NOT EXISTS spilldatabase;
USE spilldatabase;


– Definition of table platform

DROP TABLE IF EXISTS platform;
CREATE TABLE platform (
idplatform varchar(255) NOT NULL,
PRIMARY KEY (idplatform)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


– Dumping data for table platform

/*!40000 ALTER TABLE platform DISABLE KEYS /;
INSERT INTO platform (idplatform) VALUES
(‘Andre’),
(‘Nintendo Wii’),
(‘PC’),
(‘Playstation 2’),
(‘Playstation 3’),
(‘Playstation 4’),
(‘Xbox 360’),
(‘Xbox One’);
/
!40000 ALTER TABLE platform ENABLE KEYS */;


– Definition of table sjanger

DROP TABLE IF EXISTS sjanger;
CREATE TABLE sjanger (
idsjanger varchar(255) NOT NULL,
PRIMARY KEY (idsjanger)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


– Dumping data for table sjanger

/*!40000 ALTER TABLE sjanger DISABLE KEYS /;
INSERT INTO sjanger (idsjanger) VALUES
(‘Action’),
(‘Andre’),
(‘Familie’),
(‘FPS’),
(‘MMO’),
(‘MOBA’),
(‘RPG’),
(‘RTS’),
(‘Sandbox’);
/
!40000 ALTER TABLE sjanger ENABLE KEYS */;


– Definition of table spill

DROP TABLE IF EXISTS spill;
CREATE TABLE spill (
idspill int(11) NOT NULL AUTO_INCREMENT,
spillnavn varchar(255) NOT NULL,
utgiver varchar(255) NOT NULL,
aldersgrense varchar(255) NOT NULL,
bilde varchar(255) DEFAULT NULL,
sjanger varchar(255) NOT NULL,
platform varchar(255) NOT NULL,
PRIMARY KEY (idspill),
KEY fk_spill_platform (platform),
KEY fk_spill_sjanger1 (sjanger),
CONSTRAINT fk_spill_platform FOREIGN KEY (platform) REFERENCES platform (idplatform) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT fk_spill_sjanger1 FOREIGN KEY (sjanger) REFERENCES sjanger (idsjanger) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1;


– Dumping data for table spill

/*!40000 ALTER TABLE spill DISABLE KEYS /;
INSERT INTO spill (idspill,spillnavn,utgiver,aldersgrense,bilde,sjanger,platform) VALUES
(1,‘World of Warcraft’,‘Blizzard’,‘13’,‘wow.jpg’,‘MMO’,‘PC’),
(2,‘League of Legends’,‘Riot’,‘13’,‘lol.jpg’,‘MOBA’,‘PC’),
(3,‘Minecraft’,‘Mojang’,‘6’,‘minecraft.jpg’,‘Sandbox’,‘PC’),
(4,‘Grand Theft Auto 5’,‘Rockstar’,‘18’,‘gta5.jpg’,‘Sandbox’,‘Xbox 360’),
(6,‘Counter-Strike:Source’,‘Valve’,‘13’,‘css.jpg’,‘FPS’,‘PC’);
/
!40000 ALTER TABLE spill ENABLE KEYS */;


– Definition of table utlan

DROP TABLE IF EXISTS utlan;
CREATE TABLE utlan (
idutlan int(11) NOT NULL AUTO_INCREMENT,
fornavn varchar(255) NOT NULL,
etternavn varchar(255) NOT NULL,
spill_idspill int(11) NOT NULL,
PRIMARY KEY (idutlan),
KEY fk_utlan_spill1 (spill_idspill),
CONSTRAINT fk_utlan_spill1 FOREIGN KEY (spill_idspill) REFERENCES spill (idspill) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=latin1;


– Dumping data for table utlan

/*!40000 ALTER TABLE utlan DISABLE KEYS /;
INSERT INTO utlan (idutlan,fornavn,etternavn,spill_idspill) VALUES
(10,‘Tester’,‘Test’,6),
(11,‘Tester’,‘Test’,6),
(12,‘Tester2’,‘Test2’,4),
(13,‘Tester2’,‘Test2’,4);
/
!40000 ALTER TABLE utlan ENABLE KEYS */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE /;
/
!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS /;
/
!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS /;
/
!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT /;
/
!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS /;
/
!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION /;
/
!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;

[size=8pt][b]The table names etc are in Norwegian however, i hope that doesn’t trouble in understanding. Its basically games, with “genre” “age restriction” “developer” etc. And “Utlan = Rent” and “Lånt av=rented by”. This is where i want the rental function.

My code:[/b][/size]

<?php require_once('Connections/spillkobling.php'); ?> <?php if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { if (PHP_VERSION < 6) { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; } $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? doubleval($theValue) : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } if ((isset($_GET['slettID'])) && ($_GET['slettID'] != "")) { $deleteSQL = sprintf("DELETE FROM spill WHERE idspill=%s", GetSQLValueString($_GET['slettID'], "int")); mysql_select_db($database_spillkobling, $spillkobling); $Result1 = mysql_query($deleteSQL, $spillkobling) or die(mysql_error()); } $maxRows_Recordset1 = 10; $pageNum_Recordset1 = 0; if (isset($_GET['pageNum_Recordset1'])) { $pageNum_Recordset1 = $_GET['pageNum_Recordset1']; } $startRow_Recordset1 = $pageNum_Recordset1 * $maxRows_Recordset1; mysql_select_db($database_spillkobling, $spillkobling); $query_Recordset1 = "SELECT * FROM spill"; $query_limit_Recordset1 = sprintf("%s LIMIT %d, %d", $query_Recordset1, $startRow_Recordset1, $maxRows_Recordset1); $Recordset1 = mysql_query($query_limit_Recordset1, $spillkobling) or die(mysql_error()); $row_Recordset1 = mysql_fetch_assoc($Recordset1); if (isset($_GET['totalRows_Recordset1'])) { $totalRows_Recordset1 = $_GET['totalRows_Recordset1']; } else { $all_Recordset1 = mysql_query($query_Recordset1); $totalRows_Recordset1 = mysql_num_rows($all_Recordset1); } $totalPages_Recordset1 = ceil($totalRows_Recordset1/$maxRows_Recordset1)-1; $maxRows_utlan = 10; $pageNum_utlan = 0; if (isset($_GET['pageNum_utlan'])) { $pageNum_utlan = $_GET['pageNum_utlan']; } $startRow_utlan = $pageNum_utlan * $maxRows_utlan; $colname_utlan = "-1"; if (isset($_GET['oppdaterID'])) { $colname_utlan = $_GET['oppdaterID']; } mysql_select_db($database_spillkobling, $spillkobling); $query_utlan = sprintf("SELECT * FROM utlan WHERE idutlan = %s", GetSQLValueString($colname_utlan, "int")); $query_limit_utlan = sprintf("%s LIMIT %d, %d", $query_utlan, $startRow_utlan, $maxRows_utlan); $utlan = mysql_query($query_limit_utlan, $spillkobling) or die(mysql_error()); $row_utlan = mysql_fetch_assoc($utlan); if (isset($_GET['totalRows_utlan'])) { $totalRows_utlan = $_GET['totalRows_utlan']; } else { $all_utlan = mysql_query($query_utlan); $totalRows_utlan = mysql_num_rows($all_utlan); } $totalPages_utlan = ceil($totalRows_utlan/$maxRows_utlan)-1; mysql_select_db($database_spillkobling, $spillkobling); $query_utlanregister2 = "SELECT platform.idplatform, sjanger.idsjanger, spill.idspill, spill.spillnavn, spill.utgiver, spill.aldersgrense, spill.bilde, spill.sjanger, spill.platform, utlan.idutlan, utlan.fornavn, utlan.etternavn, utlan.spill_idspill FROM spill, platform, sjanger, utlan WHERE utlan.spill_idspill=spill.idspill AND sjanger.idsjanger=spill.sjanger AND platform.idplatform=spill.platform"; $utlanregister2 = mysql_query($query_utlanregister2, $spillkobling) or die(mysql_error()); $row_utlanregister2 = mysql_fetch_assoc($utlanregister2); $totalRows_utlanregister2 = mysql_num_rows($utlanregister2); ?> Untitled Document
<?php do { ?>
Ingen bilde

 <?php echo $row_Recordset1['spillnavn']; ?>


Utgiver: <?php echo $row_Recordset1['utgiver']; ?>
Sjanger: <?php echo $row_Recordset1['sjanger']; ?>
Aldersgrense: <?php echo $row_Recordset1['aldersgrense']; ?>
Platform: <?php echo $row_Recordset1['platform']; ?>
Lånt av: <?php if($row_utlanregister2['spill_idspill']==$row_Recordset1['idspill']) { echo $row_utlanregister2['fornavn']; } else echo "Ingen" ?>




<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
<?php mysql_free_result($Recordset1);

mysql_free_result($utlan);

mysql_free_result($utlanregister2);
?>

You are using deprecated code which basically means, don’t use it. You need to be using PDO. I have provided a download for a complete working PDO database to get you up and running in seconds. Download it, play around with it, and start writing your code in it.

We cannot support deprecated code. We will be more than happy to help you once you update. I will load your database on my end so I can stay in sync with you when you are ready.

http://www.phphelp.com/forum/the-occasional-tutorial/beginners-pdo-bumpstart-code-use-it-now!

Sponsor our Newsletter | Privacy Policy | Terms of Service