This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 5 years ago.
I have the following code which run when I press a button in an html form
<!DOCTYPE html>
<html>
<head>
<?php $timestamp = date("YmdHis"); ?>
<link rel="stylesheet" type="text/css" href="style.css"?v=<?php echo time(); ?>">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Data Entry</title>
</head>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql->set_charset('utf8');
// Create database
$sql = "CREATE DATABASE IF NOT EXISTS mortgagedb";
if ($conn->query($sql) === TRUE) {
echo "Database created successfully";
} else {
echo "Error creating database: " . $conn->error;
}
$sql = "CREATE TABLE IF NOT EXISTS mortgagedb . clientstest (
reg_date TIMESTAMP,
client_code INT(6) UNSIGNED PRIMARY KEY,
project_code INT(6) UNSIGNED NOT NULL,
client_name VARCHAR(255) NOT NULL,
client_id INT(14) NOT NULL,
client_id_expiry VARCHAR(7) NOT NULL,
client_address VARCHAR(255) NOT NULL,
client_profession VARCHAR(255) NOT NULL,
client_phone INT(12) NOT NULL,
income INT(6) NOT NULL,
guarantor_name VARCHAR(255) NOT NULL,
guarantor_id VARCHAR(14) NOT NULL,
guarantor_id_expiry VARCHAR(7) NOT NULL,
guarantor_address VARCHAR(255) NOT NULL,
guarantor_relation VARCHAR(255) NOT NULL,
unit_no VARCHAR(255) NOT NULL,
floor_no VARCHAR(255) NOT NULL,
bulding_no VARCHAR(255) NOT NULL,
location VARCHAR(255) NOT NULL,
project VARCHAR(255) NOT NULL,
city VARCHAR(255) NOT NULL,
governorate VARCHAR(255) NOT NULL,
area VARCHAR(255) NOT NULL,
unit_value INT(7) NOT NULL,
previous_payment INT(7) NOT NULL,
loan_maturity INT(2) NOT NULL,
mortgage_rate INT(2) NOT NULL,
annual_rate_wage INT(2) NOT NULL,
max_pay_inc INT(2) NOT NULL,
prog_max_inc INT(7) NOT NULL,
prog_min_inc INT(7) NOT NULL,
prog_max_sub INT(7) NOT NULL,
prog_min_sub INT(7) NOT NULL,
payment_graduation INT(2) NOT NULL,
iscore VARCHAR(255) NOT NULL,
documents VARCHAR(255) NOT NULL,
condition1 VARCHAR(255) NOT NULL,
condition2 VARCHAR(255) NOT NULL,
condition3 VARCHAR(255) NOT NULL,
condition4 VARCHAR(255) NOT NULL,
condition5 VARCHAR(255) NOT NULL,
condition6 VARCHAR(255) NOT NULL,
condition7 VARCHAR(255) NOT NULL,
condition8 VARCHAR(255) NOT NULL,
condition9 VARCHAR(255) NOT NULL,
condition10 VARCHAR(255) NOT NULL,
condition11 VARCHAR(255) NOT NULL,
condition12 VARCHAR(255) NOT NULL,
condition13 VARCHAR(255) NOT NULL,
condition14 VARCHAR(255) NOT NULL,
attachment1 VARCHAR(255) NOT NULL,
attachment2 VARCHAR(255) NOT NULL,
attachment3 VARCHAR(255) NOT NULL,
attachment4 VARCHAR(255) NOT NULL,
attachment5 VARCHAR(255) NOT NULL,
attachment6 VARCHAR(255) NOT NULL
)";
if ($conn->query($sql) === TRUE) {
echo "Table clientstest created successfully";
} else {
echo "Error creating table: " . $conn->error;
}
$sql = "INSERT INTO mortgagedb . clientstest (client_code, project_code, client_name, client_id, client_id_expiry, client_address, client_profession, client_phone, income, guarantor_name, guarantor_id, guarantor_id_expiry, guarantor_address, guarantor_relation, unit_no, floor_no, bulding_no, location, project, city, governorate, area, unit_value, previous_payment, loan_maturity, mortgage_rate, annual_rate_wage, max_pay_inc, prog_max_inc, prog_min_inc, prog_max_sub, prog_min_sub, payment_graduation, iscore, documents, condition1, condition2, condition3, condition4, condition5, condition6, condition7, condition8, condition9, condition10, condition11, condition12, condition13, condition14, attachment1, attachment2, attachment3, attachment4, attachment5, attachment6)
VALUES ('$_POST[ccode]', '$_POST[pcode]', '$_POST[cname]', '$_POST[cid]', '$_POST[ciddate]', '$_POST[caddress]', '$_POST[cjob]', '$_POST[cphone]', '$_POST[inc]', '$_POST[gname]', '$_POST[gid]', '$_POST[giddate]', '$_POST[gaddress]', '$_POST[grel]', '$_POST[un]', '$_POST[floor]', '$_POST[bn]', '$_POST[lctn]', '$_POST[pro]', '$_POST[city]', '$_POST[gov]', '$_POST[area]', '$_POST[unitv]', '$_POST[pp]', '$_POST[lm]', '$_POST[mr]', '$_POST[row]', '$_POST[paytoinc]', '$_POST[maxinc]', '$_POST[mininc]', '$_POST[maxsub]', '$_POST[minsub]', '$_POST[pgr]', '$_POST[iscore]', '$_POST[docs]', '$_POST[con1]', '$_POST[con2]', '$_POST[con3]', '$_POST[con4]', '$_POST[con5]', '$_POST[con6]', '$_POST[con7]', '$_POST[con8]', '$_POST[con9]', '$_POST[con10]', '$_POST[con11]', '$_POST[con12]', '$_POST[con13]', '$_POST[con14]', '$_POST[att1]', '$_POST[att2]', '$_POST[att3]', '$_POST[att4]', '$_POST[att5]', '$_POST[att6]' )";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
</body>
</html>
but I get the following error
Fatal error: Uncaught Error: Call to a member function set_charset()
on null in C:\xampp\htdocs\Mortgage Project\db.php:21 Stack trace: #0
{main} thrown in C:\xampp\htdocs\Mortgage Project\db.php on line 21
I know the error is about set_char, but I'm new and don't really understand the problem here , thanks in advance.
Your error comes from this line:
$sql->set_charset('utf8');
Basically its correct, but you haven't defined $sql at all. You need to use set_charset on your connection object.
So instead of your code, write:
$conn->set_charset('utf8');
You have $sql->set_charset('utf8');. However. $sql is only defined on the NEXT LINE!
Try changing it to $conn!
I am trying to execute the following query
DROP TABLE IF EXISTS `developer_messenger`;
CREATE TABLE `developer_messenger` (
`id` int(10) NOT NULL,
`title` varchar(45) NOT NULL,
`username` varchar(45) NOT NULL,
`message` varchar(45) NOT NULL,
`type` varchar(45) NOT NULL,
`date_time` varchar(45) NOT NULL,
`status` varchar(45) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
But this simple query is showing me Error in PHP
Could not get data: 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 'CREATE TABLE developer_messenger ( id int(10)
NOT NULL, title varchar(' at line 1
I am a newbie, Sorry if its silly,
Help Appreciated!
In Response to your second question about magic_quotes:
if (!get_magic_quotes_gpc()) { //checks php ini if magic_quotes is not on
$title = addslashes($_POST['title']);
}
else
{
$title = stripslashes($_POST['title']);
}
addslashes(); http://php.net/manual/en/function.addslashes.php
stripslashes(); http://php.net/manual/en/function.stripslashes.php
not sure if this helps but it might that way you dont have to change php.ini
just the $data that is to be entered into sql
Your query is correct, Try exciting DROP and CREATE one by one.
This code may help you.
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// sql to create table
$sql = "CREATE TABLE `developer_messenger` (
`id` int(10) NOT NULL,
`title` varchar(45) NOT NULL,
`username` varchar(45) NOT NULL,
`message` varchar(45) NOT NULL,
`type` varchar(45) NOT NULL,
`date_time` varchar(45) NOT NULL,
`status` varchar(45) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1";
if ($conn->query($sql) === TRUE) {
echo "Table MyGuests created successfully";
} else {
echo "Error creating table: " . $conn->error;
}
$conn->close();
?>
I'm trying to copy 2 tables structures into a new database.
I use a function that makes by herself the SQL command: When i execute the code on phpmyadmin the code gets executed but when I execute this by PHP, it doesn't execute.
How is possible?
The sql command is this:
CREATE TABLE `tabella_1` (
`campo1` int(11) NOT NULL AUTO_INCREMENT,
`campo2` varchar(100) COLLATE latin1_general_ci NOT NULL,
`campo_3` int(11) NOT NULL,
PRIMARY KEY (`campo1`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
CREATE TABLE `tabella_2` (
`campo1` int(11) NOT NULL DEFAULT '0',
`campo2` varchar(100) COLLATE latin1_general_ci NOT NULL,
`campo_3` int(11) NOT NULL,
PRIMARY KEY (`campo1`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
Thanks
Update this code with your database connection details:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// sql to create table
$sql = "CREATE TABLE `tabella_1` ( `campo1` int(11) NOT NULL AUTO_INCREMENT, `campo2` varchar(100) COLLATE latin1_general_ci NOT NULL, `campo_3` int(11) NOT NULL, PRIMARY KEY (`campo1`) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci";
if ($conn->query($sql) === TRUE) {
echo "Table 1 created successfully";
} else {
echo "Error creating table: " . $conn->error;
}
// sql to create table
$sql2 = "CREATE TABLE `tabella_2` ( `campo1` int(11) NOT NULL DEFAULT '0', `campo2` varchar(100) COLLATE latin1_general_ci NOT NULL, `campo_3` int(11) NOT NULL, PRIMARY KEY (`campo1`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci";
if ($conn->query($sql2) === TRUE) {
echo "Table 2 created successfully";
} else {
echo "Error creating table: " . $conn->error;
}
$conn->close();
?>
I got it!
I putted in a array() the functions and it works!
Tkanks to everybody!
I have this table:
CREATE TABLE `comment` (
`id` int(11) unsigned NOT NULL,
`name` varchar(255) DEFAULT NULL,
`email` varchar(255) DEFAULT NULL,
`comment` text,
`article_id` int(11) unsigned NOT NULL DEFAULT '1',
`date` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`deleted` tinyint(3) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
This is in the table:
INSERT INTO `comment` VALUES (0,'Bernard','user#domain.com','This is a comment',1,'2014-07-22 17:34:24',0);
This php code spits out "foo" and nothing else:
<?php
error_reporting(E_ALL);
echo 'foo';
$db = new mysqli("localhost", 'root', '', 'ggs');
$query = $db->prepare("SELECT * FROM `comment` `c` WHERE `c`.`article_id` = ? AND `c`.`deleted` = 0 ORDER BY `c`.`date` ASC");
if (!$query) {
echo $db->errno . " - Could not prepare SQL statement: " . $db->error;
} else {
$query->bind_param('i', 1);
$query->execute();
echo json_encode($query->fetch());
}
echo 'bar';
Why is this failing, and why is it not throwing any errors?
As for the query, you should replace the first 0 a null to let the auto-increment work:
INSERT INTO `comment` VALUES (NULL,'Bernard','user#domain.com','This is a comment',1,'2014-07-22 17:34:24',0);
I am trying to execute 3 queries to create tables within a database but it will not execute the query giving me a syntax error. Can anyone look at this code and tell me what I typed wrong to make this work? I have tried everything and I can not get the queries to execute!
$dh = mysqli_connect($_POST['hostname'], $_POST['username'], $_POST['password'], $_POST['database']);
if(! $dh )
{
die('Could not connect: ' . mysql_error());
}
echo "Connected successfully...<br /><br />";
$query = "CREATE TABLE IF NOT EXISTS `content` (
`content_id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`section_id` INT(11) NOT NULL,
`header` VARCHAR(255) NOT NULL,
`sub_header` VARCHAR(255) NOT NULL,
`date_range` VARCHAR(64) NOT NULL,
`content_body` TEXT NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1";
$query .= "CREATE TABLE IF NOT EXISTS `sections` (
`section_id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`title` VARCHAR(255) NOT NULL,
`position` INT(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1";
$query .= "CREATE TABLE IF NOT EXISTS `sections` (
`user_id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`username` VARCHAR(32) NOT NULL,
`password` VARCHAR(32) NOT NULL,
`first_name` VARCHAR(32) NOT NULL,
`last_name` VARCHAR(32) NOT NULL,
`email` VARCHAR(1024) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1";
$retval = mysqli_multi_query($dh, $query);
You don't have semi colons between the queries and you are joining them all together and running at once.
If you are running multiple queries like that you need semicolons at the end of each query
$query = "CREATE TABLE IF NOT EXISTS `content` (
`content_id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`section_id` INT(11) NOT NULL,
`header` VARCHAR(255) NOT NULL,
`sub_header` VARCHAR(255) NOT NULL,
`date_range` VARCHAR(64) NOT NULL,
`content_body` TEXT NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;";
$query .= "CREATE TABLE IF NOT EXISTS `sections` (
`section_id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`title` VARCHAR(255) NOT NULL,
`position` INT(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;";
$query .= "CREATE TABLE IF NOT EXISTS `sections` (
`user_id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`username` VARCHAR(32) NOT NULL,
`password` VARCHAR(32) NOT NULL,
`first_name` VARCHAR(32) NOT NULL,
`last_name` VARCHAR(32) NOT NULL,
`email` VARCHAR(1024) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;";
You are missing semicolons between queries.
So you are trying to execute something like this: <...> ENGINE=InnoDB DEFAULT CHARSET=latin1CREATE TABLE <...>