Wrong Value showing in echo after request - php

I am trying to make a team name shown from a URL pulling information from my database regarding that team
<?
$query = "select * from teams where
name='".$mysqli->real_escape_string($_REQUEST['name'])."'";
$result = $mysqli->query( $query );
$row = $result->fetch_assoc();
$id = $row['id'];
$name = $row['name'];
$lon = $row['lon'];
$lat = $row['lat'];
$distance = $row['distance'];
$postcode = $row['postcode'];
$phone = $row['phone'];
?>
This worked fine until I put a second team name in the database and now all pages shows that name
the URL is http://domain.com/team.php?name=Test%20TeamA
and its showing Test TeamB and not the required one above
I have checked this on 2 pc's just to make sure its not something wrong with my form i used to put the data into my database or any values hanging about in my browser
why is this doing it?
SQL DUMP
--
-- Table structure for table `teams`
--
CREATE TABLE IF NOT EXISTS `teams` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`lat` varchar(32) NOT NULL,
`lon` varchar(32) NOT NULL,
`distance` varchar(20) NOT NULL,
`postcode` varchar(20) NOT NULL,
`phone` varchar(20) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
--
-- Dumping data for table `teams`
--
INSERT INTO `teams` (`id`, `name`, `lat`, `lon`, `distance`, `postcode`, `phone`) VALUES
(1, 'Test TeamA', '52.483038', '0.178962', '12.9', 'PE15 0JJ', ''),
(3, 'Test TeamB', '52.45645', '0.823423', '12', '', '01231223');

This is not the idea answer for a question like this but please see all the replies under my question
In my case it was a error on another page that i was including on my page
My best advice is to do what NickCoon had commented
echo $query;
to see the query that is being used. then strip all your page down to eliminate the issue

Related

Save and update with uniqid in database

I tried to insert the player with a unique id in the database.
This model works for me, but if I give it a key update it duplicates it in my database.
Option 1
$id = uniqid();
$insert_player_query = mysqli_query($db,"INSERT INTO players (id,nickname,score,time_online,mapname,sid) VALUES ('$id','$player_nickname','$player_score','$player_time','$mapname','$server_id') ON DUPLICATE KEY UPDATE score = score + VALUES(score), time_online = time_online + VALUES(time_online) WHERE id='$id'");
Option 2
Then I tried that, but it doesn't insert anything at all
$id = uniqid();
$sql=mysqli_query($db, "SELECT * FROM players WHERE nickname='$player_nickname'");
if (mysqli_num_rows($sql) > 0)
{
$insert_player_query2 = mysqli_query($db,"UPDATE players (nickname,score,time_online,mapname,sid) VALUES ($player_nickname','$player_score','$player_time','$mapname','$server_id') WHERE id='$id'");
} else {
$insert_player_query = mysqli_query($db,"INSERT INTO players (id,nickname,score,time_online,mapname,sid) VALUES ('$id','$player_nickname','$player_score','$player_time','$mapname','$server_id') WHERE id='$id'");
}
Remove "where" condition. It does not make any sense in "INSERT" statement.
You should check field and use:
CREATE TABLE `players` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`nickname` varchar(100) DEFAULT NULL,
`score` int(11) DEFAULT NULL,
`time_online` varchar(100) DEFAULT NULL,
`mapname` varchar(100) DEFAULT NULL,
`sid` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `uniq` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
INSERT INTO players (id,nickname,score,time_online,mapname,sid) VALUES ('1','player_nickname','1','1','mapname','1')
ON DUPLICATE KEY UPDATE score = score + VALUES(score), time_online = time_online + VALUES(time_online)
Please try it.

Choosing a table to save to based on drop down choice

