What am I doing wrong with this mySQL table? - php

I am trying to create a table for a blog and I am attempting to store 2 pictures in it so I resorted to storing it with a LONGBLOB (even though I know that everyone recommends storing it to the filesystem and then adding a link to it in the database but the thing is that there will be only one user and that will be me so there is no reason to overcomplicate it because this will not be a large amount of data or big pictures). Here is the database:
CREATE TABLE posts{
id BIGINT(10) NOT NULL AUTO_INCREMENT,
title VARCHAR(10) NOT NULL,
description VARCHAR(30) NOT NULL,
content VARCHAR(MAX) NOT NULL,
picOne LONGBLOB(MAX) NOT NULL,
picTwo LONGBLOB(MAX) NOT NULL,
PRIMARY KEY(id)
);
The error I get is
ERROR 1064 (42000): 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 'MAX) NOT NULL PRIMARY KEY (id) )' at line 5
I think that I am using the word MAX wrong or maybe there is something wrong with my whole data type or perhaps there is a better approach to this.
I appreciate the help.

Its the inappropriate curly brace { that you are using. Use the following query -
CREATE TABLE posts(
id BIGINT(10) NOT NULL AUTO_INCREMENT,
title VARCHAR(10) NOT NULL,
description VARCHAR(30) NOT NULL,
content VARCHAR(MAX) NOT NULL,
picOne LONGBLOB(MAX) NOT NULL,
picTwo LONGBLOB(MAX) NOT NULL,
PRIMARY KEY(id)
);
Perhaps just a typo.

Look at this. These should be the same () when creating a new table.
'CREATE TABLE posts{'
);
CREATE TABLE posts(
id BIGINT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(10) NOT NULL,
description VARCHAR(30) NOT NULL,
content VARCHAR(MAX) NOT NULL,
picOne LONGBLOB(MAX) NOT NULL,
picTwo LONGBLOB(MAX) NOT NULL
);

I replaced VARCHAR(MAX) to TEXT and LONGBLOB(MAX) to LONGBLOB. You can try this:
CREATE TABLE posts(
id BIGINT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(10) NOT NULL,
description VARCHAR(30) NOT NULL,
content TEXT NOT NULL,
picOne LONGBLOB NOT NULL,
picTwo LONGBLOB NOT NULL
);

What I ended up doing was change the VARCHAR value to something really high like VARCHAR(10000) and then leave the LONGBLOB to stand on its own and it worked perfectly. Here is what the code looks like:
CREATE TABLE pages(
id BIGINT(10) NOT NULL AUTO_INCREMENT,
title VARCHAR(10) NOT NULL,
description VARCHAR(30) NOT NULL,
content VARCHAR(10000) NOT NULL,
picOne LONGBLOB NOT NULL,
picTwo LONGBLOB NOT NULL,
PRIMARY KEY(id)
);

Related

Mysql server gone away simple insert query

I am trying to upload video to S3 and then save it to DB, small videos are working fine they are getting saved and insterted to DB successfully but when it comes to large videos say over 10MB (which is not that large) are uploading to S3 but not saving to DB. Please suggest what is going on here. Because everything looks fine and why I'm getting MySQL server has gone away error for such simple query.
ERROR - 2021-05-16 06:46:21 --> Query error: MySQL server has gone away - Invalid query: INSERT INTO `pub_upload_list` (`publisher_id`, `standard_list_id`, `subject_id`, `chapter_id`, `file_name`, `file_short_description`, `file_size`, `file_type`, `file_submission_date`, `file_attributes`, `browse_link`, `file_owner`, `upload_date_time`, `upload_by`) VALUES (1, 1, 2, 14, '01Gan001_004.mp4', 'test 12212', 19, '8', NULL, 1, 'browse', 57, '2021-05-16 06:46:21', 0)
EDIT
Here is complete table structure -
CREATE TABLE `pub_upload_list` (
`upload_id` int(11) NOT NULL AUTO_INCREMENT,
`publisher_id` int(11) NOT NULL,
`standard_list_id` int(6) NOT NULL,
`subject_id` int(11) NOT NULL,
`chapter_id` int(11) NOT NULL,
`file_name` text COLLATE utf8mb4_unicode_ci NOT NULL,
`file_short_description` text COLLATE utf8mb4_unicode_ci NOT NULL,
`file_size` int(11) NOT NULL,
`file_type` text COLLATE utf8mb4_unicode_ci NOT NULL,
`file_submission_date` date DEFAULT NULL,
`file_attributes` int(1) NOT NULL COMMENT '0 = secured, 1 = unsecured',
`browse_link` enum('browse','link') COLLATE utf8mb4_unicode_ci NOT NULL,
`file_owner` int(11) NOT NULL,
`upload_date_time` datetime NOT NULL,
`upload_by` int(11) NOT NULL,
PRIMARY KEY (`upload_id`),
KEY `file_owner` (`file_owner`),
KEY `publisher_id` (`publisher_id`),
CONSTRAINT `pub_upload_list_ibfk_1` FOREIGN KEY (`publisher_id`) REFERENCES `publishers` (`publisher_id`)
) ENGINE=InnoDB AUTO_INCREMENT=61 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

