How to auto complete mysql database with php? - php

how do I automatically fill the mysql database query with php code?
database as follows:
CREATE TABLE IF NOT EXISTS `scrore_table` (
`scores_id` int(11) NOT NULL auto_increment,
`scores_title` varchar(32) collate utf8_bin NOT NULL default '',
`scores` varchar(15) collate utf8_bin NOT NULL default '',
`decimal_place` char(1) collate utf8_bin NOT NULL,
`value` float(15,8) NOT NULL,
PRIMARY KEY (`currency_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1; ;
INSERT INTO `scrore_table` (`scrore_id`, `scrore_title`, `scores`, `decimal_place`, `value`) VALUES
(1, '', 'GameA0', '',),
(2, '', 'GameA1', '',),
(3, '', 'GameA2', '',),
..............................etc
(15, '', 'GameA15', '',);
become the benchmark (key word) are: scores, so
IF found
scores = GameA0
then insert
scores_title = Game For Player A0
decimal_place = 2
value = 1.58030000
IF found
scores = GameA1
then insert
scores_title = Game For Player A1
decimal_place = 2
value = 158.030000
IF found
scores = GameA15
then insert
scores_title = Game For Player A15
decimal_place = 2
value = 0.31303000
etc...
Any suggestions and pointer to help me I appreciate and many thanks

Some tutorials You can download from http://dev.mysql.com/doc/
This example:
if($scores == 'GameA0')
{
$scores_title = 'Game For Player A0';
$value = 1.58030000;
}
elseif($scores == 'Game15')
{
$scores_title = 'Gone ...';
$value = 0.31303000;
}
and the table:
mysql_query("UPDATE score_table SET score_title=".$score_title.", value=".$value." where scores=".$scores."";

Related

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.

values are not inserting

<?php
$ll=mysql_query("CREATE TABLE IF NOT EXISTS p (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL DEFAULT '',
`colum` varchar(255) NOT NULL DEFAULT '',
`ord` varchar(255) NOT NULL DEFAULT '',
`tex` varchar(255) NOT NULL DEFAULT '',
`search` varchar(255) NOT NULL DEFAULT '',
`count` varchar(255) NOT NULL DEFAULT '',
`order` varchar(255) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
)ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;INSERT INTO p (title,colum,ord,tex,search,count,order) VALUES ('$a','$b','$c','$d','$f','$h','$g');") or die(mysql_error()) ;
if($ll){
echo "insert AND CREATE ";}
else {echo "fail"; }
?>
I am working in a php language . In this page if the table is not created 1st create it and then insert the values into the column
After creating the table , I am inserting the values into the table but it showing me the error in the insert query
I am getting a this 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 'INSERT INTO p VALUES ('count','name','asc','1','search','count','order ')' at line 11
what am i doing wrong
I am not absolutly sure if that is the problem but you are passing 7 values in your INSERT statement while your table deffinition has 8 fields.
I assume you are doing that because 'id' field is autoincremental. However, if that is the case, you should specify which columns correspond with your values in the insert statement:
INSERT INTO $p (title, column, ord, ...) VALUES ('$a','$b','$c','$d','$f','$h','$g')
Here is a dry run seperating the two statements - should work:
<?php
$querys[]="CREATE TABLE IF NOT EXISTS $p (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL DEFAULT '',
`colum` varchar(255) NOT NULL DEFAULT '',
`ord` varchar(255) NOT NULL DEFAULT '',
`tex` varchar(255) NOT NULL DEFAULT '',
`search` varchar(255) NOT NULL DEFAULT '',
`count` varchar(255) NOT NULL DEFAULT '',
`order` varchar(255) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
)ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1";
$querys[]="INSERT INTO $p VALUES (null,'$a','{$b}','{$c}','{$d}','{$f}','{$h}','{$g}')";
foreach($querys as $sql) {
$ll = mysql_query($sql);
$error=mysql_error();
if($error!='') {
print $sql."\n";
die($error);
}
}
?>
INSERT INTO p (title,colum,ord,tex,search,count,order) VALUES ('$a','$b','$c','$d','$f','$h','$g')
should be
INSERT INTO p (`title`,`colum`,`ord`,`tex`,`search`,`count`,`order`) VALUES ('$a','$b','$c','$d','$f','$h','$g')
Hence order is a mysql keyword
you have ended the insert statement with a " double quote but not started you can write either
INSERT INTO p (title,colum,ord,tex,search,count,order) VALUES ('$a','$b','$c','$d','$f','$h','$g');
or you can write
("INSERT INTO p (title,colum,ord,tex,search,count,order) VALUES ('$a','$b','$c','$d','$f','$h','$g')");
try it in your code
note - please do not use mysqli and mysql in same code, rather you can use mysqli PDO

PHP MYSQL update table

I'm using MYSQL and php to insert data into my sql.
I have two tables users and userrights.
I'm using checkbox to display rights on user edit page. when rights are de-selected or select i wont to update my userrights table.
CREATE TABLE userrights (
userid int(11) NOT NULL default '0',
right_name varchar(50) collate latin1_general_ci NOT NULL default '',
PRIMARY KEY (userid,right_name)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
INSERT INTO userrights (userid, right_name) VALUES
(1, 'add'),
(1, 'delete'),
(1, 'edit'),
(1, 'view'),
(2, 'add'),
(2, 'delete'),
(2, 'edit'),
(2, 'view');
CREATE TABLE users (
id int(11) NOT NULL,
`name` varchar(100) collate latin1_general_ci default NULL,
email varchar(100) collate latin1_general_ci default NULL,
pass varchar(42) collate latin1_general_ci default NULL,
PRIMARY KEY (id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
INSERT INTO users (id, name, email, pass) VALUES
(1, 'name', 'email#email.com', '5f4dcc3b5aa765d61d8327deb882cf99'),
(2, 'name name', 'email2#email.com', '1a1dc91c907325c69271ddf0c944bc72');
how do I do this?
I've had a similar problem before.
The solution I came up with is truncate the table and reinsert all of the selected entries.
You get the data of the checkbox(true or false)
$rows = mysql_num_rows(mysql_query("SELECT * FROM userrights WHERE id = 'x' and right_name = 'y'"); // Please use insted of mysql_* PDO or MySQLi
if ($rows > 0)
{
if ($checkbox == false)
{
mysql_query("DELETE FROM userrights WHERE id = 'x' AND right_name = 'y'");
}
}
else
{
// Recht nicht vorhanden --> if checkbox == true
if ($checkbox == true)
{
mysql_query("INSERT INTO... VALUES ($id, $right_name)
}
}
First you check if the user had the right. If yes you need to check if the right (data from checkbox) is set to false --> delete that row from the table.
If the user hadn't that right and the checkbox is true, you must insert that in your mysql_table.
The comments must be changed to and if-clause in your code.

Using mysql how do you count the number of successfull ORs?

I basically want to count the number of ors that are successful so that I can tell the relevance of each item to the search query. This is the table structure:
CREATE TABLE `items` (
`id` int(11) NOT NULL auto_increment,
`listing_id` int(11) NOT NULL,
`name` varchar(256) NOT NULL,
`description` varchar(1000) NOT NULL,
`image` varchar(100) NOT NULL,
`image_caption` varchar(100) NOT NULL,
`keywords` varchar(10000) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=90 DEFAULT CHARSET=latin1
and this is an example of a row:
`id` = 1
`listing_id` = 1,
`name` = 'listing test',
`description` = 'listing desc',
`image` = '',
`image_caption` = '',
`keywords` = 'youtube video fun'
And this is what I want the mysql to do in pseudo code:
SELECT id,name,description AND count FROM items WHERE `keywords` LIKE '%video%' AND LIKE '%keyword2%' AND LIKE '%youtube%' AND LIKE '%text%'
And I want to get these results:
`id` = 1
`name` = 'listing test',
`description` = 'listing desc',
`count` = 2
Something like this. (I used "score" instead of "count" because "count" is a SQL function name.)
SELECT id, name, description, (
IF(keywords LIKE '%video%', 1, 0)
+ IF(keywords LIKE '%keyword2%', 1, 0)
+ IF(keywords LIKE '%keyword3%', 1, 0)
) score
FROM table
HAVING score > 0
Note that this query will be quite slow, as it must examine every row.

Advanced many2many query for MYSQL

I im trying to build a imagegallery where people have access to different groups and the groups decide what catalogues and images they are allowed to see.
I though many2many structure would be best for this.
So far, ive manage to build the database like this:
image (image_name, image_file, image_id)
catalog (catalog_id, catalog_name)
supplier (supplier_id, supplier_name)
user (name, user_id)
image2cataloge (image_id, catalog_id)
image2supplier (image_id, supplier_id)
catalog2supplier (catalog_id, supplier_id)
user2supplier (user_id, supplier_id)
So... that been said, saving images and making supplier (or group if you want), adding users to supplier and linking images to supplier and catalogues is no problem. Inserting is no problem.
But selecting the right images based upon the users supplier setting and catalog they are in is harder.
For example, I have a user with user_id 1, which have access to supplier_id 1. supplier_id 1 have access to view catalogue 1, which holds images with image_id 1 and 2.
But supplier_id 1 only have access to image_id 2.
All this settings are stored in the database. How do I do the select query?
This is what i've tested;
//$catalog_id is the catalog_id we are in
//$user_id is the current users user_id
$sql = "SELECT i.*
FROM image i, user u, catalog cat, supplier s, supplier2user s2u, supplier2catalog s2c, image2catalog i2c, image2supplier i2s
WHERE u.id = '".$user_id."'
AND s2u.user_id = '".$user_id."'
AND s2u.supplier_id = s.id
AND s2c.catalog_id = '".$catalog_id."'
AND i2c.catalog_id = '".$catalog_id."'
AND i2s.supplier_id = s.id
AND s2c.supplier_id = s.id
GROUP BY i.id
ORDER BY i.name ASC
But when ive added more than one image, all images are shown for all users in all catalogues.
EDIT (2010/02/05):
Okey, so I've figured out how to at least show correct images in correct catalog. I do this by doing following:
$sql = "SELECT i.*
FROM
image i
INNER JOIN image2catalog i2c
ON i.id = i2c.image_id
AND i2c.catalog_id = '".$pages_level_0[$i]['id']."'
GROUP BY i.id
;";
This let's me output the correct images that belongs in the catalog the user is visiting at the moment. Now I just need to edit this query to filter out all images the user doesn't have access to. I very grateful for any help you can provide!
EDIT 2010/02/09:
---
CREATION SCHEME
CREATE TABLE `user` (
`id` int(11) NOT NULL auto_increment,
`email` varchar(250) NOT NULL,
`password` varchar(50) NOT NULL
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE `imagebank` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(250) character set utf8 NOT NULL default '',
`url` varchar(250) character set utf8 NOT NULL default '',
`childof` varchar(250) character set utf8 NOT NULL default '',
`hide` tinyint(4) NOT NULL,
`publishdate` varchar(14) NOT NULL,
`expiredate` varchar(14) NOT NULL,
`editdate` varchar(14) NOT NULL,
`editbygroup` varchar(250) NOT NULL,
`openby` varchar(250) NOT NULL,
`opendate` varchar(250) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci;
CREATE TABLE `imagebank_image` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(250) NOT NULL,
`img` varchar(250) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE `imagebank_image2catalog` (
`image_id` int(11) NOT NULL,
`catalog_id` int(11) NOT NULL,
PRIMARY KEY (`image_id`,`catalog_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE `imagebank_image2supplier` (
`image_id` int(11) NOT NULL,
`supplier_id` int(11) NOT NULL,
PRIMARY KEY (`image_id`,`supplier_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE `imagebank_supplier` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(250) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE `imagebank_supplier2catalog` (
`supplier_id` int(11) NOT NULL,
`catalog_id` int(11) NOT NULL,
PRIMARY KEY (`supplier_id`,`catalog_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE `imagebank_supplier2user` (
`supplier_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
PRIMARY KEY (`supplier_id`,`user_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
SOME DATA:
INSERT INTO `imagebank` (`id`, `name`, `url`, `childof`, `hide`, `publishdate`, `expiredate`, `editdate`, `editbygroup`, `openby`, `opendate`) VALUES
(1, 'Test 1', 'test-1', '', 0, '20100204230233', '', '', '', '', ''),
(2, 'Test 2', 'test-2', '', 0, '20100204230244', '', '', '', '', '');
INSERT INTO `imagebank_image` (`id`, `name`, `img`) VALUES
(1, 'Test img 1', 'labb_9noq80bik5.jpeg'),
(2, 'Test img 2', 'labb_53626114dz.jpeg');
INSERT INTO `imagebank_image2catalog` (`image_id`, `catalog_id`) VALUES
(1, 1),
(2, 2);
INSERT INTO `imagebank_image2supplier` (`image_id`, `supplier_id`) VALUES
(1, 2),
(2, 1);
INSERT INTO `imagebank_supplier` (`id`, `name`) VALUES
(1, 'Supplier1'),
(2, 'Supplier2'),
(3, 'Supplier3');
INSERT INTO `imagebank_supplier2catalog` (`supplier_id`, `catalog_id`) VALUES
(1, 2),
(2, 1);
INSERT INTO `imagebank_supplier2user` (`supplier_id`, `user_id`) VALUES
(1, 1),
(1, 11),
(1, 12),
(2, 1),
(2, 10),
(3, 1);
INSERT INTO `user` (`id`, `email`, `password`) VALUES
(1, 'User1#test.com', 'ff02dd5s33taa2ba5ff7c2c4d3327e444'),
(10, 'User2#test.com', 'ff02dd5s33taa2ba5ff7c2c4d3327e444'),
(11, 'User3#test.com', 'ff02dd5s33taa2ba5ff7c2c4d3327e444'),
(12, 'User4#test.com', 'ff02dd5s33taa2ba5ff7c2c4d3327e444');
WOW, now thats alot of stuff :P So I know that the tables, specially for "catalogs" which i call just "imagebank" might look abit strange. But I do have my reasons and thats not really the issue :) Its part of an even bigger picture. Hope this helps you to help me. Thanks again.
It looks like if you are passing in the user and catalogue id's then the supplier doesn't matter.
If you required the supplier information in the result, that would be a different matter.
It feels like you shouldn't be involving the user in this query at all as you seem to be looking for the images in a catalogue that are owned by a particular supplier.
If that is the case, then I would drop the requirement for the user id in the query and use the supplier id instead.
I am assuming that the user would have done the following to get to the point where they would be initiating this query:
login - obviously :)
click on 'list suppliers'
click on a supplier
click on a catalog
Either way you are going to have to do a lot of INNER JOIN's. For instance the query to retrieve the list of suppliers for a given user would be something like
SELECT
s.supplier_id,
s.Supplier_name
FROM
supplier s
INNER JOIN
user u
INNER JOIN
user2supplier u2s
ON
u.user_id = u2s.user_id
ON
u2s.supplier_id = s.supplier_id
WHERE
u.user_id = 3 -- for example...
(now, I haven't tested the SQL, but I think that is right...)
Let me know if I'm on the right track - if I have helped, I'd be happy to help some more if I can

Categories