I have 12 locations that I'm trying to figure out if I should create 12 tables (1 for each community) to save to or 1 table, add the location as a column, and throw everything in it and just get the data I need based on the location row?
CREATE TABLE `location1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`newsTitle` varchar(50) NOT NULL,
`introParagraph` varchar(500) NOT NULL,
`newsLink` varchar(100) NOT NULL,
`downloadLink` varchar(100) NOT NULL,
`file` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=15 ;
You can use a single table for your requirement.
When you need to save the data for locations which is different you can overcome this situation by using php serialize() and unserialize(). When you get data you can do whatever you want after unserializing. You can use the field as text.
CREATE TABLE `location` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`location` varchar(50) NOT NULL,
`location_data` text(32565) NOT NULL
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=15 ;
Here is how you can save data in table
$location = 'Location A';
$location_data = array('langitude' => 32.123 , 'longitude' => 87.3123 , 'status' => 'active');
$serialized = serialize($location_data);
$query = "INSERT INTO location (location , location_data) VALUES ('$location','$serialized')";
mysqli_query($query);
$location = 'Location B';
$location_data = array('test1' => 123 , 'test2' => 321);
$serialized = serialize($location_data);
$query = "INSERT INTO location (location , location_data) VALUES ('$location','$serialized')";
mysqli_query($query);
And when you get result
$query= "SELECT * FROM location WHERE id=1";
$rs = mysqli_query($query);
$row = mysqli_fetch_assoc($rs);
$location_data = unserialize($row['location_data']);
1 table. If the only difference is which community the row belongs to, then definitely 1 table. If there will be other differences, then depends on use as Mike said.

MySQL Select statement not outputting anything

my SQL statement is not outputting anything when run. Just an empty screen.
Here is my PHP code:
<?php
$con = mysqli_connect("localhost", "root", "root","payBills");
$paidBills = "SELECT * FROM houseBills WHERE houseID = '20'";
$resultset = mysqli_query($con, $paidBills);
$records = array();
//Loop through all our records and add them to our array
while ($r = mysqli_fetch_assoc($resultset)) {
$records[] = $r;
}
//Output the data as JSON
echo json_encode($records);
?>
and here is my SQL tables
CREATE TABLE `houseBills` (
`houseBillID` int(11) NOT NULL AUTO_INCREMENT,
`houseID` varchar(11) NOT NULL,
`name` varchar(50) NOT NULL,
`amount` varchar(10) NOT NULL,
`date` varchar(50) NOT NULL,
`addedBy` varchar(100) NOT NULL,
PRIMARY KEY (`houseBillID`),
UNIQUE KEY `houseBillID` (`houseBillID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
--
-- Dumping data for table `houseBills`
--
INSERT INTO `houseBills` (`houseBillID`, `houseID`, `name`, `amount`, `date`, `addedBy`) VALUES
(1, '20', 'Loo roll', '£10', '10', 'samstone920#googlemail.com'),
(2, '20', 'toothpaste', '3', 'egreg', '44tq');
Is there any plainly obvious that I am missing?
Table is currently set as CHARSET=latin1 JSON_ENCODE doesn't except this. See this post: json_encode is returning NULL?. ALTER TABLE houseBills CONVERT TO CHARACTER SET utf8;
Because the table already contains data before the alteration this stills give a problem. In this case (test phase project) the solution is to re-enter data. For large existing table perhaps try copying data to new table might offer a solution. Please note this is untested.

Query from contact table Insert to outbox table

