Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a text box with an extension of thousands.
In the text box the user enters only "7" for 7000 and "10" for 10000 and "75" for 75000. The values of 7000, 10000 has to be stored in our database. How can we do this?
As of Initial step, I have created a normal database connection to store values in the database. My databse will store only values in the format of 7,10,75 as user entered but not of multiplication*1000
But I would like to store it in the form of thousands. Could you let me know how to code this in the database?
Thanks
Multiply it by 1000 while entering.
eq
<?php
$number=intval($_POST["number"])*1000;
$sql="insert into database.table values($number)";
//query to database
?>
You can do simple arithmetic with SQL. When you are inserting, use a query something like this:
INSERT INTO table (thous_val) VALUES (1000 * ?)
where ? is the value from your form.
Upon retrieval do something like this. Use the integer DIV operator or you'll get numbers after the decimal place.
SELECT (thous_val DIV 1000) val
FROM table
You can use CONCATENATION while inserting values to table.
Like...
$sql = "INSERT INTO YourTable (col1, col2, price) VALUES ('val1', 'val2', '".intval($_POST["price"])*1000."')";
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Let's say that I have a database with this data stored:
Email-Link-Link2
1E#231-Example1-Example2
1E#231-Example3-Example4
How can I select all of this value with a PDO query excluding those seen before, so in this case 1E#231,will select only one time.
If understand what you said, this is your answer:
Connexion to data base with PDO
<?php
//CONNEXION TO DATA BASE WITH PDO...
$pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
$base = new PDO('mysql:host=localhost;dbname=your_data_base', 'the_user', 'Your_password', $pdo_options);
//end of connexion to data_base
;?>
NOW getting data from data base with not duplication(you can use sql function DISTINCT
<?php
$sc=$base->query("SELECT DISTINCT Email FROM table_where_data_stored");
while($data_distinct =$sc->fetch()){
//your data will be displayed here
;}
;?>
May this help you!!
To evade double selection commonly one use the DISTINCT statement, like this:
SELECT DISTINCT *
FROM myTable
But the best way to evade double-selection is to evade redundant entries to the database. As far as I understand the two entries in your database the second one is an update, and you don't want to overwrite the previous entry. This is a well known problem, and commonly solved like this:
There is a certain date from when on the new eMail should be used, so add a field "valid_from" and add this WHERE statement to your query:
WHERE valid_from > GETDATE()
GETDATE() returns a datetime, therefore it's enough to select "greater than".
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I've been trying to insert a tuple into my relation, but it doesn't seem to work.
<?php
$link=mysql_connect ("connection", "name", "pw");
mysql_select_db('pizzaria');
$sql = "INSERT INTO pizza_bakery (Name, Address, Telenum)
VALUES ('Test', 'Test', '111')";
?>
The connection seems to go through ok, as I'm able to print the table out easily on my html page, why is this failing?
Your insert statement should look something like this:
$sql = "INSERT INTO pizza_bakery (Name, Address, Telenum)
VALUES ('Test', 'Test', 111)";
Reason: The last column (Telenum) is an integer datatype and the insert statement was treating it like a string.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I've inserted into databases before but never used the 'where' feature. For some reason, it is not inserting, but dieing instead.
<?php
$member=$_SESSION['member'];
$SQL = "INSERT into members where name='$member'(money) VALUES ('100')";
mysql_query($SQL) or die("Could not insert money");
print "Money successfully inserted";
?>
This is not valid SQL:
INSERT into members where name='$member'(money) VALUES ('100')
I would assume something like this:
update `members` set `money`=100 where `name`='$member';
Rationale: (money) is a field and 100 is the value for money (since those 2 make the most sense from a INSERT INTO members (field) VALUES (value) syntax point of view).
Never die() with a fixed error message, especially when you can output the actual reason: ... or die(mysql_error()).
But yes, your problem is a syntax error. INSERT queries do NOT have a WHERE clause - where is used to filter records already in the database table. This makes no sense for a new record, because it's not IN the table to filtered in the first place.
You query should basically be just
INSERT into members (name, money) VALUES ('$member', '100')
And note that you are vulnerable to SQL injection attacks, and are using a deprecated/obsolete database interface.
If you want to change existing data, use the update command instead of insert.
You can't use WHERE clause with INSERT command
http://dev.mysql.com/doc/refman/5.0/en/insert.html
You have to do an update
<?php
$member=$_SESSION['member'];
$SQL = "UPDATE `members` SET `money`='100' WHERE `name`='$member'; ";
mysql_query($SQL) or die("Could not insert money");
print "Money successfully inserted";
?>
For inserting :
$SQL = "INSERT INTO members(money) VALUES ('100')";
MySQL INSERT Syntax does not support the WHERE clause. MySQL.com Insert Info
Are you actually trying to insert a new row, or update an existing 'member'? If update, then try:
UPDATE members SET money = 100, WHERE name='$member';
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
$SQL="SELECT first_name FROM people WHERE fname = '$fname' INSERT INTO (first_name) VALUES (fname)";
Anything wrong with this? Trying to insert a value from a user defined variable into a mysql table
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in D:\wamp\www\Pxxxx\process.php on line 44
This is the error
$fname is a user defined variable
first_name is the column I'm trying to insert it into and it's in a table called people
You have the order inverted. It seems like you are looking for INSERT .. SELECT syntax (see MySQL documentation here: http://dev.mysql.com/doc/refman/5.6/en/insert-select.html)
INSERT INTO target_table (first_name)
SELECT fname
FROM people
WHERE fname = ?
It was unclear from your example what the name of the table you were trying to insert data into is, so I just listed it as target_table here.
Your SQL statement has to be reordered like this:
"INSERT INTO people (fname) SELECT '$fname' FROM dual;"
This will select the value of $fname from the pseudo table "dual" and insert the value into "people".
Maybe this is more suitable:
"INSERT INTO people (fname) VALUES ('$fname');"
This snippet show you a simple insert statement.
Note: Please have a look for SQL Injection at Wikipedia. The code you are writing is open for these kinds of attacks. If you are writing PHP code, have a look for Prepared Statements and mysqli to prevent these attacks.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
How can I post only one word into the database.
I only need to insert one word from the values entered by a user.
For example: this is my car to insert only the word this
Here is my present code:
$write=mysql_query("INSERT INTO `staff_description` (`id`, `staff_name`, `description`, `description2`,`writer`, `date_stamp`) VALUES ('$id', '$staff_name', '$description', '$description2', '$user', '$date_stamp');") or die(mysql_error());
please assist
If you are looking to take only 1 word from user inputs and insert it into your database you could use explode() function as follows:
$description1 = $_POST['description1'];
$arr = explode(' ',$description1 ); // this will explode the string after every space
echo $arr[0]; //this is the first word
echo $arr[1];
echo $arr[2];
you could use this to insert into database like :
insert into database VALUES($arr[0])
// this will take the first word from the user input
If your table has only 1 column then you can simply use the INSERT query. If your table has multiple rows there can be 2 cases:
The row already exits - In this case you will have to use the UPDATE query.
The row doesn't exist - You may insert "1 word" into any column using the INSERT query. The other columsn will take default values which you have defined while creating the table OR you can simple add blanks if you plan to use those columns later.