Related
I am unable to find any error shown but for some reason there is still an issue entering data into my table using SQL. I am new to coding and am not really sure what the issue is . I am sure that everything that needs to be passed and given is done but the mysql query is where it goes wrong and i am not able to understand why . could someone please help me out ?
<?php
session_start();
$conb = mysqli_connect("127.0.0.1","root","","demo");
$sellmail = $_SESSION['sellermaill'];
$buyermail = $_POST['email'];
$bid = $_POST["bid"];
$title = $_SESSION["Titleofp"];
echo"$sellmail";
echo"$buyermail";
echo"$bid";
echo"$title";
$mysqlbuy = "INSERT INTO buyer (Seller Mail,Buyer Mail,Bid,Product Title) VALUES ('$sellmail','$buyermail','$bid','$title')";
$mysqlsellq = mysqli_query($conb,$mysqlbuy);
if(!$mysqlsellq)
{echo "Your Bid has not been saved ";}
else echo "Your Bid has been Saved ";
?>
Error
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 'Mail,Seller Mail,Product Title,Bid) VALUES
('rao.7#gmail.com','rsk101295#gmail.c' at line 1
(I) Change your column name in Database table:
1)Seller Mail to Seller_Mail
2)Buyer Mail to Buyer_Mail
3)Product Title to Product_Title
Then insert
$mysqlbuy = "INSERT INTO buyer (Seller_Mail,Buyer_Mail,Bid,Product_Title) VALUES ('$sellmail','$buyermail','$bid','$title')";
Embedded spaces or special characters are NOT ALLOWED in column name.
For more info, click Characters that are not allowed in table name & column name
OR
(II)
If, you don't want to put underscore in column name or don't want to modify your column name. Use like this. (Use Backtick to enclose column name.). But, make sure you follow naming conventions of column name.
$mysqlbuy = "INSERT INTO buyer (`Seller Mail`,`Buyer Mail`,`Bid`,`Product Title`) VALUES ('$sellmail','$buyermail','$bid','$title')";
For more info, Please click How to select a column name with space between in mysql
Find Backtick in Keyboard:
I've been fighting with a bit of code for a week now, not seeing what the heck is wrong...
I have a gaming site I'm trying to build new character sheets for, the form is all done, the action pointing to another page that is strictly the sql for inserting the information into the database. We have good connection, but it is hanging at the second insert statement. The code was working previously, but we had to delete the database and rebuild it, resulting in a rebuild of the insert sql lines.
The first portion of the insert code is:
if($_POST['Submit']=="Submit")
{
$sql="INSERT INTO accounts (log_name,owner,account_type,date_joined) VALUES (\"$_POST[char_name]\",\"$_SESSION[logname]\",\"$_POST[account_type]\",NOW())";
$result = mysql_query($sql)
or die("<p>Couldn't add character.<br/>".mysql_error()." in accounts.<br/>Please send this exact message to <a href='mailto:savvannis#houston-by-night.com'>Savvannis</a> with your character's name.</p>");
echo $result;
echo $_SESSION['logname'];
$sql="INSERT INTO topdata (log_name,char_venue,sub_venue,species,char_name,create_date,gender,age,appage,nature,demeanor,concept,description,web_site,view_pword,sfa) VALUES (\"$_SESSION[logname]\",\"$_POST[char_venue]\",\"$_POST[sub_venue]\",\"$_POST[species]\",\"$_POST[char_name]\",NOW(),\"$_POST[gender]\",\"$_POST[age]\",\"$_POST[appage]\",\"$_POST[nature]\",\"$_POST[demeanor]\",\"$_POST[concept]\",\"$_POST[description]\",\"$_POST[web_site]\"\"$_POST[viewpw]\",\"$_POST[sfa]\")";
$result=mysql_query($sql)
or die ("<p>Could not create character.<br/>".mysql_error()." in topdata.<br/>Please send this exact message to <a href='mailto:savvannis#houston-by-night.com'>Savvannis</a> with your character's name.</p>");
echo $result;
When the information is entered into the form and submit is hit, I get the following:
1
Could not create character.
Column count doesn't match value count at row 1 in topdata.
Please send this exact message to Savvannis with your character's name.
I look at the database and the information is entered into the accounts table, so that statement is working, but it is hanging up on the topdata table. It's not echoing the $_SESSION['logname'] and looking at the database, it's not saving the owner, which should be $_SESSION['logname'], so I'm wondering if that statement is now somehow incorrect??
I can't figure out what the heck is wrong. Any and all help would be greatly appreciated.
You have missed a comma here: \"$_POST[web_site]\"\"$_POST[viewpw]\" in your second insert SQL.
It should be \"$_POST[web_site]\", \"$_POST[viewpw]\"
First off the error message is telling you that there is an unequal number of columns and values in your SQL
Lets have a look at that
INSERT INTO topdata (
log_name,
char_venue,
sub_venue,
species,
char_name,
create_date,
gender,
age,
appage,
nature,
demeanor,
concept,
description,
web_site,
view_pword,
sfa
) VALUES (
\"$_SESSION[logname]\",
\"$_POST[char_venue]\",
\"$_POST[sub_venue]\",
\"$_POST[species]\",
\"$_POST[char_name]\",
NOW(),
\"$_POST[gender]\",
\"$_POST[age]\",
\"$_POST[appage]\",
\"$_POST[nature]\",
\"$_POST[demeanor]\",
\"$_POST[concept]\",
\"$_POST[description]\",
\"$_POST[web_site]\"\"$_POST[viewpw]\",
\"$_POST[sfa]\"
)";
Now by formatting your SQL (which is vulnerable to sql injection) I've noticed a missing comma between web_site and viewpw values
I have dumped the contents of a database in an sql file in a form like
insert into `a` values
(17,11,5),
(18,12,7),
(19,12,10),
(21,14,45),
(22,15,46),
(24,16,46),
(25,16,49),
(26,17,21),
(27,17,30),
(28,17,45),
(29,17,54),
(30,18,32),
(31,18,35),
(32,19,23),
(33,19,27),
(34,19,54),
(35,20,53),
(36,21,32),
(37,21,35),
(38,21,45),
(39,22,23),
(40,22,30),
(41,22,45),
(57,24,19),
(58,25,46),
(59,26,39),
(60,27,49),
(61,27,56),
(62,28,34);
insert into `b` values (14,'2009-01-06',''),
(15,'2009-02-01',''),
(16,'2009-03-01',''),
(17,'2009-03-25',''),
(18,'2009-04-05',''),
(19,'2009-04-17',''),
(20,'2009-04-18',''),
(21,'2009-04-19',''),
(22,'2009-04-23',''),
(24,'2009-07-05',''),
(25,'2009-08-02',''),
(26,'2009-08-07',''),
(27,'2009-09-06',''),
(28,'2009-09-14','');
etc..
I have 4 such tables with no foreigh key constrains. Then I try to upload the data into the db (mysql). I read the file's contents, I pass each table's insertion into an array and then i do mysql_query for each element :
$sqlArray = explode(';',$sqlFile);
for($i=0;$i<sizeof($sqlArray);$i++){
mysql_query($sqlArray[$i]) or die ('Error: '.mysql_error());;
}
The result is that the last three tables are inserted but the first one is not, and the error is :
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 ' insert into `a` values (17,11,5), (18,12,7), (19,12,10), (21,14,4' at line 1
I validated that the $sqlArray has the correct contains and the queries are correct and runnable from phpmyadmin.
The problem seems to be regardless of the first table (i.e. it will show up even if b was first) and it always seems to "cut" the query in the middle (or after almost 70 characters).
Any help will be appreciated!
Your second statement has a typo ("int" should be "into"):
insert int `b` values (14,'2009-01-06',''),
Alternately, if that's not the issue, try using separate insert statements to get a clearer error message:
insert into `a` values (17,11,5);
insert into `a` values (118,12,7);
...
I have experienced on some older versions of MySQL that they don't like the extended inserts. You can also try specifying the column names explicitly with the real names of your columns. This would be useful if you have more than 3 columns in those tables (an auto-increment column for example).
insert into `a` (`[column1name]`, `[column2name]`, `[column3name]`) values (17,11,5);
I'm having problems writing data from a form to multiple datatables on my PostgreSQL database.
Here is my data model
CREATE TABLE institutions(i_id PK, name text, memberofstaff REFERENCES staff u_id);
CREATE TABLE staff(u_id PK, username text, password text, institution REFERENCES institutions i_id);
So its a 1:1 relationship. These tables have been set up fine. It's the PHP script I'm having difficult with. I'm using CTE-datamodifying or at least trying to but I keep receiving errors on submit.
The PHP:
$conn = pg_connect('database information filled out in code');
$result = pg_query("WITH x AS (
INSERT INTO staff(username, password, institution)
VALUES('$username', '$password', nextval('institutions_i_id_seq'))
RETURNING u_id, i_id)
INSERT INTO institutions (i_id, name, memberofstaff)
SELECT x.i_id, x.u_id, '$institution'
FROM x");
pg_close($conn);
So that's the code and the error I get is:
Warning: pg_query() [function.pg-query]: Query failed: ERROR: relation "institutions_i_id_seq" does not exist LINE 3: VALUES('AberLibrary01', '', nextval('institutions_i_id_se... ^ in DIRECTORY LISTING(I replaced this) on line 22
Anyone got any ideas?
Did you actually create the table with the column types of institutions.i_id and staff.u_id set to serial? Or create the sequence manually?
If the former, you don't need to explicitly use nextval anyway. If the latter, double-check the sequence name.
This would ideally be a comment, but I think it might have something to do with:
$'password' which should be '$password' on the fourth line of that example.
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.