Help me please.
I Want insert from contact table to outbox tabel.
contact:
CREATE TABLE `contacts`
(`id` int(11) NOT NULL auto_increment,
`name` varchar(25) NOT NULL,
`phone` varchar(20) NOT NULL,
`group_name` varchar(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=7;
INSERT INTO `contacts` VALUES (1, 'jala', '+60111', 'friends');
INSERT INTO `contacts` VALUES (2, 'jali', '+60222', 'friends');
INSERT INTO `contacts` VALUES (3, 'jalu', '+60333', 'friends');
INSERT INTO `contacts` VALUES (4, 'kada', '+60444', 'members');
INSERT INTO `contacts` VALUES (5, 'kadi','+60555','members');
INSERT INTO `contacts` VALUES (6, 'kadu', '+60666', 'members');
outbox:
CREATE TABLE `outbox`
(`id` int(11) NOT NULL,
`phone_number` varchar(20) NOT NULL,
`text` varchar(160) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;}
I Want insert to outbox tabel.
insert.php
$sql =mysql_query("SELECT phone
FROM contacts
WHERE group_name = 'member'");
while($data = mysql_fetch_array($sql))
{ $id= $data['id']; $phone= $data['phone'];(phone_number, text) VALUES('$phone','I LOVE YOU') }?>
INSERT INTO outbox (id,phone_number,text)
SELECT id,phone,'I LOVE YOU'
FROM contacts
WHERE group_name = 'member'
You need to use the INSERT...SELECT statement in your query. And also use PDO extention for this.
Example:
<?php
$sqlStatement = "INSERT INTO outbox (id, phone_number, text)
SELECT id, phone, ?
FROM contacts
WHERE group_name = ?";
$text = "I LOVE YOU";
$group_name = "member";
$stmt = $dbh->prepare($sqlStatement);
$stmt->bindParam(1, $text);
$stmt->bindParam(2, $group_name);
$stmt->execute();
?>
Remember to always sanitize your inputs.

Foreign Key not linking correctly

When registering my first user in table 'users' the id is the same value as user_id in the linking table,1, (language). However, when I register another user (id2 in users) the user_id in language is still 1. See SQL:
CREATE TABLE `language` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`native` varchar(30) NOT NULL,
`other` varchar(30) NOT NULL,
`other_list` varchar(9) NOT NULL,
`other_read` varchar(9) NOT NULL,
`other_spokint` varchar(9) NOT NULL,
`other_spokprod` varchar(9) NOT NULL,
`other_writ` varchar(9) NOT NULL,
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`md5_id` varchar(200) NOT NULL,
`full_name` tinytext CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
`user_name` varchar(10) NOT NULL,
`user_email` varchar(30) NOT NULL,
`user_level` tinyint(4) NOT NULL DEFAULT '1',
`pwd` varchar(220) NOT NULL,
`nationality` varchar(30) NOT NULL,
`department` varchar(20) NOT NULL,
`birthday` date NOT NULL,
`date` date NOT NULL DEFAULT '0000-00-00',
`users_ip` varchar(200) NOT NULL,
`activation_code` int(10) NOT NULL DEFAULT '0',
`banned` int(1) NOT NULL,
`ckey` varchar(200) NOT NULL,
`ctime` varchar(220) NOT NULL,
`approved` int(1) NOT NULL DEFAULT '1',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
This is my PHP code:
if(empty($_SESSION['$user_id'])) { // user not logged in; redirect to somewhere else }
if (!empty($_POST['doLanguage']) && $_POST['doLanguage'] == 'Submit')
{
$result = mysql_query("SELECT `id` FROM users WHERE `banned` = '0'") or
die (mysql_error());
list($id) = mysql_fetch_row($result);
session_start();
$_SESSION['user_id']= $id;
sql_insert = "INSERT into `language`
(`user_id`,`native`,`other`,`other_list`,`other_read`, `other_spokint`
,`other_spokprod`,`other_writ` )
VALUES
('$id','$native','$other','$other_list','$other_read','$other_spokint',
'$other_spokprod','$other_writ') ";
mysql_query($sql_insert,$link) or die("Insertion Failed:" . mysql_error());
}
header("Location: myaccount.php?id=' . $_SESSION[user_id] .'");
exit();
Would appreciate any help!
I think this is because your initial select query is selecting all users that are not banned
So need to change the query to filter by the new user:
"SELECT `id` FROM users WHERE `banned` = '0' and id=" . $_SESSION['$user_id'];
The reason why the user_id field kept getting populated as 1 was because mysql_fetch_row was getting the first record which was user id 1.
So if you filter it by the new user, mysql_fetch_row should get the id of the new user.
EDIT
I'm just looking at the code again, and it looks like you do not store the user id in php after you insert it, so your user_id for $_SESSION is null.
So my above example will not work. Instead of the above query, use the following function to get the id of your newly created user. You call this function after you run your insert query. That function will get the new of your newly created user.
mysql_insert_id()
http://php.net/manual/en/function.mysql-insert-id.php
EDIT 2
Ok, since mysql_insert_id() didn't work for you, maybe you can try the following. I just changed your select query to order by id desc.
$result = mysql_query("SELECT `id` FROM users WHERE `banned` = '0' order by id desc")
Just a note, this is probably not the best solution. But on a low traffic website it should be fine. To make the query a bit more accurate, you'd want to search for the user id based on a unique field like a username or email. This would make sure you get the correct id back.
EDIT 3
Here is an example of checking for the user's email. This is just the mysql query, you'll have to adjust this for php. Sorry I'm in class right now.
"SELECT `id` FROM users WHERE `banned` = '0' and email='user#email.com' limit 1

Categories