php mysql error for creating a table - php

I got an error while creating a table in php with mysql database, and I tried testing directly on mysql query engine it works fine. whereas in php code it gives below 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.
Below is the code I am writing
$query14 = mysql_query("create table $tablename (
project_id INT,
project_client_id INT,
project_partner_id INT,
project_manager_id INT,
project_employees INT,
project_name VARCHAR(500),
project_status TEXT,
project_summary LONGTEXT,
project_order INT,
project_start_date DATETIME,
project_end_date DATETIME
) ENGINE = INNODB;");
and below is the image attached and table structure should be and this is the sample table i create using with phpmyadmin interface.
And below is the full 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 ''8' (project_id INT, project_client_id INT, project_partner_id INT, project_mana' at line 1`

I didn't see any problem with your query. But what is the value of $tablename? Two error possibilities here :
The variable $tablename is empty.
$tablename is a key-word.
Please check the above two otherwise its all right.
UPDATE :
As per your updated question please try with the following code. I think it will help you.
$query14 = mysql_query("create table `$tablename` (
`project_id` INT,
`project_client_id` INT,
`project_partner_id` INT,
`project_manager_id` INT,
`project_employees` INT,
`project_name` VARCHAR(500),
`project_status` TEXT,
`project_summary` LONGTEXT,
`project_order` INT,
`project_start_date` DATETIME,
`project_end_date` DATETIME
) ENGINE = INNODB;");

Your query syntax is fine, it looks like $tablename is empty.

I think the issue is what is getting replaced for $tablename.
Having the full code example would be more useful in debugging the error.
The mysql create statement works just fine here on mysql 5.1.58 .

As you said in above comments that after echoing the $tablename you got the value 8 then it is not possible to create table.
you have to rename the value of $tablename like tbl_8.
remember with the name tbl-8 your also got error,,

The problem is, $tablename is 8 currently and for SQL table, table name must start with a letter. Current tablename is invalid.

Related

Cannot insert column containing colon in column name

I am trying to insert a column with column name 00:18:e7:f9:65:a6 with the statement
ALTER IGNORE TABLE tblwifi_information ADD "00:18:e7:f9:65:a6" INT
...but it throws an error:
/* SQL Error (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 '"00:18:e7:f9:65:a6" INT' at line 1 */
in Heidi SQL. I am using MySQL database.
When I try to add the column manually b going to the structure of the table it works and does not give any error but when I run this statement it does not allow me. How can I solve this?
I just ran your query:
ALTER IGNORE TABLE tblwifi_information ADD "00:18:e7:f9:65:a6" INT
and got the same error. I then ran:
ALTER IGNORE TABLE tblwifi_information ADD `00:18:e7:f9:65:a6` INT
and it worked. Pretty sure you need to change " to `
Fail with double qoutes:
Success with backticks:
use backticks for columnname
change
ALTER IGNORE TABLE tblwifi_information ADD "00:18:e7:f9:65:a6" INT
TO
ALTER IGNORE TABLE tblwifi_information ADD `00:18:e7:f9:65:a6` INT
SQL FIDDLE
To add something to the story, when querying multiple tables, you have to use following expression:
`tablename`.`columnname`
(NOT `tablename.columnname` )
I took me some time to figure it out, so I hope it helps someone :)
Example:
SELECT *
FROM tb_product p
JOIN tb_brand b ON ( `p`.`tb_brand:IDbrand` = `b`.`IDbrand` )
WHERE IDfamily = 2
i dont really know what you are trying to achive here pal.
instead of:
00:18:e7:f9:65:a6
to
00_18_e7_f9_65_a6
then adjust your code to replace ":" to "_" in php or mysql.
in sql:
Replace(table_name, ':', '_');
in PHP:
srt_replace(':','_',$query);

Create function Error in phpmyadmin