Unknown column in 'field list' php/mySQL but the field DOES exist.

I have this query in PHP which I call from an Android app.
$sql = "SELECT
`asset`.`idasset`,
`asset`.`idlocation`,
`asset`.`asset_barcode`,
`asset`.`asset_number`,
`asset`.`category_name`,
`asset`.`make`,
`asset`.`model`,
`asset`.`serial_number`,
`asset`.`iduser`,
`asset`.`idcost_centre`,
`asset`.`idcondition`,
`asset`.`idstatus`,
`asset`.`latitude`,
`asset`.`longitude`,
`asset`.`asset_description`
from `asset`";
This is the response I receive from the call in Android Studio console:
D/RAW RESPONSE: Notice: Unknown column 'asset.idlocation' in 'field list'
But that field does exist.
Here's the full create statement I reverse copied to clipboard in MySQLWorkbench.
CREATE TABLE `asset` (
`idasset` int(11) NOT NULL AUTO_INCREMENT,
`idlocation` int(11) NOT NULL,
`asset_barcode` varchar(50) DEFAULT NULL,
`asset_number` varchar(50) DEFAULT NULL,
`category_name` varchar(50) DEFAULT NULL,
`asset_description` varchar(250) DEFAULT NULL,
`make` varchar(50) DEFAULT NULL,
`model` varchar(50) DEFAULT NULL,
`serial_number` varchar(255) DEFAULT NULL,
`iduser` int(11) DEFAULT NULL,
`idcost_centre` int(11) DEFAULT NULL,
`idcondition` int(11) DEFAULT NULL,
`idstatus` int(11) DEFAULT NULL,
`longitude` varchar(45) DEFAULT NULL,
`latitude` varchar(45) DEFAULT NULL,
PRIMARY KEY (`idasset`),
KEY `cost_center_fk_idx` (`idcost_centre`),
KEY `iduser_fk_idx` (`iduser`),
KEY `category_name_fk_idx` (`category_name`),
CONSTRAINT `category_name_fk` FOREIGN KEY (`category_name`) REFERENCES `category` (`category_name`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `iduser_fk` FOREIGN KEY (`iduser`) REFERENCES `user` (`iduser`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=93 DEFAULT CHARSET=utf8;
If I run that exact raw query in PHPMyAdmin or MySQLWorkbench it works, but not in PHP.
If I swap around the sequence of the fields I also get the same error with:
`asset`.`asset_description`
`asset`.`latitude`,
`asset`.`longitude`,
But all the other fields are fine.
Strange thing is this query worked a few weeks ago.
Am I missing something?
I don't have any ideas.
Starting to think its a PHP or MySQL setup or something.
Any help would be greatly appreciated as time is not on my side.
Sorry silly mistake, I did point to another DB.

Dynamic table using parameter or variable in table name cakephp

I am creating a dynamic table using mysql query table name should be unique that's why i have added user id to table name.
In Controller:
$last_id = $this->User->getLastInsertID();
//$table_name = 'hello_'.$last_id.'_tutors';
// debug($table_name);
$this->User->query("CREATE TABLE IF NOT EXISTS `post_`.$last_id.`_tutors` (
`id` int(8) NOT NULL AUTO_INCREMENT,
`user_id` int(8) NOT NULL,
`tutor_name` varchar(50) NOT NULL,
`tutor_email` varchar(50) NOT NULL,
`tutor_number` varchar(50) NOT NULL,
`tutor_gender` varchar(50) NOT NULL,
`tutor_address` varchar(100) NOT NULL,
`city_id` int(11) NOT NULL,
`area_id` int(11) NOT NULL,
`matric` varchar(200) NOT NULL,
`inter` varchar(200) NOT NULL,
`graduation` varchar(200) NOT NULL,
`masters` varchar(200) NOT NULL,
`diploma` varchar(200) NOT NULL,
`other_education` varchar(200) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=67 ;
");
it gave syntax error like:
Syntax error or access violation: 1064 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 '.75._tutors ( tutor_name
varchar(50) NOT NULL, tutor_email var' at line 1
if anyone may help. Thanks in advance.
Try using
$this->User->query("CREATE TABLE IF NOT EXISTS `post_".$last_id."_tutors` (

How do I tell the difference between files of the same name in the same directory?

I am building a music uploading system in PHP. I want the user to be able to upload a song to the server and of course be able to access it later. I have been informed that storing the mp3 files in a server directory is more efficient than storing it in a database. I have set up tables for the user account, the mp3 files and for albums like so
create table users (
user_id int(11) NOT NULL AUTO_INCREMENT,
user_name varchar(64) NOT NULL,
user_firstname varchar(64) NOT NULL,
user_lastname varchar(64) NOT NULL,
user_email varchar(64) NOT NULL,
user_registration_date datetime NOT NULL,
PRIMARY KEY (user_id),
UNIQUE KEY (user_name),
UNIQUE KEY (user_email)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE tracks (
user_id INT(11) NOT NULL,
track_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
album_id INT UNSIGNED NOT NULL,
track_name varchar(64) NOT NULL,
track_file_name varchar(64) NOT NULL,
track_artist varchar(20) NOT NULL,
tag1 varchar(20) DEFAULT NULL,
tag2 varchar(20) DEFAULT NULL,
tag3 varchar(20) DEFAULT NULL,
genre varchar(20) DEFAULT NULL,
sub_genre varchar(20) DEFAULT NULL,
full_length ENUM('Yes', 'No') NOT NULL,
description TEXT,
PRIMARY KEY (track_id),
INDEX (track_id),
INDEX (track_name)
) ENGINE = INNODB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE albums (
user_id INT(11) NOT NULL,
album_name varchar(64) NOT NULL,
album_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
album_artist varchar(20) NOT NULL,
album_art_file_name varchar(64),
INDEX (album_id),
INDEX (album_name)
) ENGINE = INNODB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
This is where I'm stuck, I can store the files in a directory and have a file path stored in the database (note: I only store the name of the file in my table for versatility in the future, like if I need to change the path where the files are stored) but what if someone uploads a file with the same name? Should I rename the file to include its ID? Is that even safe to do since it'll be show in the URL on request? Is there a better way, am I going about this horribly wrong? Thank you for your help.
You can try by appending the Unix time stamp at the end of the file name.
like this you can use:
<?php
$date = new DateTime();
$val= $date->getTimestamp();// it will give the unix time stamp
?>
append $val to the end of your file.

Audit Table using Triggers

DROP TABLE IF EXISTS `actividades`.`act_actividad_audit`;
CREATE TABLE `actividades`.`act_actividad_audit` (
`fe_creacion` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP,
`usr_digitador` char(10) NOT NULL,
`ip_digitador` char(15) NOT NULL,
`id_act_actividad` int(10) unsigned NOT NULL,
`titulo` char(64) NOT NULL,
`act_prioridad_id` int(10) unsigned NOT NULL,
`act_motivo_id` int(10) unsigned NOT NULL,
`detalle` text,
`detalle_tecnico` text,
`hostname_id` int(10) unsigned NOT NULL,
`hostname_nombre` char(50) NOT NULL,
`es_SMOP` tinyint(1) NOT NULL,
`url_SMOP` text,
`es_tecnico` tinyint(1) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='Auditoria Actividad General';
I want to populate that audit table with a trigger but how can i send or fill the values for usr_digitador or ip_digitador if that values are on client side.? please help
Usually the user's IP address will be sent as part of the standard HTTP header information. Certainly I've seen it in IIS logs so it's there. I believe it's in the REMOTE_ADDR server variable.

Categories