why table is not creating in phpmyadmin [duplicate] - php

This question already has an answer here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 6 years ago.
I have written these codes (if table doesn't exist) condition. The Database is fine however my table is not creating and there was not error. Can anyone help me?!
$connect=mysqli_connect($host,$username,$password)or die("cannot connect");
$create=mysqli_query($connect,"CREATE DATABASE IF NOT EXISTS projectdb") or die (mysql_error());
mysqli_select_db($connect,"projectdb");
$info="CREATE TABLE IF NOT EXISTS info (
id int NOT NULL auto_increment,
contactEmail text NOT NULL,
firstName varchar(50) NOT NULL,
lastName varchar(50) NOT NULL,
phone varchar(50) NOT NULL,
phone text NOT NULL,
dob date NOT NULL,
address varchar(50) NOT NULL,
country varchar(70) NOT NULL,
PRIMARY KEY(id),
UNIQUE KEY ( id )
)";
mysqli_query($connect,$info);

your phone column is duplicate. so use any one
phone varchar(50) NOT NULL,
phone text NOT NULL,
Please use bellow :
phone varchar(50) NOT NULL,
or
phone text NOT NULL,

Related

On DB Query PHP return NULL but are records [duplicate]

This question already has an answer here:
Unknown database error in PHP but it exists in PHPMyAdmin
(1 answer)
Closed 6 months ago.
[PHP 8.1/SQL 10.4]
DB Structure
CREATE TABLE `trundschreiben` (
`rsID` int(11) NOT NULL,
`rsMTGLemail` varchar(255) COLLATE latin1_german1_ci NOT NULL,
`rsVersendet` tinyint(1) DEFAULT NULL,
`rsVersanddatum` date DEFAULT NULL,
`rsAbsender` varchar(255) COLLATE latin1_german1_ci DEFAULT NULL,
`rssprache` varchar(3) COLLATE latin1_german1_ci NOT NULL,
`rskurse` text COLLATE latin1_german1_ci NOT NULL,
`rskurseplain` text COLLATE latin1_german1_ci NOT NULL,
`rsfirmenname` varchar(255) COLLATE latin1_german1_ci DEFAULT NULL,
`rstyp` int(10) UNSIGNED NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_german1_ci;
PHP Code:
$sql = "SELECT MAX(rsID) as rsid FROM trundschreiben";
$result = mysqli_query($db_link, $sql);
$nrRS = mysqli_fetch_array($result, MYSQLI_ASSOC)
GOAL
Get the highest value of 'rsID' from the table
WHAT I TRIED
In phpmyadmin it works perfectly, the value is returned
QUESTION
In PHP, var_dump($nrRS); returns me a value of
array(1) { ["rsid"]=> NULL }
while the $result returns me this array
current_field: 0
field_count: 1
lengths: null
num_rows: 1
type:0
How come I can't receive the value as I expect?
Thank you!
When your table is empty, the max value is NULL. After inserting at least one row in the table, your code will start returning a number.
PHP online environment

MySql statement Error: SQLSTATE[21S01]: Insert value list does not match column list: 1136 Column count doesn't match value count at row 1 [duplicate]

This question already has answers here:
PDO Uncaught exception - Insert value list does not match column list
(3 answers)
Closed 1 year ago.
`Help beginner level
here is the error
MySql statement Error: SQLSTATE[21S01]: Insert value list does not match column list: 1136 Column count doesn't match value count at row 1
SQL: insert into form values('danish','122016','danish.baksh#hyperquality.com','devloper','marketing','danish baksh','danish.baksh#hyperquality.com','156','fdjfj','564','fjf','867','dfj','5344','contactus.png','0')
query
$qry="insert into form values('$empname','$empcode','$empemail','$designation','$process','$supervisior','$supemail','$expense1','$aboutexp1','$expense2','$aboutexp2','$expense3','$aboutexp3','$total','$file_name','0')";
here is the database
CREATE TABLE `form` (
`empname` varchar(50) DEFAULT NULL,
`empcode` varchar(50) DEFAULT NULL,
`empemail` varchar(50) DEFAULT NULL,
`designation` varchar(50) DEFAULT NULL,
`process` varchar(50) DEFAULT NULL,
`supervisior` varchar(50) DEFAULT NULL,
`supemail` varchar(50) DEFAULT NULL,
`expense1` int(50) DEFAULT NULL,
`aboutexp1` varchar(50) DEFAULT NULL,
`expense2` int(50) DEFAULT NULL,
`aboutexp2` varchar(50) DEFAULT NULL,
`expense3` int(50) DEFAULT NULL,
`aboutexp3` varchar(50) DEFAULT NULL,
`total` int(50) DEFAULT NULL,
`bill` varchar(50) DEFAULT NULL,
`approved` tinyint(1) DEFAULT '0'
) ENGINE=MyISAM DEFAULT CHARSET=latin1
ask if u need more
Try using below query:
$qry="insert into form (empname,empcode,empemail,designation,process, supervisior,supemail,expense1,aboutexp1,expense2,aboutexp2,expense3, aboutexp3,total,bill,approved)
values('$empname','$empcode','$empemail','$designation','$process','$supervisior','$supemail','$expense1','$aboutexp1','$expense2','$aboutexp2','$expense3','$aboutexp3','$total','$file_name','0')";
It may help. Moreover, You should have primary key in your table.

MySQL error with NOT NULL

I am trying to run this query in my database:
CREATE TABLE users(
user_id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT,
username VARCHAR NOT NULL,
userpass VARCHAR NOT NULL,
first_name VARCHAR NOT NULL,
last_name VARCHAR NOT NULL,
email VARCHAR NOT NULL,
address VARCHAR NOT NULL,
phone BIGINT UNSIGNED NOT NULL,
PRIMARY KEY(user_id)
)
MySQL returns this error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'NOT NULL,
userpass VARCHAR NOT NULL,
first_name VARCHAR NOT NULL,
' at line 3
I cant figure out what is wrong with the code...
You need to specify size for VARCHAR columns:
CREATE TABLE users( user_id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT,
username VARCHAR(100) NOT NULL,
userpass VARCHAR(100) NOT NULL,
first_name VARCHAR(100) NOT NULL,
last_name VARCHAR(100) NOT NULL,
email VARCHAR(100) NOT NULL,
address VARCHAR(100) NOT NULL,
phone BIGINT UNSIGNED NOT NULL,
PRIMARY KEY(user_id) );
SqlFiddleDemo
Storing phone number as BIGINT is not best idea. Consider normal string instead.

Table in php is not being created when ran through the query

I did this earlier and it worked just fine and I even brought in another table and it was created just fine. I am stuck.
Here is the table that is not working.
<?php
include_once('dbconx.php');
$tbl_pages = "CREATE TABLE IF NOT EXISTS pages (
id INT(11) NOT NULL AUTO_INCREMENT,
label VARCHAR(20) NOT NULL,
title VARCHAR(50) NOT NULL,
body TEXT NOT NULL,
slug VARCHAR(50) NOT NULL,
create TIMESTAMP NOT NULL,
updated TIMESTAMP NULL,
PRIMARY KEY(id),
)";
$query = mysqli_query($dbcon, $tbl_pages);
if ($query === TRUE) {
echo "<h3>Pages table created OK :) </h3>";
} else {
echo "<h3>Pages table NOT created :( </h3>";
}
?>
One of your field names is a MySQL reserved words create. Try changing the field name to something like created. See MySQL Keywords and Reserved Words
You also have an extra comma after the primary key definition.
Try this query:
$tbl_pages = "CREATE TABLE IF NOT EXISTS pages (
id INT(11) NOT NULL AUTO_INCREMENT,
label VARCHAR(20) NOT NULL,
title VARCHAR(50) NOT NULL,
body TEXT NOT NULL,
slug VARCHAR(50) NOT NULL,
created TIMESTAMP NOT NULL,
updated TIMESTAMP NULL,
PRIMARY KEY(id)
)";
You can also echo mysqli_error if it fails so you can see details of the error.

"Incorrect table definition" when trying to CREATE TABLE

I am trying to create a table in a php script and I am getting an SQL error. I can't find it or figure out how to fix it. I do have $table defined earlier in the script. Thanks in advance!
$newtable = "CREATE TABLE $table (id INT(11) NOT NULL AUTO_INCREMENT, time datetime NOT NULL,punchtype VARCHAR(255) NOT NULL,groupname VARCHAR(255) NOT NULL, dept VARCHAR(255) NOT NULL, notes VARCHAR(255))";
Error Message: Incorrect table definition; there can be only one auto column and it must be defined as a key
try this, it works
$newtable = "CREATE TABLE $table
(id INT(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
time datetime NOT NULL,
punchtype VARCHAR(255) NOT NULL,
groupname VARCHAR(255) NOT NULL,
dept VARCHAR(255) NOT NULL,
notes VARCHAR(255))";

Categories