when I try to create a function to retrieve userName from user table using their email it gives me this useless error:
#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 'DECLARE name VARCHAR; BEGIN select userName AS name from
user WHERE `ema' at line 2
, the same code with a different syntax works in mssql so I wonder what is the difference? in better words what am I doing wrong here?
DELIMITER ;;
CREATE FUNCTION getUserName(email varchar(50)) RETURNS VARCHAR
BEGIN
DECLARE name VARCHAR;
SELECT `userName` AS name FROM `user` WHERE `email` = email;
RETURN name;
END ;;
Well, first of all, varchar needs to have length, which you lack in two places. Also, you need to select into the variable and return the variable, because you cannot return resultset. Also you should escape name, and you do not need to alias your column because you are selecting into anyway. So, your code should be like this:
DELIMITER ;;
CREATE FUNCTION getUserName(email VARCHAR(50)) RETURNS VARCHAR(50)
BEGIN
DECLARE NAME VARCHAR(50);
SELECT `userName` FROM `user` WHERE `email` = email
INTO `name`;
RETURN `name`;
END ;;

PHP/SQL Syntax Error 1064

I'm new to PHP so bear with me.
This is the code for entering into a bookings table on my site:
http://pastie.org/8190189
And this is the error I get when I enter data into a form on the front end of the site:
http://pastie.org/8190194
I've been working at it for hours but I can't get anywhere.
I thought the problem may be with the:
SELECT u_id FROM `joom_hl_puser_role` WHERE pid = '1'
The table puser_role just connects joomla users to hotels that they can manage on the site. (I want to enter the hotel manager's joomla user number to the table).
but I'm sure that the syntax is correct.
Thanks for your help.
The problem is your use of TIME();
TIME needs a parameter passed.
mysql> select TIME();
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 ')' at line 1
Change it to
TIME(NOW())
From the MySQL manual for TIME
TIME(expr)
Extracts the time part of the time or datetime expression expr and
returns it as a string.
This function is unsafe for statement-based replication. Beginning
with MySQL 5.5.1, a warning is logged if you use this function when
binlog_format is set to STATEMENT. (Bug #47995)
mysql> SELECT TIME('2003-12-31 01:02:03');
-> '01:02:03'
mysql> SELECT TIME('2003-12-31 01:02:03.000123');
-> '01:02:03.000123'
you are in inserting the TIME() and NOW() functions inside "double quotes". try try chaing it to look like this
$query = "INSERT INTO `#__chbookings_bookings` (`id` ,`company` ,`firstname`,`lastname`, ,`email` ,`reference_number` , `transactionid` ,`invoice_number` , `property` ,`payment_type` ,`people` ,`phonenumber` ,`checkin_date` ,`checkout_date`,`room_type` , `status` , `total_amount` ,`amount_paid` ,`pending_amount`, `date_booked`,`last_updated`, `comments`) VALUES (NULL , '".$user_id."','".$data['firstName']."', '".$data['lastName']."',
'".$data['email']."', ".TIME().",
'".$result["transaction_id"]."','".$invoice_number."',
(SELECT u_id FROM `#__hl_puser_role` WHERE pid = '".$data['property_id']."'),
0 ,'".$total_adult."', '".$data['phone']."',
'".$session->get('checkin_date')."', '".$session->get('checkout_date')."', 0, 0,
'".$final_amount."', '".$session->get('amount_payable')."',
'".$session->get('balance_amount')."',
".NOW().", ".NOW().", 'comments'";

creating DB table by giving input from php text box

I want to make a table in a database, by giving the name of that table as an input from a text box.
<?php
$tablename = $_POST['tablename'];
// Create a MySQL table in the selected database
mysql_query("CREATE TABLE $tablename(
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
firstpublish VARCHAR(255),
descriptions VARCHAR(255))") or die(mysql_error());
?>
it makes the table and the field, but i can't insert any data in it. When i run the code below
<?php
$firstpublish = $_POST['firstpublish'];
$descriptions = $_POST['descriptions'];
if(isset($_POST['firstpublish']) || ($_POST['descriptions']))
{
$order="INSERT INTO $tablename (id,firstpublish,descriptions) VALUES ('','$firstpublish','$descriptions')";
$result = mysql_query($order) or die (mysql_error());
}
?>
it showing an error message
"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 '( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), firstpublish VARCHA' at line 1"
how can i fixed this problem.
-thankyou.
My best guess would be that your first query is somehow being rerun with a blank value for $tablename
If this is a publicly accessible page, i'd be very careful about creating tables from user input. You also probably want to (at the very least) run $tablenale through mysql_real_escape_string() and change CREATE TABLE to CREATE TABLE IF NOT EXISTS
The error is here:
mysql_query("CREATE TABLE $tablename(
Since you are mixing a variable in a quoted string, you insert whitespace after its name, because "$tablename(" is illegal name and will most likely be replaced will null, so the SQL-statement will actually be seen for MySQL as
CREATE TABLE id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), firstpublish VARCHAR(255), descriptions VARCHAR (255))
which, in turn, is a syntax error.
Solution: Add a space between $tablename and "(".

entering datas of php page to database

I have a php form with two text boxes and i want to enter the text box values into the database. I have created the table (with two columns namely webmeasurementsuite id and webmeasurements id) I used the following syntax for creating table:
CREATE TABLE `radio` (
`webmeasurementsuite id` INT NOT NULL,
`webmeasurements id` INT NOT NULL
);
Utilising the tutorial in the following link, I wrote the php coding but unfortunately the datas are not getting entered into the database. I am getting an error in the insert sql syntax. I checked it but i am not able to trace out the error.Can anyone correct me? I got the coding from http://www.webune.com/forums/php-how-to-enter-data-into-database-with-php-scripts.html
$sql = "INSERT INTO $db_table(webmeasurementsuite id,webmeasurements id) values
('".mysql_real_escape_string(stripslashes($_REQUEST['webmeasurementsuite
id']))."','".mysql_real_escape_string(stripslashes($_REQUEST['webmeasurements id']))."')";
echo$sql;
My error is as follows:
INSERT INTO radio(webmeasurementsuite id,webmeasurements id) values ('','')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 'id,webmeasurements id) values ('','')' at line 1
Because your table names have a space in them, you have to always surround them in backticks. Try this for your query:
$sql = "INSERT INTO $db_table(`webmeasurementsuite id`,`webmeasurements id`) values ('".mysql_real_escape_string(stripslashes($_REQUEST['webmeasurementsuite id']))."','".mysql_real_escape_string(stripslashes($_REQUEST['webmeasurements id']))."')";
Looking at your pastebin, it looks like you have forgotten to close your input tags:
<input TYPE="text" name="webmeasurementsuite id"

Categories