PHP Not Executing Code To Create Database - sql

I wrote the following code and I'm trying to make an install to create the databases and tables for the SQL but I can't figure out why it's not creating the databases or the tables.
CONFIG.PHP
<?php
// Database Details
$host = 'localhost';
$user = 'root';
$password = '';
//Install Settings
$root_username = 'root';
$root_password = 'password';
// Install Settings Advanced - Do Not Modify For Default Settings
$database = 'Sims2';
?>
INSTALL.PHP
<?php
include 'config.php';
// Remove any instance of .mysqli_connect_error() before publishing
$conn = mysqli_connect($host,$user,$password);
if (!$conn) {die("Connection failed: ".mysqli_connect_error());};
$sql = "CREATE DATABASE Sims2";
if (!$conn){die("Error creating database: ".mysqli_connect_error());};
$sql = "CREATE TABLE TEACHERS (
id INT(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
uid VARCHAR(128) NOT NULL,
pwd VARCHAR(128) NOT NULL
);";
if(!$conn){die("Error creating table: ".mysqli_connect_error());};
$sql = "CREATE TABLE STUDENTS (
id INT(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
first VARCHAR(64) NOT NULL,
last VARCHAR(64) NOT NULL,
gender VARCHAR(6) NOT NULL,
dob VARCHAR(10) NOT NULL,
semail VARCHAR(64) NOT NULL,
address VARCHAR(120) NOT NULL,
phone VARCHAR(15) NOT NULL,
tutor VARCHAR(64) NOT NULL
);";
if(!$conn){die("Error creating table: ".mysqli_connect_error());};
$sql = "INSERT INTO TEACHERS (uid, pwd) VALUES ($root_username,$root_password)";
if(!$conn){die("Error creating root account: ".mysqli_connect_error());};
echo "Done";
?>
Any help would be appreciated

Related

The command dal:create:schema does not create foreign keys

