Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
mysql_query("
INSERT INTO `LMS`.`Presentation`
('Pre_Name' ,'Path' ,'PLec_ID' ,'pdatein' ,'pdesc','PSems_ID')
values
('$fname','$newname','$com',NOW(),'$filedesc','$semes')"
) or die("failed");
Dear All,
I have a table named presentation and I am going to enter value to it, it is mentionable that $com and $sems are comboboxs value, but the query show failed, anyone could help please,
thanks in advance
You're using quotes when you should be using backticks:
mysql_query("INSERT INTO `LMS`.`Presentation` (`Pre_Name`, `Path`, ...
Or simply don't use any special character. The backtick is only necessary if you do something silly like use a reserved word as a column name and I would hope people would choose their column names to be more readable.
In other words, date and in and select are silly names for columns, you should be using expiry_date, isInLocation and selectionStatus.
change or die("failed") into or die(mysql_error()) and you'll know why.
btw, consider changing from mysql functions to mysqli functions. And use parameterized queries. Otherwise you will be open to SQL injection.
mysql_query("
INSERT INTO table
(column1, column2, column3, column4 .... columnX)
VALUES(column1Data,column2Data, column3Data, column4Data ... columnXdata)
") or die(mysql_error());
> rove on this example and if there isnt a alias for a table then could not use a alias.
everything hiddends on details..
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Been sitting here all morning trying to improve safety when users create a new user in my system. And finally when everything is fixed and everything works, and at the end of the code where i send informations to the database, im getting a SQL syntax error sigh!!
Here's the error i get:
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 'by) VALUES('tester', 'blabla', 'xx', 'bla#bla.dk', '1387708599', './gfx/profilbi' at line 1
And heres my code..
mysql_query("INSERT INTO brugere (brugernavn, kodeord, salt_encrypt, email, timestamp, img, ip, status, by) VALUES('$_POST[brugernavn]', '$_POST[kode]', 'vissevasse', '$_POST[email]', '$time', '$uploadfile', '$ip_adresse', '0', '$_POST[by]') ") or die(mysql_error());
hope you guys can spot this one, because i can't!
BY is a mysql reserved keyword you need to wrap it with back-ticks
INSERT INTO brugere (`brugernavn`, `kodeord`, `salt_encrypt`, `email`, `timestamp`, `img`, `ip`, `status`, `by`) ....
Reserved Words in MySQL
by
is a reserved mysql keyword
even when you code no longer will throw an error it is HIGHLY advisable that you do NOT use something like
'$_POST[brugernavn]'
directly in a query. Google for sql injection and maybe also consider what xkcd has to say about this
Your code is vulnerable to SQL Injection.
fetch post parameter using
$something = mysqli_real_eacape_string($_POST['<something>']);
and then use that variable in your insert query.
Also since mysql is deprecated use mysqli/PDO
For reserved mysql keyword try to use back tick character (`)
eg:
INSERT INTO users (by) VALUES ('Jibran')
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
How should I make a sql update query only IF a value of the table is equal to "something"?
I would not want to use case because I don't have any "else" statement and it is regulated by another simple value of the table, so there are no more cases.
EDIT: Since there is so much need to see one single line of code because certainly my question has no answer this way, I'll leave it here:
$query = "IF seen=1 UPDATE something SET other_thing = 100 WHERE yet_another_thing= 'outro' ";
You use a where statement:
update t
set foo = bar
where value = 'something';
Looking at everyone's answers, here is the code for YOU.
$query = "UPDATE something SET other_thing = 100 WHERE yet_another_thing= 'outro' AND seen = 1";
This is where use use a WHERE clause:
UPDATE
SomeTable
SET
field = 1234
WHERE
anotherField = 5678
UPDATE tablename SET updatevalue = 'value' WHERE avalue = 'something'
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I am retrieving multiple records from a table in MySQL. Is there a way that I can retrieve one row which totals each of the columns from this query? I was hoping that there might be an SQL solution, as I'll be repeating the process for many tables as it's being used for a dynamic report.
If there is no SQL solution, is there a way to easily add together all the nested arrays inside of an array? This would achieve the same result, but in PHP.
I can't seem to find any information on either approach.
You can do this using using aggregation. You don't give much guidance on what your query looks like, but something like:
select sum(col1) as sumcol1, sum(col2) as sumcol2, . . .
from t;
Without the group by clause, this returns one row, which is a summary of everything in t.
I am not sure what you exactly want to achieve by reading your question as you did not provide any codes or data. Try the following:
SELECT id, col_1, col_2, col_3, col_4, sum(t.Col)
FROM
(SELECT id, col_1, col_2, col_3, col_4,
SUM(col_1+col_2+col_3+col_4)
AS Col
FROM datas
WHERE id = '1'
GROUP BY id)t
You can see the fiddle here
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have many records in my table
I want to know that how can i use loop to insert all record in a single query.
e.g
DATABASE TABLE:
invoice_id___ Product_1____Price_1___Product_2____Price_2_____Product_3____Price_3
similarly there are 10 products i want to insert them against same invoice id(primary key.)
how can I do this please help. thanks
From the mySQL Manual
INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);
Here is the documentation link http://dev.mysql.com/doc/refman/5.6/en/insert.html
The problem is the database design. It's broken because it's not normalized. This makes it hard (to the point of being practically unfeasible) to write inserts, query, or establish DRI.
Instead the tables should look something like the following. Note that the InvoicedProducts table establishes a Many-to-Many relationship.
Invoices
--
invoiceNumber
invoiceDate
Products
--
productName
productPrice -- current product price
InvoicedProducts
--
Invoice (FK) -- the Invoice and Product FKs create a compound PK
Product (FK)
invoicedProductPrice -- product purchased at price (in case the price changes)
Then you simply insert the into the InvoicedProducts like ..
INSERT INTO InvoiceProducts (invoiceId, productId, invoidedProductPrice)
VALUES(.., .., ..)
.. the appropriate number of times (or, once per Invoice/Product pair). Use separate INSERT statements, but a single transaction.
try this
$con=mysqli_connect(hostname,username,password,databasename);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_query($con,"INSERT INTO table_name (a,b,c)
VALUES ('Peter', 'Griffin'),('absss','avdss','advas'),(7,8,9)");
Syntax is similar as below
-- INSERT INTO TABLE_NAME(COLUMN1, COLUMN2) VALUES(VALUE1,VALUE2),(VALUE3,VALUE4)....
But if you have multiple places to insert/update/delete, check the database design. You may need to normalize it.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm trying to let users insert a subject to an image. I know I can't use a WHERE clause in combination with an INSERT INTO statement, and I know that I should use SELECT. I am very new to mysql and I didn't understand the results I tried to search on google. So I needed a more specific answer;)
$classed = mysql_query("INSERT INTO images (subject) VALUES ('$_POST[subject]') WHERE image_id='$_POST[id]'");
You can't use WHERE in your INSERT statement. Consider trying UPDATE if you want to update existing rows, or INSERT without the WHERE to insert new rows.
Don't use the mysql_query function, as it deprecated. Try using PDO or mysqli_query instead.
Don't ever use unfiltered input in your query.
if you already have image_id value then you should UPDATE your table not INSERT.
you should escape your variables before inserting them in your table. by
mysql_real_escape_string()
try this:
$subject = mysql_real_escape_string($_POST['subject']) ;
$id =mysql_real_escape_string($_POST['id'] ) ;
$classed = mysql_query("UPDATE images
SET subject = '".$subject."'
WHERE image_id='".$id."' ");
You cannot use INSERT in existing rows, try UPDATE the SQL will be something like
UPDATE images SET subject = '...' WHERE id=x;