Given the following example for an Entity-Definition, there is a foreign key defined. As a developer and database engineer i would expect that the command dal:create:schema would also create the expected foreign keys. But this is not the case.
return new FieldCollection([
(new IdField('id', 'id'))->addFlags(new PrimaryKey(), new Required()),
(new LongTextField('comment', 'name'))->addFlags(new Required()),
(new FkField('order_id', 'orderId', OrderDefinition::class))->addFlags(new Required()),
new OneToOneAssociationField('order', 'order_id', 'id', OrderDefinition::class, false),
new CreatedAtField(),
new UpdatedAtField()
]);
Instead this is the result:
CREATE TABLE `order_refund` (
`id` BINARY(16) NOT NULL,
`comment` LONGTEXT NOT NULL,
`order_id` BINARY(16) NOT NULL,
`created_at` DATETIME(3) NOT NULL,
`updated_at` DATETIME(3) NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
However, it seems like that ManyToOneAssociations will add foreign keys. Is there something missing in the entity definition?
The command you mentioned is using the SchemaGenerator which has a method to generate Foreign keys:
\Shopware\Core\Framework\DataAbstractionLayer\SchemaGenerator::generateForeignKeys
Looking at this method it seems to work only fields of the type ManyToOneAssociationField
private function generateForeignKeys(EntityDefinition $definition): string
{
$fields = $definition->getFields()->filter(
function (Field $field) {
if (!$field instanceof ManyToOneAssociationField) {
return false;
}
return true;
}
);
I also think it is a shortcoming of this function that it does not generate foreign keys for fields of the type OneToOneAssociationField. Maybe you can try to adjust this filtering and see if it works and make a pull request on GitHub for the benefit of yourself and other developers?

PhoneGap sql checking for duplicates

I want to input a query to check the database for duplicate when inserting data into the database so it would prevent the activity Name from being entered more than once in a database
function insertQueryDB(tx) {
var myDB = window.openDatabase("test", "1.0", "Test DB", 1000000);
tx.executeSql('CREATE TABLE IF NOT EXISTS dataEntryTb (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, activityName TEXT NOT NULL, location TEXT NOT NULL, time NOT NULL, date NOT NULL, reporter NOT NULL)');
var an = document.forms["myForm"]["activityName"].value;
var l = document.forms["myForm"]["location"].value;
var t = document.forms["myForm"]["time"].value;
var d = document.forms["myForm"]["date"].value;
var r = document.forms["myForm"]["reporter"].value;
var query = 'INSERT INTO dataEntryTb ( activityName, location, time, date, reporter) VALUES ( "'+an+'", "'+l+'", "'+t+'", "'+d+'", "'+r+'")';
navigator.notification.alert("Retrieved the following: Activity Name="+an+" and Location="+l);
tx.executeSql(query,[]);
}``
Create the table with name being unique:
CREATE TABLE IF NOT EXISTS dataEntryTb (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
activityName TEXT NOT NULL UNIQUE,
location TEXT NOT NULL,
time NOT NULL, date NOT NULL,
reporter NOT NULL
);
Then the database will return an error if the name is already in the table.

creating extension not working: Table does not exist

I'm new in typo3 CMS and I'm now creating a new extension but I always get the following error when I try to execute query from repository.
1247602160: Table 'hr.tx_hr_domain_model_job' doesn't exist
this is my controller
<?php
namespace Hr\Hr\Controller;
class HrController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController
{
protected $jobsRepository;
protected $objectManager;
public function initializeAction()
{
parent::initializeAction();
$this->objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
$this->jobsRepository = $this->objectManager->get('Hr\\Hr\\Domain\\Repository\\JobRepository');
}
/**
* jobs list
*
* #return void
*/
public function listAction()
{
$this->view->assign('jobs', $this->jobsRepository->findAll());
}
}
and this is job repository class
<?php
namespace Hr\Hr\Domain\Repository;
class JobRepository extends \TYPO3\CMS\Extbase\Persistence\Repository
{
}
this is the content of ext_tables.sql file
#
# Table structure for table 'tx_hr_job'
#
CREATE TABLE IF NOT EXISTS `tx_hr_job` (
`JobId` int(10) NOT NULL,
`Kunde` varchar(255) NOT NULL,
`Titel` varchar(255) NOT NULL,
`Ort` varchar(255) NOT NULL,
`Volltext` text NOT NULL,
`Bundesland` varchar(255) NOT NULL,
`Region` varchar(255) NOT NULL,
`Branche` varchar(255) NOT NULL,
`Berufsgruppe` varchar(255) NOT NULL,
`Stellenart` varchar(255) NOT NULL,
`Datum` date NOT NULL,
PRIMARY KEY (`JobId`)
);
any help?
By convention the table name should be tx_hr_domain_model_job, alternatively you can use table mapping, but it could be tricky.
Use the extension_builder for kickstarting your ext - it's great tool for creating basic models, you can do it just with drag'n'drop - also relations, etc.
What's more important it will create all required pieces of code, models, repositories TCA configs etc so you'll see what's the most valid approach.

Yii's updateByPk keeps returning 0

I'm trying to update my user table, which my code does. but for some reason it keeps executing the else statement.
in the documentation it states that updateByPk should return the number of rows being updated. Which should be 1. what am i missing here? and how do i check if the table has been updated successfully?
if (User::model()->updateByPk($model->id, array("last_login"=> Shared::timeNow())))
{
Yii::app()->user->login($identity, $duration);
echo json_encode(array('error' => false, 'success' => url(app()->users->getHomeUrl())));
Yii::app()->end();
}
else {
echo json_encode(array('error' => 'Could not update user info', 'code' => 'auth'));
Yii::app()->end();
}
my table schema is this
CREATE TABLE IF NOT EXISTS `user` (
`id` int(10) unsigned NOT NULL,
`username` varchar(100) DEFAULT NULL,
`email` varchar(255) NOT NULL,
`first_name` varchar(45) NOT NULL,
`last_name` varchar(45) NOT NULL,
`gender` char(1) DEFAULT NULL,
`birthday` date DEFAULT '0000-00-00',
`address` varchar(255) DEFAULT NULL,
`city` varchar(45) DEFAULT NULL,
`state` varchar(45) DEFAULT NULL,
`website` text,
`postal_code` varchar(45) DEFAULT NULL,
`phone` varchar(45) DEFAULT NULL,
`password` varchar(63) DEFAULT NULL,
`activate` varchar(63) NOT NULL DEFAULT '1',
`create_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`last_login` datetime DEFAULT NULL,
`password_reset` int(11) unsigned DEFAULT NULL,
`admin` tinyint(1) unsigned NOT NULL DEFAULT '0',
`email_verified` tinyint(1) unsigned DEFAULT NULL,
`login_disabled` tinyint(1) unsigned NOT NULL DEFAULT '0',
`oauth_id` bigint(20) DEFAULT NULL,
`oauth_username` varchar(255) DEFAULT NULL,
`oauth_provider` varchar(10) DEFAULT NULL,
`oauth_email` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
You should check $model exits first otherwise updateByPk can return 0
if ($model && User::model()->updateByPk($model->id, array("last_login"=> Shared::timeNow())))
Your table does not specify a primary key. Yet you update by primary key.
You have a few choices.
Change your table structure to include a primary key
Use another update method. For example, Update() allow you to pass a criteria object.
Know it's old, but : https://www.php.net/manual/en/pdostatement.rowcount.php
If the last SQL statement executed by the associated PDOStatement was a SELECT statement, some databases may return the number of rows returned by that statement. However, this behaviour is not guaranteed for all databases and should not be relied on for portable applications.
And Yii1 use rowCount
Model can be not updated because last_login attribute has not valid value.
Try get model errors ($model->errors()) or check model by $model->validate();
Why you not implement this as follows:
$model->last_login = Shared::timeNow();
if ($model->save())
{
Yii::app()->user->login($identity, $duration);
echo json_encode(array('error' => false, 'success' => url(app()->users->getHomeUrl())));
Yii::app()->end();
}
else {
echo json_encode(array('error' => 'Could not update user info', 'code' => 'auth'));
Yii::app()->end();
}

Django: make auth_user.email case-insensitive unique and nullable

I want to make the auth_user.email case-insensitive unique, nullable and default null. The following almost works:
from django.db.models.signals import post_syncdb
import app.models
SQLITE_AUTH_REFORM = [
"PRAGMA writable_schema = 1;",
"""UPDATE SQLITE_MASTER SET SQL =
'CREATE TABLE auth_user (
"id" integer NOT NULL PRIMARY KEY,
"username" varchar(30) NOT NULL UNIQUE,
"first_name" varchar(30) NOT NULL,
"last_name" varchar(30) NOT NULL,
"email" varchar(75) DEFAULT NULL,
"password" varchar(128) NOT NULL,
"is_staff" bool NOT NULL,
"is_active" bool NOT NULL,
"is_superuser" bool NOT NULL,
"last_login" datetime NOT NULL,
"date_joined" datetime NOT NULL
)' WHERE NAME = 'auth_user';""",
"PRAGMA writable_schema = 0;",
]
def post_syncdb_callback(sender, **kwargs):
from django.db import connections
from django.conf import settings
cursor = connections['default'].cursor()
if 'sqlite' in settings.DATABASES['default']['ENGINE']:
for stmt in SQLITE_AUTH_REFORM:
cursor.execute(stmt)
cursor.execute(
"CREATE UNIQUE INDEX IF NOT EXISTS auth_user_email_unique "
"ON auth_user (email COLLATE NOCASE);"
)
else: # Oracle
cursor.execute(
"CREATE UNIQUE INDEX auth_user_email_unique "
"ON auth_user (upper(email));"
)
cursor.cursor.close()
post_syncdb.connect(post_syncdb_callback, sender=app.models)
I can
User.objects.create(username=str(random.random()), email=None)
To my heart's content. And also,
User.objects.create(username=str(random.random()), email='Foo')
User.objects.create(username=str(random.random()), email='foo')
...
IntegrityError: column email is not unique
The only problems is that the DEFAULT NULL does not seem to work: User.objects.create(username=str(random.random())) creates a user with an empty-string email.
However, in a unit-test, I believe something is going on that prevents the post-syncdb hook from working:
class DjangoUserTest(TestCase):
def test_unique_nullable_email(self):
import IPython; IPython.embed()
u1 = User.objects.create(username="u1", email=None)
u2 = User.objects.create(username="u2", email=None)
I can drop into the ipython shell and see that the table has been apparently modified:
In [1]: from django.db import connection
In [2]: c = connection.cursor()
In [3]: r = c.execute("select `sql` from sqlite_master WHERE tbl_name = 'auth_user';")
In [4]: r.fetchall()
Out[4]:
[(u'CREATE TABLE auth_user (\n "id" integer NOT NULL PRIMARY KEY,\n "username" varchar(30) NOT NULL UNIQUE,\n "first_name" varchar(30) NOT NULL,\n "last_name" varchar(30) NOT NULL,\n "email" varchar(75) DEFAULT NULL,\n "password" varchar(128) NOT NULL,\n "is_staff" bool NOT NULL,\n "is_active" bool NOT NULL,\n "is_superuser" bool NOT NULL,\n "last_login" datetime NOT NULL,\n "date_joined" datetime NOT NULL\n)',),
(None,),
(u'CREATE UNIQUE INDEX auth_user_email_unique ON auth_user (email COLLATE NOCASE)',)]
However, upon trying to do the creates, I get, IntegrityError: auth_user.email may not be NULL. How did this happen when the select sql from sqlite_master WHERE tbl_name = 'auth_user'; clearly says "email" varchar(75) DEFAULT NULL. I feel like I just need to commit the post_syncdb stuff or sth. Any ideas?
UPDATE: No amount of connection.commit(), cursor.close() helps, using TransactionTestCase does not help.